performance: Potential startup regression since on Node.js >= 22

Hi folks,

I’ve been investigating some areas of regression on Node.js 22 and apparently, the Node.js startup has degraded. I’ve run a pretty simple benchmark

➜  nodejs-internal-benchmark git:(main) ✗ hyperfine --warmup 20 -- './node18 semicolon.js'
Benchmark 1: ./node18 semicolon.js
  Time (mean ± σ):      19.5 ms ±   0.3 ms    [User: 16.1 ms, System: 2.2 ms]
  Range (min … max):    18.6 ms …  21.0 ms    142 runs

➜  nodejs-internal-benchmark git:(main) ✗ hyperfine --warmup 20 -- './node20 semicolon.js'
Benchmark 1: ./node20 semicolon.js
  Time (mean ± σ):      19.6 ms ±   0.3 ms    [User: 16.3 ms, System: 2.0 ms]
  Range (min … max):    18.9 ms …  20.6 ms    142 runs

➜  nodejs-internal-benchmark git:(main) ✗ hyperfine --warmup 20 -- './node22 semicolon.js'
Benchmark 1: ./node22 semicolon.js
  Time (mean ± σ):      27.5 ms ±   0.4 ms    [User: 23.9 ms, System: 2.1 ms]
  Range (min … max):    26.7 ms …  28.5 ms    102 runs

➜  nodejs-internal-benchmark git:(main) ✗ hyperfine --warmup 20 -- './node23 semicolon.js'
Benchmark 1: ./node23 semicolon.js
  Time (mean ± σ):      30.5 ms ±   0.4 ms    [User: 26.8 ms, System: 2.4 ms]
  Range (min … max):    29.8 ms …  31.6 ms    92 runs

Where the semicolon is just a js file with ;

Although this regression isn’t confirmed by our benchmarks:

misc/startup-core.js n=30 mode='process' script='test/fixtures/semicolon'                    ***      5.82 %       ±1.01% ±1.34% ±1.74%

Please note, the hyperfine benchmarks were run on a local macOS machine – I couldn’t run it in a dedicated one as I’m travelling and I don’t have access via SSH to my machines and the nodejs benchmark (misc/startup-core.js) was run in a dedicated aws machine

Does someone has an idea? cc @joyeecheung

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 58 (33 by maintainers)

Commits related to this issue

Most upvoted comments

I think I found out what’s happening, which was along the lines of my theory in https://github.com/nodejs/performance/issues/180#issuecomment-2494974471 - in the v8 upgrade, there were a lot of templated instantiations added, leading to an explosion of weak symbols that are resolved at run time (I think we weren’t excluding them very well in the first place, but the explosion of weak symbols from V8’s use of templates made the problem more obvious), and then it continue to regress as V8 adds, er, more templated functions…

It’s also easy to understand why V8’s fork is free from this regression - their builds applies -fvisibility=hidden properly.

Fix in: https://github.com/nodejs/node/pull/56275 - locally the startup time is slightly faster than 20.x (as we are properly excluding the symbols now).

$ hyperfine "./node-c4aa34aa --version" "out/Release/node --version" "/Users/joyee/.nvs/node/20.18.0/arm64/bin/node --version" --warmup 10
Benchmark 1: ./node-c4aa34aa --version
  Time (mean ± σ):      28.5 ms ±   1.5 ms    [User: 25.8 ms, System: 1.9 ms]
  Range (min … max):    27.5 ms …  34.9 ms    89 runs

Benchmark 2: out/Release/node --version
  Time (mean ± σ):      12.1 ms ±   1.3 ms    [User: 8.8 ms, System: 1.9 ms]
  Range (min … max):    11.3 ms …  18.2 ms    179 runs

Benchmark 3: /Users/joyee/.nvs/node/20.18.0/arm64/bin/node --version
  Time (mean ± σ):      14.2 ms ±   1.0 ms    [User: 12.1 ms, System: 1.7 ms]
  Range (min … max):    13.5 ms …  20.0 ms    158 runs

Summary
  'out/Release/node --version' ran
    1.17 ± 0.15 times faster than '/Users/joyee/.nvs/node/20.18.0/arm64/bin/node --version'
    2.35 ± 0.27 times faster than './node-c4aa34aa --version'

Also, it produces a smaller binary

$ ls -lah out/Release/node
-rwxr-xr-x  1 joyee  staff   108M Dec 16 22:39 out/Release/node
$ ls -lah ./node-c4aa34aa
-rwxr-xr-x  1 joyee  staff   118M Dec  5 12:39 ./node-c4aa34aa

