Skip to content

Commit

Permalink
src: stop copying code cache
Browse files Browse the repository at this point in the history
The code cache is quite large - around 1.3 MiB. Change the code to use
non-owning buffers to avoid copying it. For starting up an otherwise
empty main isolate, this saves around 1.3 MiB of unique set size memory
(9.9 MiB -> 8.6 MiB) and 1.1ms elapsed time (22.9 ms -> 21.8 ms).

Copying the code cache is unnecessary since:

1. for the builtin snapshot, the code cache data has static lifetime.
2. for non-builtin snapshots, we create copies of the code cache data in
   `SnapshotDeserializer::ReadVector`. These copies are owned by the
   `Environment` (through `IsolateData` -> `SnapshotData`), so they
   won't be deallocated.
3. a worker thread can copy a parent's isolate's code cache, but in that
   case we still know that the parent isolate will outlive the worker
   isolate.

(Admittedly point (2) feels a little fragile from a lifetime
perspective, and I would be happy to restrict this optimization to the
builtin snapshot.)

```console
$ perf stat -r 100 -e ... ./node -e 0

 Performance counter stats for './node -e 0' (100 runs):

             21.78 msec task-clock
              2760      page-faults
         113161604      instructions
          18437648      branches
            423230      branch-misses
            853093      cache-references
             41474      cache-misses

         0.0225473 +- 0.0000504 seconds time elapsed  ( +-  0.22% )

$ perf stat -r 100 -e ... ./node-main -e 0

 Performance counter stats for './node-main -e 0' (100 runs):

             22.91 msec task-clock
              3102      page-faults
         114890673      instructions
          18751329      branches
            428909      branch-misses
            895721      cache-references
             45202      cache-misses

         0.0233760 +- 0.0000741 seconds time elapsed  ( +-  0.32% )
```

PR-URL: #47144
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
  • Loading branch information
kvakil authored and targos committed May 12, 2023
1 parent cad42e7 commit a11507e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@ struct IsolateSettings {
// feature during the build step by passing the --disable-shared-readonly-heap
// flag to the configure script.
//
// The snapshot *must* be kept alive during the execution of the Isolate
// that was created using it.
//
// Snapshots are an *experimental* feature. In particular, the embedder API
// exposed through this class is subject to change or removal between Node.js
// versions, including possible API and ABI breakage.
Expand Down
16 changes: 10 additions & 6 deletions src/node_builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,17 @@ void BuiltinLoader::CopyCodeCache(std::vector<CodeCacheInfo>* out) const {

void BuiltinLoader::RefreshCodeCache(const std::vector<CodeCacheInfo>& in) {
RwLock::ScopedLock lock(code_cache_->mutex);
code_cache_->map.reserve(in.size());
DCHECK(code_cache_->map.empty());
for (auto const& item : in) {
size_t length = item.data.size();
uint8_t* buffer = new uint8_t[length];
memcpy(buffer, item.data.data(), length);
auto new_cache = std::make_unique<v8::ScriptCompiler::CachedData>(
buffer, length, v8::ScriptCompiler::CachedData::BufferOwned);
code_cache_->map[item.id] = std::move(new_cache);
auto result = code_cache_->map.emplace(
item.id,
std::make_unique<v8::ScriptCompiler::CachedData>(
item.data.data(),
item.data.size(),
v8::ScriptCompiler::CachedData::BufferNotOwned));
USE(result.second);
DCHECK(result.second);
}
code_cache_->has_code_cache = true;
}
Expand Down

0 comments on commit a11507e

Please sign in to comment.