Skip to content

perf: prefer module.enableCompileCache over v8-compile-cache#574

Merged
aduh95 merged 2 commits intonodejs:mainfrom
merceyz:merceyz/perf/node-compile-cache
Nov 23, 2024
Merged

perf: prefer module.enableCompileCache over v8-compile-cache#574
aduh95 merged 2 commits intonodejs:mainfrom
merceyz:merceyz/perf/node-compile-cache

Conversation

@merceyz
Copy link
Copy Markdown
Member

@merceyz merceyz commented Nov 17, 2024

Node.js has added a built-in compile cache so this PR changes the code to prefer that over v8-compile-cache.

https://nodejs.org/docs/v22.11.0/api/module.html#moduleenablecompilecachecachedir

Fixes #342
Closes #573 (supersedes it)

Results

$ node -v
v22.11.0
$ yarn -v
4.5.1
$ npm -v
10.9.0
$ pnpm -v
9.13.2
$ COREPACK_ENABLE_AUTO_PIN=0 hyperfine -w 5 \
  "node ./dist-base/corepack.js -v" \
  "node ./dist-v8-compile-cache/corepack.js -v" \
  "node ./dist-node-compile-cache/corepack.js -v" \
  "node ./dist-base/yarn.js -v" \
  "node ./dist-v8-compile-cache/yarn.js -v" \
  "node ./dist-node-compile-cache/yarn.js -v" \
  "node ./dist-base/npm.js -v" \
  "node ./dist-v8-compile-cache/npm.js -v" \
  "node ./dist-node-compile-cache/npm.js -v" \
  "node ./dist-base/pnpm.js -v" \
  "node ./dist-v8-compile-cache/pnpm.js -v" \
  "node ./dist-node-compile-cache/pnpm.js -v"
Benchmark 1: node ./dist-base/corepack.js -v
  Time (mean ± σ):      47.2 ms ±   0.5 ms    [User: 40.2 ms, System: 10.4 ms]
  Range (min … max):    46.2 ms …  48.1 ms    63 runs

Benchmark 2: node ./dist-v8-compile-cache/corepack.js -v
  Time (mean ± σ):      47.9 ms ±   0.9 ms    [User: 40.0 ms, System: 11.3 ms]
  Range (min … max):    46.6 ms …  50.7 ms    62 runs

Benchmark 3: node ./dist-node-compile-cache/corepack.js -v
  Time (mean ± σ):      38.9 ms ±   1.1 ms    [User: 29.0 ms, System: 12.8 ms]
  Range (min … max):    37.9 ms …  43.6 ms    76 runs

Benchmark 4: node ./dist-base/yarn.js -v
  Time (mean ± σ):     187.0 ms ±   1.7 ms    [User: 179.3 ms, System: 30.5 ms]
  Range (min … max):   184.6 ms … 189.9 ms    16 runs

Benchmark 5: node ./dist-v8-compile-cache/yarn.js -v
  Time (mean ± σ):     146.8 ms ±   1.1 ms    [User: 134.7 ms, System: 33.5 ms]
  Range (min … max):   145.0 ms … 149.1 ms    20 runs

Benchmark 6: node ./dist-node-compile-cache/yarn.js -v
  Time (mean ± σ):     131.8 ms ±   1.8 ms    [User: 122.5 ms, System: 29.1 ms]
  Range (min … max):   129.3 ms … 136.1 ms    22 runs

Benchmark 7: node ./dist-base/npm.js -v
  Time (mean ± σ):      90.5 ms ±   0.8 ms    [User: 89.0 ms, System: 17.2 ms]
  Range (min … max):    89.0 ms …  92.3 ms    33 runs

Benchmark 8: node ./dist-v8-compile-cache/npm.js -v
  Time (mean ± σ):      90.8 ms ±   0.8 ms    [User: 87.1 ms, System: 19.4 ms]
  Range (min … max):    89.0 ms …  92.8 ms    32 runs

Benchmark 9: node ./dist-node-compile-cache/npm.js -v
  Time (mean ± σ):      76.0 ms ±   0.9 ms    [User: 67.9 ms, System: 23.2 ms]
  Range (min … max):    74.8 ms …  79.3 ms    39 runs

Benchmark 10: node ./dist-base/pnpm.js -v
  Time (mean ± σ):     325.8 ms ±   2.3 ms    [User: 324.5 ms, System: 32.3 ms]
  Range (min … max):   322.1 ms … 330.3 ms    10 runs

Benchmark 11: node ./dist-v8-compile-cache/pnpm.js -v
  Time (mean ± σ):     256.1 ms ±   2.4 ms    [User: 236.1 ms, System: 47.5 ms]
  Range (min … max):   251.0 ms … 259.3 ms    11 runs

Benchmark 12: node ./dist-node-compile-cache/pnpm.js -v
  Time (mean ± σ):     233.7 ms ±   1.9 ms    [User: 224.8 ms, System: 34.6 ms]
  Range (min … max):   229.1 ms … 236.4 ms    12 runs

Summary
  node ./dist-node-compile-cache/corepack.js -v ran
    1.21 ± 0.04 times faster than node ./dist-base/corepack.js -v
    1.23 ± 0.04 times faster than node ./dist-v8-compile-cache/corepack.js -v
    1.95 ± 0.06 times faster than node ./dist-node-compile-cache/npm.js -v
    2.33 ± 0.07 times faster than node ./dist-base/npm.js -v
    2.33 ± 0.07 times faster than node ./dist-v8-compile-cache/npm.js -v
    3.39 ± 0.11 times faster than node ./dist-node-compile-cache/yarn.js -v
    3.77 ± 0.11 times faster than node ./dist-v8-compile-cache/yarn.js -v
    4.81 ± 0.14 times faster than node ./dist-base/yarn.js -v
    6.01 ± 0.18 times faster than node ./dist-node-compile-cache/pnpm.js -v
    6.58 ± 0.19 times faster than node ./dist-v8-compile-cache/pnpm.js -v
    8.38 ± 0.24 times faster than node ./dist-base/pnpm.js -v

@merceyz merceyz marked this pull request as ready for review November 17, 2024 11:35
Comment thread sources/corepackUtils.ts
Comment on lines +403 to +408
// @ts-expect-error - Missing types
if (!Module.enableCompileCache) {
// Node.js segfaults when using npm@>=9.7.0 and v8-compile-cache
// $ docker run -it node:20.3.0-slim corepack npm@9.7.1 --version
// [SIGSEGV]
if (locator.name !== `npm` || semverLt(locator.reference, `9.7.0`)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can minimize the diff, to avoid cluttering git blame

Suggested change
// @ts-expect-error - Missing types
if (!Module.enableCompileCache) {
// Node.js segfaults when using npm@>=9.7.0 and v8-compile-cache
// $ docker run -it node:20.3.0-slim corepack npm@9.7.1 --version
// [SIGSEGV]
if (locator.name !== `npm` || semverLt(locator.reference, `9.7.0`)) {
// Node.js segfaults when using npm@>=9.7.0 and v8-compile-cache
// $ docker run -it node:20.3.0-slim corepack npm@9.7.1 --version
// [SIGSEGV]
if (locator.name !== `npm` || semverLt(locator.reference, `9.7.0`)) {
// @ts-expect-error - Missing types
if (!Module.enableCompileCache) {

Copy link
Copy Markdown
Member Author

@merceyz merceyz Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't benchmarked it but this would cause it to do work it doesn't have to.

Copy link
Copy Markdown
Contributor

@aduh95 aduh95 Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only for npm though, and if native support is disabled

Copy link
Copy Markdown
Member Author

@merceyz merceyz Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would always affect npm as it has to check the version first.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's what I'm saying, and if native support is indeed disabled – otherwise it would have to do the check anyway

@aduh95 aduh95 merged commit cba6905 into nodejs:main Nov 23, 2024
@merceyz merceyz deleted the merceyz/perf/node-compile-cache branch November 23, 2024 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v8-compile-cache doesn't allow to use dynamic import in yarn.config.cjs

2 participants