Skip to content

perf: preload eligible lazy modules safely - #703

Merged
domgan-oai merged 1 commit into
octanejs:mainfrom
openai-oss-forks:fix/673-preload-eligible-lazy-modules
Aug 10, 2026
Merged

domgan-oai merged 1 commit into
octanejs:mainfrom
openai-oss-forks:fix/673-preload-eligible-lazy-modules

Conversation

@domgan-oai

@domgan-oai domgan-oai commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Start an independently reachable lazy() module loader during an existing suspended sibling's compiler-proven warm plan instead of waiting for all data dependencies to resolve.
  • Preserve real dynamic-import chunk splitting, exactly-once loader/request behavior, lazy wrapper construction, rejection semantics, default-export accessor timing, and component identity.
  • Keep intentionally dormant <Hydrate> boundaries opaque to speculative discovery; only their own activation or explicitly configured prefetch may load deferred code.

Correctness and scope

  • Reuse the existing DOM-client compiler warmChild reachability/guard proof; no compiler, server-runtime, public API, or hydration-chunk extraction changes.
  • Share the existing transactional lazy() payload initializer between speculative module discovery and actual rendering; do not execute component code, hooks, DOM, effects, or resolved module export accessors during discovery.
  • Keep module loading inert until a real eligible render path suspends and deduplicate the same wrapper across preload, replay, normal rendering, updates, and independent concurrent roots.
  • Retain synchronous fulfillment/rejection, synchronous loader/subscription retry, and deferred error routing. A thenable that synchronously settles and then throws replays its original subscription failure exactly once before its cached settled result becomes visible.
  • Preserve false branch guards, nested wrappers, component/default-prop/memo/context/ref behavior, deferred hydration server-node identity, explicit activation/prefetch, and SSR behavior.
  • Keep all generic static-root, hydration-root, deferred-hydration, Suspense-transition, and unused-pure-lazy production bundles byte-identical.

Production browser evidence

The existing async-composition dashboard gains one real, separately code-split synchronous lazy child. The browser benchmark adds a deterministic 35 ms delay to that actual JavaScript chunk and compares unchanged canonical runtime versus candidate runtime on the same authored dashboard, minification policy, Chromium environment, and interleaved AB/BA samples.

Observation Baseline Candidate
Lazy loader start 106.5 ms 1.3 ms
Actual split-chunk HTTP request start 106.9 ms 2.4 ms
Split-chunk response 145.3 ms 40.1 ms
Lazy module visible 146.2 ms 105.8 ms
Loader calls across mount + transition 1 1
Actual split-chunk network requests 1 1
Data resource creators per operation 8 8
Actual data network starts 8 8
Independent first-wave data requests 7 7
Data request waves 2 2
Mixed-version committed states 0 0

The eligible lazy module becomes visible approximately 40.4 ms sooner (27.6%) because its delayed code request overlaps the existing first data wave. The data-only dashboard-ready and update timings are unchanged within measurement noise; this does not claim faster data fetching. The separate module chunk remains 324 bytes; the affected main application adds 237 raw bytes / 80 gzip bytes. The fixed 16-file compiler-output corpus and all unrelated generic production bundles are byte-identical.

Validation

  • Full Octane development/production core suite: 806 files / 11,988 tests passed.
  • Broad affected lazy, Suspense, transition, hydration, SSR/streaming, compiler, and universal suites: 310 project-file executions / 3,927 tests passed.
  • Focused public lazy-wrapper regressions in development and production: 40 tests passed, including first-wave discovery, concurrent roots, false branches, loader rejection, synchronous transaction failures, and fulfilled/rejected-then-throw RED-to-GREEN cases.
  • Real SSR-to-client deferred-hydration regression: a dormant never() island remains untouched while an outside sibling actually warms, then explicitly activates once and adopts its original server DOM.
  • Existing production Vite integration: 15 production SSR/browser tests passed, including real Chromium development/production SSR/hydration, absent deferred-chunk module-preload, unchanged explicit prefetch, chunk splitting, and preserved deferred DOM.
  • Existing real-Chromium Suspense/hydration suite: 11 tests passed.
  • Existing async-composition production Chromium gate with actual delayed split-chunk request ordering and unchanged 8/8/7/2/0 data, wave, and atomicity controls.
  • Exact-head generic bundle DCE, pure unused lazy() elimination, and fixed compiler-corpus byte comparisons.
  • Full workspace typecheck: pnpm --config.verify-deps-before-run=false --config.enable-pre-post-scripts=false run typecheck.
  • Final repository synchronization: pnpm --config.verify-deps-before-run=false sync.
  • Scoped Prettier verification and git diff --check.

Refs #673.

Provenance

  • An agent produced this diff (agent-authored)

Note

Medium Risk
Touches core lazy() and Suspense warm-path semantics (loader timing, once-only loads, sync-thenable errors, Hydrate opacity); behavior is heavily tested but mistakes could cause duplicate loads or premature chunk fetches.

Overview
Eligible lazy() loaders can start while an independently suspended sibling is already in a compiler-proven warm plan, overlapping dynamic-import fetches with pending data instead of waiting for the boundary to reach the lazy child.

The runtime refactors lazy() so initializeLazy is shared between first render and speculative warming via lazyWrapper.__warm. Loaders still run once; resolved modules and default-export getters are not read until the component actually renders. Dormant <Hydrate> islands, unselected @if branches, and code behind never() stay out of discovery until activation or explicit prefetch.

Error handling adds fulfilled-error / rejected-error so a thenable that settles synchronously and then throws during .then registration replays that failure on the first real render instead of leaking a cached payload after speculative warm.

Validation extends lazy.test.ts (parallel siblings, concurrent roots, Hydrate dormancy, conditionals, rejections, sync loader/subscription failures), adds a code-split LazyCodeProof to the async-composition Octane dashboard, and tightens the Chromium gate (35ms chunk delay, single loader/request, overlap with the first project wave, stable DOM across transition). Docs describe the React difference and that lazy discovery does not enter dormant Hydrate boundaries.

Reviewed by Cursor Bugbot for commit b16a866. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent perf PR: performance work labels Aug 10, 2026
@domgan-oai
domgan-oai marked this pull request as ready for review August 10, 2026 23:15
@domgan-oai
domgan-oai merged commit db5687e into octanejs:main Aug 10, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent perf PR: performance work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant