Skip to content

Commit 43101ad

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 <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
1 parent 6c2f360 commit 43101ad

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
@@ -2637,6 +2637,34 @@ Any other value will result in colorized output being disabled.
26372637
[`NO_COLOR`][] is an alias for `NODE_DISABLE_COLORS`. The value of the
26382638
environment variable is arbitrary.
26392639

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

26422670
<!-- YAML
@@ -3314,6 +3342,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
33143342
[Subresource Integrity]: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
33153343
[V8 Inspector integration for Node.js]: debugger.md#v8-inspector-integration-for-nodejs
33163344
[V8 JavaScript code coverage]: https://v8project.blogspot.com/2017/12/javascript-code-coverage.html
3345+
[V8 code cache]: https://v8.dev/blog/code-caching-for-devs
33173346
[Web Crypto API]: webcrypto.md
33183347
[`"type"`]: packages.md#type
33193348
[`--allow-addons`]: #--allow-addons

node.gyp

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)