node
node copied to clipboard
src: add JS APIs for compile cache and NODE_DISABLE_COMPILE_CACHE
This patch adds the following API for tools to enable compile cache dynamically and query its status.
- module.enableCompileCache(cacheDir)
- module.getCompileCacheDir()
In addition this adds a NODE_DISABLE_COMPILE_CACHE environment variable to disable the code cache enabled by the APIs as an escape hatch to avoid unexpected/undesired effects of the compile cache (e.g. less precise test coverage).
When the module.enableCompileCache() method is invoked without a specified directory, Node.js will use the value of the NODE_COMPILE_CACHE environment variable if it's set, or defaults to path.join(os.tmpdir(), 'node-compile-cache') otherwise. Therefore it's recommended for tools to call this method without specifying the directory to allow overrides.
Fixes: https://github.com/nodejs/node/issues/53639
Review requested:
- [ ] @nodejs/loaders
I put together https://github.com/microsoft/TypeScript/pull/59720 to enable this in TypeScript; the results are as expected:
Benchmark 1: ~/work/node/out/Release/node ./built/local/_tsc.js --version
Time (mean ± σ): 120.5 ms ± 1.7 ms [User: 108.6 ms, System: 11.2 ms]
Range (min … max): 117.5 ms … 129.1 ms 100 runs
Benchmark 2: ~/work/node/out/Release/node ./built/local/tsc.js --version
Time (mean ± σ): 51.8 ms ± 1.0 ms [User: 42.3 ms, System: 9.0 ms]
Range (min … max): 50.4 ms … 56.6 ms 100 runs
Summary
'~/work/node/out/Release/node ./built/local/tsc.js --version' ran
2.33 ± 0.06 times faster than '~/work/node/out/Release/node ./built/local/_tsc.js --version'
Benchmark 1: echo '{"seq": 1, "command": "status"}' | ~/work/node/out/Release/node ./built/local/_tsserver.js --disableAutomaticTypingAcquisition
Time (mean ± σ): 192.3 ms ± 2.5 ms [User: 178.3 ms, System: 19.8 ms]
Range (min … max): 188.1 ms … 201.1 ms 100 runs
Benchmark 2: echo '{"seq": 1, "command": "status"}' | ~/work/node/out/Release/node ./built/local/tsserver.js --disableAutomaticTypingAcquisition
Time (mean ± σ): 84.6 ms ± 1.1 ms [User: 76.5 ms, System: 14.5 ms]
Range (min … max): 82.6 ms … 88.6 ms 100 runs
Summary
'echo '{"seq": 1, "command": "status"}' | ~/work/node/out/Release/node ./built/local/tsserver.js --disableAutomaticTypingAcquisition' ran
2.27 ± 0.04 times faster than 'echo '{"seq": 1, "command": "status"}' | ~/work/node/out/Release/node ./built/local/_tsserver.js --disableAutomaticTypingAcquisition'
Codecov Report
Attention: Patch coverage is 98.18182% with 2 lines in your changes missing coverage. Please review.
Project coverage is 87.34%. Comparing base (
cc26951) to head (73c03b1). Report is 354 commits behind head on main.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/compile_cache.cc | 75.00% | 1 Missing :warning: |
| src/node_modules.cc | 97.22% | 0 Missing and 1 partial :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## main #54501 +/- ##
========================================
Coverage 87.34% 87.34%
========================================
Files 649 649
Lines 182544 182666 +122
Branches 35030 35042 +12
========================================
+ Hits 159445 159555 +110
- Misses 16372 16387 +15
+ Partials 6727 6724 -3
| Files with missing lines | Coverage Δ | |
|---|---|---|
| lib/internal/modules/helpers.js | 97.85% <100.00%> (+0.28%) |
:arrow_up: |
| lib/module.js | 100.00% <100.00%> (ø) |
|
| src/compile_cache.h | 100.00% <ø> (+25.00%) |
:arrow_up: |
| src/env.cc | 85.48% <100.00%> (+0.32%) |
:arrow_up: |
| src/compile_cache.cc | 80.00% <75.00%> (ø) |
|
| src/node_modules.cc | 76.97% <97.22%> (+2.07%) |
:arrow_up: |
For fun, this is eslint (with enablecompileCache added to bin/eslint.js:
Benchmark 1: NODE_DISABLE_COMPILE_CACHE=true ~/work/node/out/Release/node ./node_modules/eslint/bin/eslint.js --version
Time (mean ± σ): 129.9 ms ± 7.8 ms [User: 128.6 ms, System: 17.4 ms]
Range (min … max): 126.1 ms … 204.6 ms 100 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet PC without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.
Benchmark 2: ~/work/node/out/Release/node ./node_modules/eslint/bin/eslint.js --version
Time (mean ± σ): 105.5 ms ± 1.8 ms [User: 103.3 ms, System: 18.6 ms]
Range (min … max): 101.7 ms … 114.5 ms 100 runs
Summary
'~/work/node/out/Release/node ./node_modules/eslint/bin/eslint.js --version' ran
1.23 ± 0.08 times faster than 'NODE_DISABLE_COMPILE_CACHE=true ~/work/node/out/Release/node ./node_modules/eslint/bin/eslint.js --version'
Benchmark 1: NODE_DISABLE_COMPILE_CACHE=true ~/work/node/out/Release/node ./node_modules/eslint/bin/eslint.js --print-config ./src/compiler/debug.ts
Time (mean ± σ): 791.6 ms ± 6.2 ms [User: 987.2 ms, System: 86.3 ms]
Range (min … max): 779.1 ms … 811.3 ms 100 runs
Benchmark 2: ~/work/node/out/Release/node ./node_modules/eslint/bin/eslint.js --print-config ./src/compiler/debug.ts
Time (mean ± σ): 595.0 ms ± 6.4 ms [User: 786.4 ms, System: 89.5 ms]
Range (min … max): 583.7 ms … 621.3 ms 100 runs
Summary
'~/work/node/out/Release/node ./node_modules/eslint/bin/eslint.js --print-config ./src/compiler/debug.ts' ran
1.33 ± 0.02 times faster than 'NODE_DISABLE_COMPILE_CACHE=true ~/work/node/out/Release/node ./node_modules/eslint/bin/eslint.js --print-config ./src/compiler/debug.ts'
CI: https://ci.nodejs.org/job/node-test-pull-request/61445/
CI: https://ci.nodejs.org/job/node-test-pull-request/61502/
Fixed the tmpdir test (should be checking what's in $tmpdir/node-compile-cache, not $tmpdir directly)
CI: https://ci.nodejs.org/job/node-test-pull-request/61537/
CI: https://ci.nodejs.org/job/node-test-pull-request/61559/
CI: https://ci.nodejs.org/job/node-test-pull-request/61590/
CI: https://ci.nodejs.org/job/node-test-pull-request/61594/
CI: https://ci.nodejs.org/job/node-test-pull-request/61604/
CI is finally green...can I get some re-review please? @jasnell @benjamingr @aduh95
Landed in ff5ef7083dc23969219bdb7e412ffd5388783a25
The https://github.com/nodejs/node/labels/notable-change label has been added by @joyeecheung.
Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment. Otherwise, the commit will be placed in the Other Notable Changes section.