Skip to content

Commit 87abd02

Browse files
committed
module: implement NODE_COMPILE_CACHE for automatic on-disk code caching
This patch implements automatic on-disk code caching that can be enabled via an environment variable NODE_COMPILE_CACHE. When set, whenever Node.js compiles a CommonJS or a ECMAScript Module, it will use on-disk [V8 code cache][] persisted in the specified directory to speed up the compilation. This may slow down the first load of a module graph, but subsequent loads of the same module graph may get a significant speedup if the contents of the modules do not change. Locally, this speeds up loading of test/fixtures/snapshot/typescript.js from ~130ms to ~80ms. To clean up the generated code cache, simply remove the directory. It will be recreated the next time the same directory is used for `NODE_COMPILE_CACHE`. Compilation cache generated by one version of Node.js may not be used by a different version of Node.js. Cache generated by different versions of Node.js will be stored separately if the same directory is used to persist the cache, so they can co-exist. Caveat: currently when using this with V8 JavaScript code coverage, the coverage being collected by V8 may be less precise in functions that are deserialized from the code cache. It's recommended to turn this off when running tests to generate precise coverage. Implementation details: There is one cache file per module on disk. The directory layout is: - Compile cache directory (from NODE_COMPILE_CACHE) - 8b23c8fe: CRC32 hash of CachedDataVersionTag + NODE_VERESION - 2ea3424d: - 10860e5a: CRC32 hash of filename + module type - 431e9adc: ... - ... Inside the cache file, there is a header followed by the actual cache content: ``` [uint32_t] code size [uint32_t] code hash [uint32_t] cache size [uint32_t] cache hash ... compile cache content ... ``` When reading the cache file, we'll also check if the code size and code hash match the code that the module loader is loading and whether the cache size and cache hash match the file content read. If they don't match, or if V8 rejects the cache passed, we'll ignore the mismatch cache, and regenerate the cache after compilation succeeds and rewrite it to disk. PR-URL: #52535 Refs: #47472 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
1 parent 83eb4f2 commit 87abd02

25 files changed

+1203
-10
lines changed

doc/api/cli.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,34 @@ Any other value will result in colorized output being disabled.
24992499
[`NO_COLOR`][] is an alias for `NODE_DISABLE_COLORS`. The value of the
25002500
environment variable is arbitrary.
25012501

2502+
### `NODE_COMPILE_CACHE=dir`
2503+
2504+
<!-- YAML
2505+
added: REPLACEME
2506+
-->
2507+
2508+
> Stability: 1.1 - Active Development
2509+
2510+
When set, whenever Node.js compiles a CommonJS or a ECMAScript Module,
2511+
it will use on-disk [V8 code cache][] persisted in the specified directory
2512+
to speed up the compilation. This may slow down the first load of a
2513+
module graph, but subsequent loads of the same module graph may get
2514+
a significant speedup if the contents of the modules do not change.
2515+
2516+
To clean up the generated code cache, simply remove the directory.
2517+
It will be recreated the next time the same directory is used for
2518+
`NODE_COMPILE_CACHE`.
2519+
2520+
Compilation cache generated by one version of Node.js may not be used
2521+
by a different version of Node.js. Cache generated by different versions
2522+
of Node.js will be stored separately if the same directory is used
2523+
to persist the cache, so they can co-exist.
2524+
2525+
Caveat: currently when using this with [V8 JavaScript code coverage][], the
2526+
coverage being collected by V8 may be less precise in functions that are
2527+
deserialized from the code cache. It's recommended to turn this off when
2528+
running tests to generate precise coverage.
2529+
25022530
### `NODE_DEBUG=module[,…]`
25032531

25042532
<!-- YAML
@@ -3144,6 +3172,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
31443172
[Source Map]: https://sourcemaps.info/spec.html
31453173
[Subresource Integrity]: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
31463174
[V8 JavaScript code coverage]: https://v8project.blogspot.com/2017/12/javascript-code-coverage.html
3175+
[V8 code cache]: https://v8.dev/blog/code-caching-for-devs
31473176
[Web Crypto API]: webcrypto.md
31483177
[`"type"`]: packages.md#type
31493178
[`--allow-child-process`]: #--allow-child-process

node.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
'src/base_object.cc',
7070
'src/cares_wrap.cc',
7171
'src/cleanup_queue.cc',
72+
'src/compile_cache.cc',
7273
'src/connect_wrap.cc',
7374
'src/connection_wrap.cc',
7475
'src/dataqueue/queue.cc',
@@ -190,6 +191,7 @@
190191
'src/callback_queue-inl.h',
191192
'src/cleanup_queue.h',
192193
'src/cleanup_queue-inl.h',
194+
'src/compile_cache.h',
193195
'src/connect_wrap.h',
194196
'src/connection_wrap.h',
195197
'src/dataqueue/queue.h',

src/api/environment.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ MaybeLocal<Value> LoadEnvironment(Environment* env,
546546
if (preload) {
547547
env->set_embedder_preload(std::move(preload));
548548
}
549+
env->InitializeCompileCache();
549550

550551
return StartExecution(env, cb);
551552
}

0 commit comments

Comments
 (0)