src: compile code eagerly in snapshot builder
By default V8 only compiles the top-level function and skips code generation for inner functions - that would only be done when those inner functions are invoked. Since builtins are compiled as wrapped functions, most functions that look visually top-level are not actually included in the built-in code cache. For most of the builtins this is not too bad because usually only a subset of all builtin functions are needed by a particular application and including all their code in the binary would incur an unnecessary size overhead. But there is also a subset of more commonly used builtins and it would be better to include the inner functions in the built-in code cache because they are more universally used by most applications.
This patch changes the compilation strategy to eager compilation (including inner functions) for the following scripts:
- Primordials (internal/per_context/*), in all situations.
- Bootstrap scripts (internal/bootstrap/) and main scripts (internal/main/), when being compiled for built-in code cache.
- Any scripts loaded during built-in snapshot generation.
We can't compile the code eagerly during snapshot generation and include them into the V8 snapshot itself just now because we need to start the inspector before context deserialization for coverage collection to work. So leave that as a TODO.
With this patch the binary size increases by about 666KB (~0.6% increase) in return the worker startup can be 18-19% faster.
Review requested:
- [ ] @nodejs/startup
Just noticed that we can't compile the code eagerly during snapshot creation just yet because that requires refactoring of the V8ProfilerConnection classes. Added a TODO instead. The accompanying code cache is still compiled eagerly.
New numbers (only workers are more significantly impacted for now):
confidence improvement accuracy (*) (**) (***)
misc/startup-cli-version.js count=30 cli='deps/corepack/dist/corepack.js' -0.40 % ±0.98% ±1.30% ±1.69%
misc/startup-cli-version.js count=30 cli='deps/npm/bin/npm-cli.js' -0.15 % ±0.74% ±0.99% ±1.29%
misc/startup-cli-version.js count=30 cli='deps/npm/bin/npx-cli.js' -0.18 % ±1.02% ±1.36% ±1.77%
misc/startup-cli-version.js count=30 cli='tools/node_modules/eslint/bin/eslint.js' -0.63 % ±0.85% ±1.13% ±1.47%
misc/startup.js count=30 mode='process' script='benchmark/fixtures/require-builtins' 1.11 % ±2.52% ±3.37% ±4.42%
misc/startup.js count=30 mode='process' script='test/fixtures/semicolon' 0.29 % ±2.07% ±2.76% ±3.59%
misc/startup.js count=30 mode='worker' script='benchmark/fixtures/require-builtins' *** 18.46 % ±3.94% ±5.27% ±6.94%
misc/startup.js count=30 mode='worker' script='test/fixtures/semicolon' *** 19.29 % ±4.84% ±6.47% ±8.46%
CI: https://ci.nodejs.org/job/node-test-pull-request/57109/
CI: https://ci.nodejs.org/job/node-test-pull-request/57110/
CI: https://ci.nodejs.org/job/node-test-pull-request/57150/
CI: https://ci.nodejs.org/job/node-test-pull-request/57183/
CI: https://ci.nodejs.org/job/node-test-pull-request/57208/
Landed in 3e57b93963ffe285e14faa3926e83f9633c98e66