Skip to content

Load process and Buffer lazily with the new module registry - #8815

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

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

Conversation

@littledivy

Copy link
Copy Markdown
Member

With the new module registry, every isolate evaluates the process module (node-internal:public_process or legacy_process) and node:buffer before the main module, even for a Worker that never touches them. nodejs_compat_v2 is on by date, so this happens with or without nodejs_compat, and it makes each new isolate start about 1.3 ms slower than with the legacy registry, which loads both on first access. The pre-resolve was added so that a first access during top-level evaluation wouldn't drain the microtask queue, but require() no longer drains while a module is evaluating, so that case is already covered.

In this patch the pre-resolve is removed, and the process and Buffer getters resolve their module inside a ModuleEvaluationScope when the new registry is in use. A first access during a request still doesn't run the Worker's pending microtasks, as it doesn't today, and both modules are synchronous built-ins that settle without a drain. The legacy registry path is unchanged.

process.emitWarning() emits on the process object from a microtask, and that object is only set once the process module has loaded. A warning raised before then, such as the MaxListenersExceededWarning from a node:events emitter, threw a TypeError from the microtask. This already happens with the legacy registry, and loading lazily would have exposed it with the new one too, so the first commit drops the warning when there is no process object yet. Nothing can be listening for it then.

The new new-module-registry-lazy-globals-test covers a first access in a request and during top-level evaluation, and the warning with both registries. The request case fails without the scope, and the warning cases fail without the emitWarning() fix.

Worker, each extra isolate legacy registry before after speedup
empty, no nodejs_compat 2.46 ms 3.74 ms 2.43 ms 1.54x
empty, nodejs_compat 3.74 ms 4.76 ms 3.75 ms 1.27x

CPU time to start the Worker in a fresh isolate, median of 400 workerd bench startup samples per cell, both binaries interleaved, -c opt on a 4-CPU Linux x86-64 machine. A Worker that imports every node: built-in is unchanged (15.4 vs 15.1 ms), and so is the first isolate of a process.

Part of #8365.

@littledivy
littledivy requested review from a team as code owners November 23, 2026 02:27

@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 853ea980.

This review, of ef092db8

💬 Approved with comments — The change is correct and safe. The one WARNING is a stale developer reference document that still describes the removed process/Buffer warm-up, and it has no runtime impact.

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 #8815.

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] docs/reference/detail/new-module-registry.md:829-832 still describes the nodejs_compat_v2 process/buffer warm-up in worker.c++, which this pull request removes. The flag is now checked in the lazy process/Buffer getters in api/global-scope.c++.
- **File:** docs/reference/detail/new-module-registry.md:831

📝 Documentation: ⚠️ 1 warning

The new module registry reference still describes the removed process/Buffer warm-up.

Recommendation: 📝 Minor Comments

  • [WARNING] docs/reference/detail/new-module-registry.md:829-832 still describes the nodejs_compat_v2 process/buffer warm-up in worker.c++, which this pull request removes. The flag is now checked in the lazy process/Buffer getters in api/global-scope.c++. (docs/reference/detail/new-module-registry.md:831)
View full review

Lines 829-832 of docs/reference/detail/new-module-registry.md still list "the nodejs_compat_v2 process/buffer warm-up" among the worker.c++ flag-check sites. This pull request removes that warm-up from worker.c++. The process and Buffer getters in api/global-scope.c++ now check the flag themselves when they resolve their module lazily. Update the sentence so it describes those getters, in line with the repository's rule that documents describe the current state of the code.


✅ All other checks passed

🔒 Security: ✅ LGTM

No security-relevant changes.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


📋 Code Quality: ✅ LGTM

Both getters resolve their module inside a ModuleEvaluationScope only when the new registry is enabled, and they re-check the cached value after resolving. featureFlags in worker.c++ is still used by the timers block, so removing the pre-resolve leaves no dead variable. The optional chaining in emitWarning() drops a warning only when no process object exists yet, so nothing can be listening for it.


⚡ Performance: ✅ LGTM

Loading these modules lazily removes per-isolate startup work and adds no new cost on the hot path.


Reviewed commit: ef092db8

@littledivy
littledivy force-pushed the littledivy/2026-11-21-startup-new-registry branch from ef092db to 853ea98 Compare November 23, 2026 02:30

@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 c821c4a6.

This review, of 853ea980

✅ Approved — The earlier documentation warning is fixed, and I found nothing else to raise. Loading these modules lazily is correct and limited to the new module registry.

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 getters resolve fixed built-in specifiers that no user input reaches. No access-control or isolation check changes.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


📋 Code Quality: ✅ LGTM

