Skip to content

Don't serialize a module code cache on its first compile - #8424

Merged
littledivy merged 3 commits into
mainfrom
littledivy/2026-11-13-new-registry-startup
Nov 20, 2026
Merged

littledivy merged 3 commits into
mainfrom
littledivy/2026-11-13-new-registry-startup

Conversation

@littledivy

@littledivy littledivy commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

With new_module_registry, a module that has no embedded bytecode and no process-wide code cache store keeps its bytecode in a slot on its shared definition. On a miss, SharedModuleCompilationCache::compile() called CreateCodeCache right after compiling, so every Worker bundle module was serialized at startup. The slot is only read when another isolate compiles the same definition. workerd never installs a store and builds one registry per Worker, so it never reads the slot. Those Workers paid about 10 ms per MB of JS at startup for bytecode nobody reads. The legacy registry has no slot and doesn't pay this. This is the part of the startup cost in #8365 that grows with the size of the evaluated JS.

In this patch the first compile from source only marks the slot. The next isolate that misses generates the bytecode, and later ones consume it as before. When several isolates share a registry, the second one now pays for serialization instead of the first. Snapshot preparation still neither generates nor marks. The compile cache flow in docs/reference/detail/new-module-registry.md is updated to match.

new_module_registry off main this patch
1 MB module, nodejs_compat 129 ms 140 ms 131 ms
300 modules of 8 KB 250 ms 274 ms 251 ms

Median process CPU of workerd test, -c opt, interleaved runs on a 4-CPU Linux executor.

@littledivy
littledivy requested review from a team as code owners November 13, 2026 03:48

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 828f0ab8.

This review, of e8f2b010

💬 Approved with comments — The logic change is correct and the tests cover it. One WARNING: the developer reference doc for the new module registry still describes the first isolate generating bytecode.

Findings: 1 warning

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8424.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Documentation] The Compile Cache Flow in docs/reference/detail/new-module-registry.md still describes the first isolate serializing bytecode, which this change no longer does.
- **File:** docs/reference/detail/new-module-registry.md:247

📝 Documentation: ⚠️ 1 warning

The compile cache flow in the registry reference doc is out of date

Recommendation: 📝 Minor Comments

  • [WARNING] The Compile Cache Flow in docs/reference/detail/new-module-registry.md still describes the first isolate serializing bytecode, which this change no longer does. (docs/reference/detail/new-module-registry.md:247)
View full review
  • docs/reference/detail/new-module-registry.md:247-252: the "Compile Cache Flow" still says the first isolate calls CreateCodeCache and stores the bytecode. With this change, the first compile from source only marks the slot. The next isolate that misses generates the bytecode, and the isolates after that consume it.

✅ All other checks passed

🔒 Security: ✅ LGTM

The change adds no new input, trust boundary or secret handling. Cached bytecode is still checked for compatibility and cleared if V8 rejects it.


📖 AGENTS.md: ✅ LGTM

Nothing in this change needs an AGENTS.md update.


📋 Code Quality: ✅ LGTM

Deferring the serialization is easy to follow. Rejected bytecode is still regenerated right away, because compiled is already set.


⚡ Performance: ✅ LGTM

Serialization moves from the first isolate to the second one that misses. When several isolates share a registry, the second isolate does one extra source compile before bytecode is available. That trade-off is deliberate and described in the pull request.


Reviewed commit: e8f2b010

@littledivy
littledivy force-pushed the littledivy/2026-11-13-new-registry-startup branch from e8f2b01 to 828f0ab Compare November 13, 2026 03:52

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 500c70aa.

This review, of 828f0ab8

💬 Approved with comments — The earlier documentation WARNING is fixed and the logic change is still correct. The only remaining item is an optional Markdown line-wrap SUGGESTION.

