0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-compile-cache-dynamic-import.js
Joyee Cheung 87abd023aa
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 <benjamingr@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
2024-04-17 23:39:45 +02:00

112 lines
3.4 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');
{
tmpdir.refresh();
const dir = tmpdir.resolve('.compile_cache_dir');
const cjs = fixtures.path('es-modules', 'dynamic-import', 'import.cjs');
const mjs = fixtures.path('es-modules', 'dynamic-import', 'import.mjs');
spawnSyncAndAssert(
process.execPath,
[cjs],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_COMPILE_CACHE: dir
},
cwd: tmpdir.path
},
{
trim: true,
stdout: 'hello world',
stderr(output) {
assert.match(output, /reading cache from .* for ESM .*mod\.js/);
assert.match(output, /mod\.js was not initialized, initializing the in-memory entry/);
assert.match(output, /writing cache for .*mod\.js.*success/);
assert.match(output, /reading cache .* for CommonJS .*import\.cjs/);
assert.match(output, /import\.cjs was not initialized, initializing the in-memory entry/);
assert.match(output, /writing cache for .*import\.cjs.*success/);
return true;
}
});
spawnSyncAndAssert(
process.execPath,
[cjs],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_COMPILE_CACHE: dir
},
cwd: tmpdir.path
},
{
trim: true,
stdout: 'hello world',
stderr(output) {
assert.match(output, /cache for .*mod\.js was accepted, keeping the in-memory entry/);
assert.match(output, /.*skip .*mod\.js because cache was the same/);
assert.match(output, /cache for .*import\.cjs was accepted, keeping the in-memory entry/);
assert.match(output, /.*skip .*import\.cjs because cache was the same/);
return true;
}
});
spawnSyncAndAssert(
process.execPath,
[mjs],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_COMPILE_CACHE: dir
},
cwd: tmpdir.path
},
{
trim: true,
stdout: 'hello world',
stderr(output) {
assert.match(output, /cache for .*mod\.js was accepted, keeping the in-memory entry/);
assert.match(output, /.*skip .*mod\.js because cache was the same/);
assert.match(output, /reading cache .* for ESM .*import\.mjs/);
assert.match(output, /import\.mjs was not initialized, initializing the in-memory entry/);
assert.match(output, /writing cache for .*import\.mjs.*success/);
return true;
}
});
spawnSyncAndAssert(
process.execPath,
[mjs],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
NODE_COMPILE_CACHE: dir
},
cwd: tmpdir.path
},
{
trim: true,
stdout: 'hello world',
stderr(output) {
assert.match(output, /cache for .*mod\.js was accepted, keeping the in-memory entry/);
assert.match(output, /.*skip .*mod\.js because cache was the same/);
assert.match(output, /cache for .*import\.mjs was accepted, keeping the in-memory entry/);
assert.match(output, /.*skip .*import\.mjs because cache was the same/);
return true;
}
});
}