Skip to content

Commit 6f26cd4

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: nodejs#52535 Refs: nodejs#47472 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
1 parent df97c26 commit 6f26cd4

25 files changed

+1203
-10
lines changed

doc/api/cli.md

+29
Original file line numberDiff line numberDiff line change
@@ -2648,6 +2648,34 @@ Any other value will result in colorized output being disabled.
26482648
[`NO_COLOR`][] is an alias for `NODE_DISABLE_COLORS`. The value of the
26492649
environment variable is arbitrary.
26502650

2651+
### `NODE_COMPILE_CACHE=dir`
2652+
2653+
<!-- YAML
2654+
added: REPLACEME
2655+
-->
2656+
2657+
> Stability: 1.1 - Active Development
2658+
2659+
When set, whenever Node.js compiles a CommonJS or a ECMAScript Module,
2660+
it will use on-disk [V8 code cache][] persisted in the specified directory
2661+
to speed up the compilation. This may slow down the first load of a
2662+
module graph, but subsequent loads of the same module graph may get
2663+
a significant speedup if the contents of the modules do not change.
2664+
2665+
To clean up the generated code cache, simply remove the directory.
2666+
It will be recreated the next time the same directory is used for
2667+
`NODE_COMPILE_CACHE`.
2668+
2669+
Compilation cache generated by one version of Node.js may not be used
2670+
by a different version of Node.js. Cache generated by different versions
2671+
of Node.js will be stored separately if the same directory is used
2672+
to persist the cache, so they can co-exist.
2673+
2674+
Caveat: currently when using this with [V8 JavaScript code coverage][], the
2675+
coverage being collected by V8 may be less precise in functions that are
2676+
deserialized from the code cache. It's recommended to turn this off when
2677+
running tests to generate precise coverage.
2678+
26512679
### `NODE_DEBUG=module[,…]`
26522680

26532681
<!-- YAML
@@ -3325,6 +3353,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
33253353
[Subresource Integrity]: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
33263354
[V8 Inspector integration for Node.js]: debugger.md#v8-inspector-integration-for-nodejs
33273355
[V8 JavaScript code coverage]: https://v8project.blogspot.com/2017/12/javascript-code-coverage.html
3356+
[V8 code cache]: https://v8.dev/blog/code-caching-for-devs
33283357
[Web Crypto API]: webcrypto.md
33293358
[`"type"`]: packages.md#type
33303359
[`--allow-addons`]: #--allow-addons

node.gyp

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
'src/base_object.cc',
7171
'src/cares_wrap.cc',
7272
'src/cleanup_queue.cc',
73+
'src/compile_cache.cc',
7374
'src/connect_wrap.cc',
7475
'src/connection_wrap.cc',
7576
'src/dataqueue/queue.cc',
@@ -192,6 +193,7 @@
192193
'src/callback_queue-inl.h',
193194
'src/cleanup_queue.h',
194195
'src/cleanup_queue-inl.h',
196+
'src/compile_cache.h',
195197
'src/connect_wrap.h',
196198
'src/connection_wrap.h',
197199
'src/dataqueue/queue.h',

src/api/environment.cc

+1
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ MaybeLocal<Value> LoadEnvironment(Environment* env,
512512
if (preload) {
513513
env->set_embedder_preload(std::move(preload));
514514
}
515+
env->InitializeCompileCache();
515516

516517
return StartExecution(env, cb);
517518
}

0 commit comments

Comments
 (0)