Findings: 1 suggestion (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📝 Documentation: 💬 1 suggestion

The stale Compile Cache Flow is fixed; one optional line-wrap nit remains.

Recommendation: 💬 Optional Nits

  • [SUGGESTION] Rewrap the overlong line in the rewritten paragraph to match the surrounding Markdown formatting. (docs/reference/detail/new-module-registry.md:220)
View full review

The Compile Cache Flow and the overview now match the code. One rewrapped paragraph at docs/reference/detail/new-module-registry.md:220 is much longer than the lines around it.


✅ All other checks passed

🔒 Security: ✅ LGTM

The new compiled flag is read and written under the exclusive lock. The change adds no new input, trust boundary or secret handling. V8 still checks cached bytecode for compatibility, and rejected bytecode is still cleared.


📖 AGENTS.md: ✅ LGTM

Nothing in this change needs an AGENTS.md update.


📋 Code Quality: ✅ LGTM

Deferring serialization to the second compile is easy to follow. Rejected bytecode is still regenerated right away, because compiled is already set.


⚡ Performance: ✅ LGTM

Serialization moves from the first isolate to the second one that misses. A Worker whose module is compiled by a single isolate no longer pays for it. The pull request describes this trade-off and intends it.


Reviewed commit: 828f0ab8

@littledivy
littledivy force-pushed the littledivy/2026-11-13-new-registry-startup branch from 828f0ab to 500c70a Compare November 13, 2026 03:57

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 731b952e.

This review, of 500c70aa

✅ Approved — This revision fixes the earlier formatting note, and no issues remain. Deferring serialization to the second compile is correct, and the tests cover it.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change adds no new input, trust boundary or secret handling. V8 still checks cached bytecode for compatibility, and rejected bytecode is still cleared.


📖 AGENTS.md: ✅ LGTM

Nothing in this change needs an AGENTS.md update.


📋 Code Quality: ✅ LGTM

The compiled flag is read and written under lockExclusive(). A slot that holds rejected data has always been compiled before, so it is still regenerated right away. When two isolates race on the first compile, the worst case is one extra serialization by the second isolate. The tests cover each step: mark on the first compile, generate on the second, hit on the third, replacement after rejection, snapshot preparation, and the hints path.


⚡ Performance: ✅ LGTM

Serialization moves from the first isolate to the second one that misses. A module compiled by only one isolate no longer pays for it. The pull request description explains this trade-off.


📝 Documentation: ✅ LGTM

The rewritten paragraph now wraps like the text around it. The Compile Cache Flow section and the overview match the code.


Reviewed commit: 500c70aa

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 12c3998f.

This review, of 731b952e

✅ Approved — This revision fixes the earlier formatting note, and no issues remain. Deferring serialization to the second compile is correct, and the tests cover it.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change adds no new input, trust boundary or secret handling. V8 still checks cached bytecode for compatibility, and rejected bytecode is still cleared.


📖 AGENTS.md: ✅ LGTM

Nothing in this change needs an AGENTS.md update.


📋 Code Quality: ✅ LGTM

The compiled flag is read and written under lockExclusive(). A slot that holds rejected data has always been compiled before, so it is still regenerated right away. When two isolates race on the first compile, the worst case is one extra serialization by the second isolate. The tests cover each step: mark on the first compile, generate on the second, hit on the third, replacement after rejection, snapshot preparation, and the hints path.


⚡ Performance: ✅ LGTM

Serialization moves from the first isolate to the second one that misses. A module compiled by only one isolate no longer pays for it. The pull request description explains this trade-off.


📝 Documentation: ✅ LGTM

The rewritten paragraph now wraps like the text around it. The Compile Cache Flow section and the overview match the code.


Reviewed commit: 731b952e — same changes as 500c70aa, so this is that commit's review (no new review was run; line numbers are as of 500c70aa)

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of b1021d11.

This review, of 12c3998f

✅ Approved — This revision fixes the earlier formatting note, and no issues remain. Deferring serialization to the second compile is correct, and the tests cover it.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change adds no new input, trust boundary or secret handling. V8 still checks cached bytecode for compatibility, and rejected bytecode is still cleared.


📖 AGENTS.md: ✅ LGTM

Nothing in this change needs an AGENTS.md update.


📋 Code Quality: ✅ LGTM

The compiled flag is read and written under lockExclusive(). A slot that holds rejected data has always been compiled before, so it is still regenerated right away. When two isolates race on the first compile, the worst case is one extra serialization by the second isolate. The tests cover each step: mark on the first compile, generate on the second, hit on the third, replacement after rejection, snapshot preparation, and the hints path.


⚡ Performance: ✅ LGTM

Serialization moves from the first isolate to the second one that misses. A module compiled by only one isolate no longer pays for it. The pull request description explains this trade-off.


📝 Documentation: ✅ LGTM

The rewritten paragraph now wraps like the text around it. The Compile Cache Flow section and the overview match the code.


Reviewed commit: 12c3998f — same changes as 500c70aa, so this is that commit's review (no new review was run; line numbers are as of 500c70aa)

With new_module_registry, a module that has no embedded bytecode and no
process-wide code cache store keeps its bytecode in a slot on its shared
definition. On a miss, SharedModuleCompilationCache::compile() called
CreateCodeCache right after compiling, so every Worker bundle module was
serialized at startup. workerd never installs a store, and the slot is
only read when another isolate compiles the same definition, so a
one-isolate Worker paid about 10 ms per MB of JS at startup for bytecode
nobody reads. The legacy registry has no slot and doesn't pay this. This
is the part of the startup cost in #8365 that grows with the size of the
evaluated JS.

In this patch the first compile from source only marks the slot. The
next isolate that misses generates the bytecode, and later ones consume
it as before. Snapshot preparation still neither generates nor marks.

                               new_module_registry off   main   this patch
  1 MB module, nodejs_compat           129 ms           140 ms     131 ms
  300 modules of 8 KB                  250 ms           274 ms     251 ms

Median process CPU of `workerd test`, -c opt, interleaved runs on a
4-CPU Linux executor.
The first isolate to compile a module from source only marks its shared
slot. This test checks the whole sequence across three isolates: the
first generates nothing, the second generates the bytecode, and the
third consumes it.
The new module registry reference said the first isolate to compile a
module generates its bytecode. In this patch the cross-isolate cache
section and the compile cache flow describe the second isolate
generating it and later isolates consuming it.
@littledivy
littledivy force-pushed the littledivy/2026-11-13-new-registry-startup branch from 12c3998 to b1021d1 Compare November 19, 2026 07:58

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — This revision fixes the earlier formatting note, and no issues remain. Deferring serialization to the second compile is correct, and the tests cover it.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change adds no new input, trust boundary or secret handling. V8 still checks cached bytecode for compatibility, and rejected bytecode is still cleared.


📖 AGENTS.md: ✅ LGTM

Nothing in this change needs an AGENTS.md update.


📋 Code Quality: ✅ LGTM

The compiled flag is read and written under lockExclusive(). A slot that holds rejected data has always been compiled before, so it is still regenerated right away. When two isolates race on the first compile, the worst case is one extra serialization by the second isolate. The tests cover each step: mark on the first compile, generate on the second, hit on the third, replacement after rejection, snapshot preparation, and the hints path.


⚡ Performance: ✅ LGTM

Serialization moves from the first isolate to the second one that misses. A module compiled by only one isolate no longer pays for it. The pull request description explains this trade-off.


📝 Documentation: ✅ LGTM

The rewritten paragraph now wraps like the text around it. The Compile Cache Flow section and the overview match the code.


Reviewed commit: b1021d11 — same changes as 500c70aa, so this is that commit's review (no new review was run; line numbers are as of 500c70aa)

@littledivy
littledivy merged commit 79f5ba8 into main Nov 20, 2026
23 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-13-new-registry-startup branch November 20, 2026 17:48
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.

2 participants