Following up my theory in https://github.com/nodejs/performance/issues/180#issuecomment-2494974471 I built V8’s Node.js fork and the benchmark result is…amusing:

From Node.js main branch:

❯ hyperfine -i "out/Release/node --version"
Benchmark 1: out/Release/node --version
  Time (mean ± σ):      26.3 ms ±   1.4 ms    [User: 23.7 ms, System: 1.2 ms]
  Range (min … max):    25.3 ms …  35.0 ms    80 runs

From V8’s fork

❯ hyperfine -i "out/Release/node --version"
Benchmark 1: out/Release/node --version
  Time (mean ± σ):       8.4 ms ±   0.4 ms    [User: 7.3 ms, System: 0.7 ms]
  Range (min … max):     7.9 ms …  11.3 ms    270 runs

I feel that we should probably take a look at their configuration (granted, it’s gn, but something might have been missing in the gyp -> gn porting)

if i just do --version on same setup:

linux/i5

nice -n 20 taskset --cpu-list 0  hyperfine --warmup 100 --runs 200 "./node-ci --version" "./node23 --version" 2>/dev/null
Benchmark 1: ./node-ci --version
  Time (mean ± σ):       3.1 ms ±   0.1 ms    [User: 1.2 ms, System: 2.0 ms]
  Range (min … max):     3.0 ms …   3.9 ms    200 runs
 
Benchmark 2: ./node23 --version
  Time (mean ± σ):       3.8 ms ±   0.1 ms    [User: 1.7 ms, System: 2.1 ms]
  Range (min … max):     3.6 ms …   4.5 ms    200 runs
 
Summary
  './node-ci --version' ran
    1.21 ± 0.06 times faster than './node23 --version'

macos/m3max

hyperfine --warmup 100 --runs 200 "./node23 --version" "./node-ci --version" 2>/dev/null
Benchmark 1: ./node23 --version
  Time (mean ± σ):      30.9 ms ±   0.6 ms    [User: 23.0 ms, System: 1.9 ms]
  Range (min … max):    29.4 ms …  35.7 ms    200 runs
 
Benchmark 2: ./node-ci --version
  Time (mean ± σ):       9.3 ms ±   0.1 ms    [User: 7.2 ms, System: 1.2 ms]
  Range (min … max):     9.1 ms …   9.9 ms    200 runs
 
Summary
  ./node-ci --version ran
    3.31 ± 0.07 times faster than ./node23 --version

you can use this shell script to build node-ci on macos or linux:

mkdir google
cd google
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
export PATH=$(pwd)/depot_tools:$PATH
git clone https://chromium.googlesource.com/v8/node-ci
gclient config https://chromium.googlesource.com/v8/node-ci --unmanaged
gclient sync
CPUS=$(nproc --all)
cd node-ci
make clean
make deps
make -j $CPUS node

i did a clean build on the machines i have here. macos M3Max is >10x quicker to do a clean build on 16 threads versus ubuntu 22.04 on 8th gen core i5 / 8 threads.

machine time threads cpuseconds
M3Pro 5m 45s 16 5520
i5 60m 8 28800

if there are any other tests i can run over coming days let me know @joyeecheung.

Dumping the v8_features.json:

{
  "v8_deprecation_warnings": true,
  "v8_enable_31bit_smis_on_64bit_arch": false,
  "v8_enable_conservative_stack_scanning": false,
  "v8_enable_direct_handle": false,
  "v8_enable_extensible_ro_snapshot": true,
  "v8_enable_gdbjit": false,
  "v8_enable_hugepage": false,
  "v8_enable_i18n_support": true,
  "v8_enable_javascript_promise_hooks": true,
  "v8_enable_lite_mode": false,
  "v8_enable_map_packing": false,
  "v8_enable_object_print": false,
  "v8_enable_pointer_compression": true,
  "v8_enable_pointer_compression_shared_cage": true,
  "v8_enable_sandbox": false,
  "v8_enable_shared_ro_heap": true,
  "v8_enable_short_builtin_calls": true,
  "v8_enable_v8_checks": false,
  "v8_enable_webassembly": true,
  "v8_enable_zone_compression": false,
  "v8_imminent_deprecation_warnings": false,
  "v8_optimized_debug": true,
  "v8_random_seed": "314159265",
  "v8_use_perfetto": false,
  "v8_use_siphash": false
}

TIL they enabled pointer compression in their CI (I thought they were consistent with the main branch). Maybe that’s the key because I did notice a particularly pricy mprotect call in the dtruss output. But I don’t know the right way to disable it yet (or it might not build without pointer compression and need to be rolled back a bunch of commits).

Also V8’s fork uses their own llvm/clang/libc++ and all that (chromium, you know), so that might be important as well…

