node icon indicating copy to clipboard operation
node copied to clipboard

src: add JS APIs for compile cache and NODE_DISABLE_COMPILE_CACHE

Open joyeecheung opened this issue 1 year ago • 9 comments

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

joyeecheung avatar Aug 22 '24 16:08 joyeecheung

Review requested:

  • [ ] @nodejs/loaders

nodejs-github-bot avatar Aug 22 '24 16:08 nodejs-github-bot

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'

jakebailey avatar Aug 22 '24 17:08 jakebailey

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:

... and 57 files with indirect coverage changes

codecov[bot] avatar Aug 22 '24 18:08 codecov[bot]

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'

jakebailey avatar Aug 22 '24 18:08 jakebailey

CI: https://ci.nodejs.org/job/node-test-pull-request/61445/

nodejs-github-bot avatar Aug 25 '24 10:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61502/

nodejs-github-bot avatar Aug 26 '24 16:08 nodejs-github-bot

Fixed the tmpdir test (should be checking what's in $tmpdir/node-compile-cache, not $tmpdir directly)

joyeecheung avatar Aug 27 '24 09:08 joyeecheung

CI: https://ci.nodejs.org/job/node-test-pull-request/61537/

nodejs-github-bot avatar Aug 27 '24 09:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61559/

nodejs-github-bot avatar Aug 27 '24 16:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61590/

nodejs-github-bot avatar Aug 28 '24 13:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61594/

nodejs-github-bot avatar Aug 28 '24 15:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61604/

nodejs-github-bot avatar Aug 28 '24 19:08 nodejs-github-bot

CI is finally green...can I get some re-review please? @jasnell @benjamingr @aduh95

joyeecheung avatar Aug 28 '24 22:08 joyeecheung

Landed in ff5ef7083dc23969219bdb7e412ffd5388783a25

nodejs-github-bot avatar Aug 28 '24 23:08 nodejs-github-bot

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.

github-actions[bot] avatar Aug 29 '24 02:08 github-actions[bot]