Skip to content

Commit

Permalink
remove unnecessary copy
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Oct 29, 2024
1 parent 0f72eb3 commit 515dc86
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/workerd/tools/create-compile-cache.c++
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ constexpr bool resourceIsOpaque = false;
constexpr bool isWasm = false;
constexpr bool isModule = true;

v8::ScriptCompiler::CachedData* getCodeCache(kj::StringPtr id) {
const auto& cache = jsg::CompileCache::get();
KJ_IF_SOME(cached, cache.find(id)) {
return cached.AsCachedData().release();
}
return nullptr;
}

// CompileCacheCreator receives an argument of a text file where each line
// represents the path of the file to create compile caches for.
class CompileCacheCreator {
Expand Down Expand Up @@ -53,10 +61,7 @@ public:
dir.openFile(kj::Path::parse(kj::StringPtr(path.asChars().begin(), path.size())));
auto content = file->mmap(0, file->stat().size);

auto heap = kj::heapArray<kj::byte>(content.size());
std::memcpy(heap.begin(), content.begin(), content.size());
// TODO(soon): Rather than storing them on memory, prepare caches while iterating through the list.
file_contents.add(kj::tuple(kj::heapString(path.asChars()), kj::mv(heap)));
file_contents.add(kj::tuple(kj::heapString(path.asChars()), kj::mv(content)));
}

end++;
Expand All @@ -73,7 +78,7 @@ public:
ccIsolate.runInLockScope([&](CompileCacheIsolate::Lock& js) {
for (auto& entry: file_contents) {
kj::StringPtr name = kj::get<0>(entry);
kj::ArrayPtr<kj::byte> content = kj::get<1>(entry);
kj::ArrayPtr<const kj::byte> content = kj::get<1>(entry);

v8::ScriptOrigin origin(jsg::v8StrIntern(js.v8Isolate, name), resourceLineOffset,
resourceColumnOffset, resourceIsSharedCrossOrigin, scriptId, {}, resourceIsOpaque,
Expand Down Expand Up @@ -104,7 +109,7 @@ private:
}

// Key is the path of the file, and value is the content.
kj::Vector<kj::Tuple<kj::String, kj::Array<kj::byte>>> file_contents{};
kj::Vector<kj::Tuple<kj::String, kj::Array<const kj::byte>>> file_contents{};
};

} // namespace
Expand Down

0 comments on commit 515dc86

Please sign in to comment.