Both getters open a ModuleEvaluationScope only when the new registry is enabled, and they check the cached value again after resolving. Removing the warm-up in worker.c++ leaves no unused variable. The emitWarning() guard drops a warning only when no process object exists yet.


⚡ Performance: ✅ LGTM

Removing the warm-up takes work off every isolate's startup. A first access adds no cost on the hot path.


📝 Documentation: ✅ LGTM

docs/reference/detail/new-module-registry.md now lists the lazy process and Buffer getters in global-scope.c++ as flag-check sites, which matches the code.


Reviewed commit: 853ea980

Comment thread src/workerd/api/global-scope.c++ Resolved
Comment thread src/workerd/api/global-scope.c++ Resolved
emitWarning() emits the warning on the process object from a microtask,
but the process object is only set once its module has loaded, on first
use. A warning raised before then, such as the MaxListenersExceededWarning
from a node:events emitter that has too many listeners, threw
"Cannot read properties of undefined (reading 'emit')" from the
microtask. This happens with the legacy module registry today. The new
registry loads the process module before every Worker, which hides it.

In this patch the warning is dropped when the process object isn't set.
Nothing can have a 'warning' listener before then, so emitting it would
have had no effect.

Compatibility: no flag. The only change is that a microtask that threw a
TypeError now does nothing, which is what the same warning does once the
process module has loaded and has no 'warning' listener.
With the new module registry, every isolate evaluated the process module
(node-internal:public_process or legacy_process) and node:buffer before the
main module, even for a Worker that never touches them. nodejs_compat_v2 is
on by date, so this ran with or without nodejs_compat and cost about 1.3 ms
of startup per isolate. The legacy registry loads both on first access.

The pre-resolve was there so that a first access during top-level
evaluation wouldn't drain the microtask queue. require() no longer drains
while a module is evaluating, so that case is covered without it.

In this patch the pre-resolve is removed, and the process and Buffer
getters resolve their module inside a ModuleEvaluationScope when the new
registry is in use. A first access during a request therefore still
doesn't run the Worker's pending microtasks, as it doesn't today. Both
modules are synchronous built-ins, so they settle without a drain. The
legacy registry path is unchanged.

Compatibility: no flag. The process and buffer modules now evaluate on
first use instead of before the main module, as they do with the legacy
registry. Microtask behaviour on first access is unchanged.
The process and Buffer globals load their modules on first access with the
new module registry. A Worker that queues a microtask and then reads either
global for the first time must not see the microtask run inside the
getter, in a request or during top-level evaluation. A built-in that emits
a process warning before the process module has loaded must not fail.

In this patch new-module-registry-lazy-globals-test covers the first access
in a request and at top level, and checks the globals are the node:process
and node:buffer exports. The request case fails if the getters resolve
without a ModuleEvaluationScope. It also raises a MaxListenersExceededWarning
from node:events with each registry before anything loads the process
module, which fails without the emitWarning() fix.
@littledivy
littledivy force-pushed the littledivy/2026-11-21-startup-new-registry branch from 853ea98 to c821c4a Compare November 23, 2026 16:06

@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 — No findings. process and Buffer now load lazily only under the new module registry, and the emitWarning() guard drops a warning only when no process object exists yet.

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 getters resolve fixed built-in specifiers, and no user input reaches them. No access-control or isolation check changes.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


📋 Code Quality: ✅ LGTM

  • global-scope.c++: getBuffer() and getProcess() open a ModuleEvaluationScope only when isNewModuleRegistryEnabled(FeatureFlags::get(js)) is true. That leaves the legacy path unchanged. Both getters check the cached value again after resolving.
  • worker.c++: removing the pre-resolve block leaves no unused variable.
  • internal_process.ts: the guard drops a warning only when the process object is not set yet.
  • Tests: they cover a first access during a request and during top-level evaluation, and the warning case with both registries.

⚡ Performance: ✅ LGTM

Removing the eager pre-resolve takes work off every isolate's startup. A first access adds no cost on the hot path.


📝 Documentation: ✅ LGTM

docs/reference/detail/new-module-registry.md matches the code.


Reviewed commit: c821c4a6

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.73%. Comparing base (5f71182) to head (c821c4a).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8815      +/-   ##
==========================================
- Coverage   68.00%   67.73%   -0.28%     
==========================================
  Files         847      847              
  Lines      219245   215043    -4202     
  Branches    32592    32590       -2     
==========================================
- Hits       149097   145655    -3442     
+ Misses      50903    50145     -758     
+ Partials    19245    19243       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@littledivy
littledivy merged commit 3cea65e into main Nov 23, 2026
23 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-21-startup-new-registry branch November 23, 2026 17:43
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.

3 participants