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
- build: build v8 with -fvisibility=hidden on macOS V8 should be built with -fvisibility=hidden, otherwise the resulting binary would contain unnecessary symbols. In particular, on macOS, this leads to... — committed to nodejs/node by joyeecheung 2 years ago
- build: build v8 with -fvisibility=hidden on macOS V8 should be built with -fvisibility=hidden, otherwise the resulting binary would contain unnecessary symbols. In particular, on macOS, this leads to... — committed to nodejs/node by joyeecheung 2 years ago
- build: build v8 with -fvisibility=hidden on macOS V8 should be built with -fvisibility=hidden, otherwise the resulting binary would contain unnecessary symbols. In particular, on macOS, this leads to... — committed to nodejs/node by joyeecheung 2 years ago
- build: build v8 with -fvisibility=hidden on macOS V8 should be built with -fvisibility=hidden, otherwise the resulting binary would contain unnecessary symbols. In particular, on macOS, this leads to... — committed to nodejs/node by joyeecheung 2 years ago
- build: build v8 with -fvisibility=hidden on macOS V8 should be built with -fvisibility=hidden, otherwise the resulting binary would contain unnecessary symbols. In particular, on macOS, this leads to... — committed to nodejs/node by joyeecheung 2 years ago
- build: build v8 with -fvisibility=hidden on macOS V8 should be built with -fvisibility=hidden, otherwise the resulting binary would contain unnecessary symbols. In particular, on macOS, this leads to... — committed to nodejs/node by joyeecheung 2 years ago
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).
Also, it produces a smaller binary
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:
From V8’s fork
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
--versionon same setup:linux/i5
macos/m3max
you can use this shell script to build node-ci on macos or linux:
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.
if there are any other tests i can run over coming days let me know @joyeecheung.
Dumping the v8_features.json:
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
You could be talking about
process.configbut that is pretty high-level, the compiler options are not embedded because er, well they can be pretty verbose and big 😉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
That’s consistent with my guess that this is a macOS problem. (Meaning a problem that occurs on macOS with node.)
configure --without-sslLook at InitializeOncePerProcess:
@billywhizz
Intriguing.
@just-js mentioned that it might be a binary size threshold, just to comment that I previously tried to build with
--without-intlto 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