We don’t build Node.js with gn, but with gyp. I don’t really see any change to compiler options in https://github.com/nodejs/node/pull/49639 Relevant diff: https://github.com/nodejs/node/compare/162a0652ab1a2198dc4eadee4134cbf84710a734...86cb23d09f4cc4f73dca00effb69b83eb7548003

wondering if it would be possible to embed these options in the node runtime going forward so they could easily be dumped with a cli flag at least?

You could be talking about process.config but that is pretty high-level, the compiler options are not embedded because er, well they can be pretty verbose and big 😉

tracing in any serious way on macos seems to require booting into recovery mode afaict? i can see if that sheds any light

Yes, that’s dtrace/dtruss, I tried to use them but they didn’t really reveal anything interesting to me (for something as short-lived as ~20ms, I could sample it at 5000Hz at most and didn’t get much samples anyways, and didn’t find anything particuarly interesting in dtruss output, but I might not be using it correctly/using a buggy version of it).

I tried to profile the empty main function binary on macOS - the binary with an empty main function already takes ~25ms on my laptop, meanwhile v23.x running an empty script takes 32ms, so I was trying to figure out what it’s trying to do in the 25ms outside of the main function. Unfortunately neither Instruments, dtrace nor samply gave me adequate information because it seems neither of these work well when you are trying to profile what happens outside of the main function…

I do have a theory, though, since it isn’t actually doing much outside of the main function (not enough for a sampling profiler to pick up, anyway), I suspect it could be some compilation option changed in the V8 11.3 -> 11.8 upgrade that turned off some optimization or introduced something that would make the loading of the executable more costly. That could explain why this is macOS only because the compiler commands are typically generated in a platform-specific manner. I haven’t looked into the actual compiler commands or how they differed along the years, though.

No worries, maybe I wasn’t clear enough about my findings. Also, I am not 100% sure about the theory unless someone reproduce on their machine (I was able to reproduce this on two different generation of MacBooks, though). Thanks for helping out and looking into this!

Have you tried making the main function a no-op and benchmark again, like what I did in https://github.com/nodejs/performance/issues/180#issuecomment-2442160544? My testing showed that whatever that regressed the startup mostly happened before/after the main function is run. After the V8 11.3 -> 11.8 upgrade the Node.js binary with a no-op main function regressed for about 8ms (or before the V8 upgrade it was 1.6x faster). I am a bit surprised that we are still focusing on what happens inside the main function after 15 days of that comment…

Also on Snapshot::Initialize: if you look at the flame graph you will notice that in recent versions of Node.js no internal JS code is compiled at all when executing a CJS script, that’s because we moved the internal JS code compilation into build time and serialized the bytecode into the snapshot. You may see a percentage increase in snapshot initialization but that was just because the code are deserialized instead of being compiled now, which is what makes the startup overall faster on other platforms, so it’s compensated with all the V8 compile/parse frames disappearing from the flamegraph. You could check if snapshot played a difference at all by using --without-node-snapshot runtime flag and my previous showed that it didn’t matter. But then again, my testing also showed that whatever happened in the main function didn’t matter because the regression persists even if you make the main function a no-op…

Hi, I’ll try to catch up when I get some time. I don’t have much bandwidth right now.

@billywhizz

Thanks for running these tests.

It suggests to me that openssl is affected by the issue but is not the cause of the issue.

So I am back at my theory: this is a case where dynamic linking and rebasing is expensive under macOS for some reason.

@richardlau

That’s very helpful. We can use this to try to see if openssl is to blame.

@billywhizz

but if we stick to the final release for each version then i think we can say clearly on linux/x64 on my i5 laptop performance is steadily improving, while on macos/arm64, as shown above, it seems to be getting worse.

That’s consistent with my guess that this is a macOS problem. (Meaning a problem that occurs on macOS with node.)

Is there a quick way to build node with OpenSSL disabled? (Evidently, such a node version might be dysfunctional.)

configure --without-ssl

Look at InitializeOncePerProcess:

Screenshot 2024-11-10 at 6 29 54 PM Screenshot 2024-11-10 at 6 29 44 PM

@billywhizz

Intriguing.

@just-js mentioned that it might be a binary size threshold, just to comment that I previously tried to build with --without-intl to shrink the size by ~30MB and it didn’t seem to make a difference (though of course others can try to see if that reproduce to make sure that’s not a false elimination too)

I cannot reproduce on a Linux server though can reproduce on an arm64 macbook. There has been a regression in the range of 0522ac086c...ed49722a8a, most likely due to the v8 11.8 upgrade https://github.com/nodejs/node/pull/49639