From 4d77fd2c469cb32a5b06308f08290afd685f99be Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 29 Feb 2024 02:09:16 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/52535 Refs: https://github.com/nodejs/node/issues/47472 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Yagiz Nizipli Reviewed-By: Mohammed Keyvanzadeh --- doc/api/cli.md | 29 ++ node.gyp | 2 + src/api/environment.cc | 1 + src/compile_cache.cc | 387 ++++++++++++++++++ src/compile_cache.h | 84 ++++ src/debug_utils.h | 1 + src/env-inl.h | 10 + src/env.cc | 18 + src/env.h | 6 + src/module_wrap.cc | 34 +- src/node_contextify.cc | 29 +- src/node_internals.h | 1 + src/util.cc | 6 +- .../es-modules/dynamic-import/import.cjs | 2 + .../es-modules/dynamic-import/import.mjs | 2 + .../dynamic-import/node_modules/deps/mod.js | 2 + .../node_modules/deps/package.json | 4 + .../parallel/test-compile-cache-bad-syntax.js | 54 +++ .../test-compile-cache-dynamic-import.js | 111 +++++ test/parallel/test-compile-cache-esm.js | 53 +++ .../test-compile-cache-existing-directory.js | 55 +++ .../test-compile-cache-permission-allowed.js | 78 ++++ ...est-compile-cache-permission-disallowed.js | 100 +++++ test/parallel/test-compile-cache-success.js | 66 +++ .../test-compile-cache-updated-file.js | 78 ++++ 25 files changed, 1203 insertions(+), 10 deletions(-) create mode 100644 src/compile_cache.cc create mode 100644 src/compile_cache.h create mode 100644 test/fixtures/es-modules/dynamic-import/import.cjs create mode 100644 test/fixtures/es-modules/dynamic-import/import.mjs create mode 100644 test/fixtures/es-modules/dynamic-import/node_modules/deps/mod.js create mode 100644 test/fixtures/es-modules/dynamic-import/node_modules/deps/package.json create mode 100644 test/parallel/test-compile-cache-bad-syntax.js create mode 100644 test/parallel/test-compile-cache-dynamic-import.js create mode 100644 test/parallel/test-compile-cache-esm.js create mode 100644 test/parallel/test-compile-cache-existing-directory.js create mode 100644 test/parallel/test-compile-cache-permission-allowed.js create mode 100644 test/parallel/test-compile-cache-permission-disallowed.js create mode 100644 test/parallel/test-compile-cache-success.js create mode 100644 test/parallel/test-compile-cache-updated-file.js diff --git a/doc/api/cli.md b/doc/api/cli.md index 5c40f209139e5e..d60cf069f9de7f 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -2501,6 +2501,34 @@ Any other value will result in colorized output being disabled. [`NO_COLOR`][] is an alias for `NODE_DISABLE_COLORS`. The value of the environment variable is arbitrary. +### `NODE_COMPILE_CACHE=dir` + + + +> Stability: 1.1 - Active Development + +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. + +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. + ### `NODE_DEBUG=module[,…]`