mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
87abd023aa
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 <benjamingr@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
79 lines
1.9 KiB
JavaScript
79 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
// This tests NODE_COMPILE_CACHE works in existing directory.
|
|
|
|
require('../common');
|
|
const { spawnSyncAndAssert } = require('../common/child_process');
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
{
|
|
tmpdir.refresh();
|
|
const dir = tmpdir.resolve('.compile_cache_dir');
|
|
const script = tmpdir.resolve('script.js');
|
|
fs.writeFileSync(script, 'const foo = 1;', 'utf-8');
|
|
|
|
spawnSyncAndAssert(
|
|
process.execPath,
|
|
[script],
|
|
{
|
|
env: {
|
|
...process.env,
|
|
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
|
|
NODE_COMPILE_CACHE: dir
|
|
},
|
|
cwd: tmpdir.path
|
|
},
|
|
{
|
|
stderr(output) {
|
|
assert.match(output, /script\.js was not initialized, initializing the in-memory entry/);
|
|
assert.match(output, /writing cache for .*script\.js.*success/);
|
|
return true;
|
|
}
|
|
});
|
|
|
|
// Update the content.
|
|
fs.writeFileSync(script, 'const foo = 2;', 'utf-8');
|
|
|
|
// Another run regenerates it.
|
|
spawnSyncAndAssert(
|
|
process.execPath,
|
|
[script],
|
|
{
|
|
env: {
|
|
...process.env,
|
|
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
|
|
NODE_COMPILE_CACHE: dir
|
|
},
|
|
cwd: tmpdir.path
|
|
},
|
|
{
|
|
stderr(output) {
|
|
assert.match(output, /reading cache from .* for .*script\.js.*code hash mismatch:/);
|
|
assert.match(output, /writing cache for .*script\.js.*success/);
|
|
return true;
|
|
}
|
|
});
|
|
|
|
// Then it's consumed just fine.
|
|
spawnSyncAndAssert(
|
|
process.execPath,
|
|
[script],
|
|
{
|
|
env: {
|
|
...process.env,
|
|
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
|
|
NODE_COMPILE_CACHE: dir
|
|
},
|
|
cwd: tmpdir.path
|
|
},
|
|
{
|
|
stderr(output) {
|
|
assert.match(output, /cache for .*script\.js was accepted, keeping the in-memory entry/);
|
|
assert.match(output, /.*skip .*script\.js because cache was the same/);
|
|
return true;
|
|
}
|
|
});
|
|
}
|