mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 21:19:50 +01: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
2.2 KiB
JavaScript
79 lines
2.2 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 tmpdir = require('../common/tmpdir');
|
|
const fixtures = require('../common/fixtures');
|
|
const fs = require('fs');
|
|
|
|
function testAllowed(readDir, writeDir, envDir) {
|
|
console.log(readDir, writeDir, envDir); // Logging for debugging.
|
|
|
|
tmpdir.refresh();
|
|
const dummyDir = tmpdir.resolve('dummy');
|
|
fs.mkdirSync(dummyDir);
|
|
const script = tmpdir.resolve(dummyDir, 'empty.js');
|
|
fs.copyFileSync(fixtures.path('empty.js'), script);
|
|
// If the directory doesn't exist, permission will just be disallowed.
|
|
fs.mkdirSync(tmpdir.resolve(envDir));
|
|
|
|
spawnSyncAndAssert(
|
|
process.execPath,
|
|
[
|
|
'--experimental-permission',
|
|
`--allow-fs-read=${dummyDir}`,
|
|
`--allow-fs-read=${readDir}`,
|
|
`--allow-fs-write=${writeDir}`,
|
|
script,
|
|
],
|
|
{
|
|
env: {
|
|
...process.env,
|
|
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
|
|
NODE_COMPILE_CACHE: `${envDir}`
|
|
},
|
|
cwd: tmpdir.path
|
|
},
|
|
{
|
|
stderr(output) {
|
|
assert.match(output, /writing cache for .*empty\.js.*success/);
|
|
return true;
|
|
}
|
|
});
|
|
|
|
spawnSyncAndAssert(
|
|
process.execPath,
|
|
[
|
|
'--experimental-permission',
|
|
`--allow-fs-read=${dummyDir}`,
|
|
`--allow-fs-read=${readDir}`,
|
|
`--allow-fs-write=${writeDir}`,
|
|
script,
|
|
],
|
|
{
|
|
env: {
|
|
...process.env,
|
|
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
|
|
NODE_COMPILE_CACHE: `${envDir}`
|
|
},
|
|
cwd: tmpdir.path
|
|
},
|
|
{
|
|
stderr(output) {
|
|
assert.match(output, /cache for .*empty\.js was accepted/);
|
|
return true;
|
|
}
|
|
});
|
|
}
|
|
|
|
{
|
|
testAllowed(tmpdir.resolve('.compile_cache'), tmpdir.resolve('.compile_cache'), '.compile_cache');
|
|
testAllowed(tmpdir.resolve('.compile_cache'), tmpdir.resolve('.compile_cache'), tmpdir.resolve('.compile_cache'));
|
|
testAllowed('*', '*', '.compile_cache');
|
|
testAllowed('*', tmpdir.resolve('.compile_cache'), '.compile_cache');
|
|
testAllowed(tmpdir.resolve('.compile_cache'), '*', '.compile_cache');
|
|
}
|