src: let embedders supply a builtin code cache without a snapshot - #65352
Open
codebytere wants to merge 1 commit into
Open
src: let embedders supply a builtin code cache without a snapshot#65352codebytere wants to merge 1 commit into
codebytere wants to merge 1 commit into
Conversation
Collaborator
|
Review requested:
|
jasnell
reviewed
Aug 17, 2026
jasnell
reviewed
Aug 17, 2026
Contexts and Environments created from the built-in snapshot get the builtins' code cache from that snapshot. An embedder that bootstraps them from scratch (its own isolate and context, no EmbedderSnapshotData) has no way to provide one: every builtin the bootstrap touches, and the per-context scripts NewContext() runs, are compiled from source in every such process, and each of them then serializes a fresh cache (SaveCodeCache) that only a later worker thread would ever consume. Add a small public API for that case: - node::GenerateBuiltinCodeCache(context) compiles every builtin in a context of the right kind of isolate and returns the caches, for a build step that embeds them. - node::SetBuiltinCodeCache(entries) installs process-wide entries that every BuiltinLoader created afterwards starts with, i.e. each Environment's and the loader for the per-context scripts. Entries a snapshot provides still merge on top (RefreshCodeCache() now merges with insert_or_assign instead of assuming a single call). - ProcessInitializationFlags::kNoHarvestBuiltinCodeCache stops serializing caches for builtins compiled without one, for embedders that supply their own or never create workers. The default is unchanged because worker threads copy the harvested cache. embedtest gains --builtin-code-cache-create, --builtin-code-cache and --no-harvest-builtin-code-cache, and a test that generates a cache in one process, checks that the bootstrap and per-context scripts of another compile with it, and that a worker does or does not find a harvested cache depending on the flag. On x64 Linux embedtest's start-to-exit goes from ~64 to ~44 ms with a supplied cache; not harvesting alone saves ~5 ms on a snapshot-less start and is a no-op with the snapshot. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
codebytere
force-pushed
the
embedder/builtin-code-cache-seed
branch
from
August 17, 2026 13:25
1b99e86 to
136d2ad
Compare
jasnell
approved these changes
Aug 17, 2026
Collaborator
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65352 +/- ##
==========================================
- Coverage 90.13% 90.13% -0.01%
==========================================
Files 752 752
Lines 251568 251864 +296
Branches 47270 47355 +85
==========================================
+ Hits 226759 227012 +253
- Misses 16168 16182 +14
- Partials 8641 8670 +29
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An embedder that creates its contexts and Environments without Node's snapshot (its own isolate, no
EmbedderSnapshotData) compiles every builtin the bootstrap touches from source in each such process, and thenserializes a fresh code cache for each of them that only a later worker thread ever reads. This adds a way to hand
Node.js a cache built ahead of time, plus a flag to skip the runtime serialization.
embedtest's start-to-exit goesfrom 58 to 39 ms with a supplied cache, and the flag alone saves 6 ms;
nodeitself is unchanged.With the supplied cache all 114 functions the bootstrap and
NewContext()compile are accepted from it,internal/per_context/*included (NODE_DEBUG_NATIVE=CODE_CACHE).node::GenerateBuiltinCodeCache(context)compiles every builtin in a context made withnode::NewContext()in thekind of isolate the cache is for (same V8, flags and read-only snapshot) and returns id + bytes for a build step to
embed.
node::SetBuiltinCodeCache(entries)installs process-wide entries that everyBuiltinLoadercreated afterwardsstarts with: each Environment's, and the loader
NewContext()uses for the per-context scripts. A snapshot's entriesstill merge on top, so
RefreshCodeCache()now merges withinsert_or_assigninstead of asserting a single call.ProcessInitializationFlags::kNoHarvestBuiltinCodeCachestopsLookupAndCompile()from serializing a cache forbuiltins compiled without one. The default stays as it is because worker threads start from that harvested cache.
embedtestgets--builtin-code-cache-create <file>,--builtin-code-cache <file>and--no-harvest-builtin-code-cacheso the test drives all three through an embedder binary: it generates a cache in oneprocess, checks that another process's bootstrap and per-context scripts compile from it, and that a worker started
with and without the flag does and doesn't find a harvested cache.
Tests: the new embedding test and two cctests (
RefreshCodeCachemerges, the process cache seeds new loaders);embedding, cctest and the default suite pass.
Disclosure: the code, tests, measurements and this description were written by Claude Code, directed and reviewed by @codebytere.