Skip to content

fix(code-splitting): don't order-wrap a merged-away entry with an unobservable load - #10454

Closed
hyfdev wants to merge 1 commit into
link-symbol-chunk-tablefrom
skip-unobservable-entry-wrap
Closed

hyfdev wants to merge 1 commit into
link-symbol-chunk-tablefrom
skip-unobservable-entry-wrap

Conversation

@hyfdev

@hyfdev hyfdev commented Jul 26, 2026 •

Copy link
Copy Markdown
Member

Under output.strictExecutionOrder, order-wrapping an entry whose chunk the post-chunk optimizer merged away does not defer anything. It makes restore_order_wrap_entry_facades resurrect a file for that entry, because a merged entry has nowhere else to put its trigger. On an app that declares a lazy entry per route, that is one extra chunk per route. This skips the wrapper in the one case where it cannot buy anything: no module in the entry's own static load closure is order-sensitive, so running that whole closure early is not observable.

lobehub, same Vite, only the rolldown package swapped:

JS files raw gzip brotli
strictExecutionOrder off 1518 — — —
before 2585 (+1067) +0.80% +2.12% +2.04%
after 1600 (+82) +0.42% +0.94% +0.91%

ComfyUI_frontend and plane are byte-identical before and after — neither has an entry the optimizer merged away, so nothing is skipped.

Motivation

The chunk growth is a regression against released behaviour, not an inherent cost of the guarantee. order_wrapping.rs does not exist at v1.2.0; it arrived with #10104. Released 1.2.0 emits 1471 JS files on lobehub with strictExecutionOrder on, 47 fewer than with it off. Current main emits 2585.

Nor is the growth the price of the correctness fixes in that series. #10441 gates create_order_wrap_entry_facades on the real cross-chunk edges, and on lobehub that gate creates 59 facades. The other 1008 files come from restore_order_wrap_entry_facades, whose candidate set is every order-wrapped entry with no necessity test — and the default plan wraps every entry.

Restoring is not the thing to gate. A resurrected chunk is the only place a merged entry's trigger can live:

  • Dropping the restore leaves the entry with no trigger at all, so it never initializes. That is a missing evaluation, not a mis-ordering, which is why gating it broke optimization/chunk_merging/dynamic_entry_merged_in_user_defined_entry.
  • One file can host only one entry's top-level trigger, so two merged entries sharing a host chunk cannot share one.
  • Moving the trigger to the import() call site fires a microtask too late even when the host chunk's top level is inert. function/strict_execution_order_invariants/m4_dynamic_facade_race pins that with a fully wrapped host chunk.

So the wrapper is what to skip, and it is safe to skip exactly when the entry's execution position is unobservable. is_order_sensitive already answers that per module from module content — EcmaViewMeta::ExecutionOrderSensitive, plus eagerly triggering an interop module. When no module in the entry's static load closure is order-sensitive, an unwrapped body inline in the host chunk runs that closure whenever the chunk loads and nothing an importer can observe differs.

That question reads module content, never chunk reachability, so it does not approximate a graph that does not exist yet when the plan is built — the failure mode that sank three earlier attempts at narrowing this cost, recorded in #10294.

What comes off, concretely

issues/9463 is the fixture this facade mechanism exists for: entry b must not run entry a's top-level side effect. Its shared.js is a dynamic-import target the optimizer merged into the shared chunk, so it was order-wrapped and restored as its own file:

// shared.js — resurrected so this call has somewhere to live
import { i as init_shared, r as foo } from "./common~a~b~shared.js";
init_shared();
export { foo };

init_shared was __esmMin((() => {})). shared.js only declares a function, so the wrapper deferred nothing and the file existed to call an empty init. Afterwards the module is a namespace object in the host chunk and the dynamic import collapses to a same-chunk Promise.resolve(). init_a and init_b are untouched, and the fixture's _test.mjs — which asserts that loading b never runs a's side effect — still passes.

optimization/chunk_merging/dynamic_entry_merged_in_user_defined_entry goes from three emitted files to one for the same reason.

The sensitivity signal has to be complete

add_top_level_eager_order_reasons ran only when experimental.onDemandWrapping was set, which left ExecutionOrderSensitive less complete in the default plan than in the selective one. Any decision about what a wrapper buys reads that flag, so the reasons are now collected whenever strictExecutionOrder is on.

Measured on its own, with the entry rule disabled: ComfyUI_frontend and plane byte-identical, lobehub raw byte-identical with gzip and brotli moving by 30 and 59 bytes out of 19 MB.

Non-goals

  • The selective wrapping plan stays behind experimental.onDemandWrapping. The predicted chunk edges, the expected-versus-actual order comparison and the emergent-cycle fixpoint are untouched, and which non-entry modules the default plan defers is unchanged.
  • The default plan is not brought to parity with the selective one on bytes. Both emit 1600 files on lobehub, but the selective plan is 28 KB smaller raw there, and on ComfyUI_frontend it is +4.13% raw against this PR's +7.12%. That remainder is wrapper code rather than chunk proliferation.
  • 1471 is not a target. Counting execution-order violations directly in the emitted output — a chunk whose inline __esm init another chunk statically imports, so loading that other chunk runs it — gives 58 for strictExecutionOrder off (pre-existing, from interop wrapping), 56 for released 1.2.0, and 0 both before and after this PR. Part of why the released file count is lower is that the build was not emitting those triggers.

Tests

test_extended_top_level_reasons_do_not_leak_to_the_non_strict_default replaces ..._do_not_leak_to_default_or_wrap_all: the reasons still must not reach a non-strict build, and both strict plans now collect the same ones. The snapshot updates are the fixtures whose entries the rule now leaves unwrapped.

hyfdev commented Jul 26, 2026

Copy link
Copy Markdown
Member Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


How to use the Graphite Merge Queue

Add the label graphite: merge-when-ready to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@hyfdev
hyfdev marked this pull request as ready for review July 26, 2026 11:19
Copilot AI review requested due to automatic review settings July 26, 2026 11:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hyfdev

hyfdev commented Jul 26, 2026

Copy link
Copy Markdown
Member Author

I'm also investigating the extra 82 chunks to check if it's reasonable or not.

@codspeed

codspeed Bot commented Jul 26, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing skip-unobservable-entry-wrap (34adef6) with link-symbol-chunk-table (3ba6b62)

Open in CodSpeed

Footnotes

  1. 10 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@hyfdev

hyfdev commented Jul 26, 2026

Copy link
Copy Markdown
Member Author

Wait. It seems there's a better solution

@hyfdev
hyfdev marked this pull request as draft July 26, 2026 14:40
@hyfdev
hyfdev force-pushed the link-symbol-chunk-table branch 2 times, most recently from dbd573e to f2113da Compare July 26, 2026 16:34
@hyfdev
hyfdev force-pushed the skip-unobservable-entry-wrap branch from 34adef6 to 5031e0f Compare July 26, 2026 16:36
@graphite-app
graphite-app Bot force-pushed the link-symbol-chunk-table branch 2 times, most recently from ec75a5f to e07c6d8 Compare July 26, 2026 16:58
…bservable load

Under `output.strictExecutionOrder`, order-wrapping an entry whose chunk the post-chunk optimizer merged away does not defer anything. It makes `restore_order_wrap_entry_facades` resurrect a file for that entry, because a merged entry has nowhere else to put its trigger. On an app that declares a lazy entry per route, that is one extra chunk per route. This skips the wrapper in the one case where it cannot buy anything: no module in the entry's own static load closure is order-sensitive, so running that whole closure early is not observable.

lobehub, same Vite, only the rolldown package swapped:

| | JS files | raw | gzip | brotli |
| --- | ---: | ---: | ---: | ---: |
| `strictExecutionOrder` off | 1518 | — | — | — |
| before | 2585 (+1067) | +0.80% | +2.12% | +2.04% |
| after | 1600 (+82) | +0.42% | +0.94% | +0.91% |

ComfyUI_frontend and plane are byte-identical before and after — neither has an entry the optimizer merged away, so nothing is skipped.

The chunk growth is a regression against released behaviour, not an inherent cost of the guarantee. `order_wrapping.rs` does not exist at `v1.2.0`; it arrived with #10104. Released 1.2.0 emits 1471 JS files on lobehub with `strictExecutionOrder` on, 47 *fewer* than with it off. Current `main` emits 2585.

Nor is the growth the price of the correctness fixes in that series. #10441 gates `create_order_wrap_entry_facades` on the real cross-chunk edges, and on lobehub that gate creates 59 facades. The other 1008 files come from `restore_order_wrap_entry_facades`, whose candidate set is every order-wrapped entry with no necessity test — and the default plan wraps every entry.

Restoring is not the thing to gate. A resurrected chunk is the only place a merged entry's trigger can live:

- Dropping the restore leaves the entry with no trigger at all, so it never initializes. That is a missing evaluation, not a mis-ordering, which is why gating it broke `optimization/chunk_merging/dynamic_entry_merged_in_user_defined_entry`.
- One file can host only one entry's top-level trigger, so two merged entries sharing a host chunk cannot share one.
- Moving the trigger to the `import()` call site fires a microtask too late even when the host chunk's top level is inert. `function/strict_execution_order_invariants/m4_dynamic_facade_race` pins that with a fully wrapped host chunk.

So the wrapper is what to skip, and it is safe to skip exactly when the entry's execution position is unobservable. `is_order_sensitive` already answers that per module from module content — `EcmaViewMeta::ExecutionOrderSensitive`, plus eagerly triggering an interop module. When no module in the entry's static load closure is order-sensitive, an unwrapped body inline in the host chunk runs that closure whenever the chunk loads and nothing an importer can observe differs.

That question reads module content, never chunk reachability, so it does not approximate a graph that does not exist yet when the plan is built — the failure mode that sank three earlier attempts at narrowing this cost, recorded in #10294.

`issues/9463` is the fixture this facade mechanism exists for: entry `b` must not run entry `a`'s top-level side effect. Its `shared.js` is a dynamic-import target the optimizer merged into the shared chunk, so it was order-wrapped and restored as its own file:

```js
// shared.js — resurrected so this call has somewhere to live
import { i as init_shared, r as foo } from "./common~a~b~shared.js";
init_shared();
export { foo };
```

`init_shared` was `__esmMin((() => {}))`. `shared.js` only declares a function, so the wrapper deferred nothing and the file existed to call an empty init. Afterwards the module is a namespace object in the host chunk and the dynamic import collapses to a same-chunk `Promise.resolve()`. `init_a` and `init_b` are untouched, and the fixture's `_test.mjs` — which asserts that loading `b` never runs `a`'s side effect — still passes.

`optimization/chunk_merging/dynamic_entry_merged_in_user_defined_entry` goes from three emitted files to one for the same reason.

`add_top_level_eager_order_reasons` ran only when `experimental.onDemandWrapping` was set, which left `ExecutionOrderSensitive` less complete in the default plan than in the selective one. Any decision about what a wrapper buys reads that flag, so the reasons are now collected whenever `strictExecutionOrder` is on.

Measured on its own, with the entry rule disabled: ComfyUI_frontend and plane byte-identical, lobehub raw byte-identical with gzip and brotli moving by 30 and 59 bytes out of 19 MB.

- The selective wrapping plan stays behind `experimental.onDemandWrapping`. The predicted chunk edges, the expected-versus-actual order comparison and the emergent-cycle fixpoint are untouched, and which non-entry modules the default plan defers is unchanged.
- The default plan is not brought to parity with the selective one on bytes. Both emit 1600 files on lobehub, but the selective plan is 28 KB smaller raw there, and on ComfyUI_frontend it is +4.13% raw against this PR's +7.12%. That remainder is wrapper code rather than chunk proliferation.
- 1471 is not a target. Counting execution-order violations directly in the emitted output — a chunk whose inline `__esm` init another chunk statically imports, so loading that other chunk runs it — gives 58 for `strictExecutionOrder` off (pre-existing, from interop wrapping), 56 for released 1.2.0, and 0 both before and after this PR. Part of why the released file count is lower is that the build was not emitting those triggers.

`test_extended_top_level_reasons_do_not_leak_to_the_non_strict_default` replaces `..._do_not_leak_to_default_or_wrap_all`: the reasons still must not reach a non-strict build, and both strict plans now collect the same ones. The snapshot updates are the fixtures whose entries the rule now leaves unwrapped.
@graphite-app
graphite-app Bot force-pushed the skip-unobservable-entry-wrap branch from 5031e0f to b4e4554 Compare July 26, 2026 16:58
@hyfdev hyfdev closed this Jul 27, 2026
graphite-app Bot pushed a commit that referenced this pull request Jul 28, 2026
…trict build (#10463)

`add_top_level_eager_order_reasons` ran only when `experimental.onDemandWrapping` was set, which left `EcmaViewMeta::ExecutionOrderSensitive` less complete in the default strict plan than in the selective one. That walk exists because a pure annotation answers a different question than execution order asks: it says a statement is safe to *drop* when nothing uses it, not that the statement is safe to *run at a different time* when it is kept. A top-level statement tree-shaking treats as pure — a `manualPureFunctions`-tagged call in a class static initializer that reads mutable global state, say — still observes its own position once the module is included. So the plan that defers every eligible module was working from a weaker signal than the plan that derives its wrapping from the analysis, for a fact that is a property of the module rather than of the plan.

## What reads the signal

`ExecutionOrderSensitive` is not read only by the wrap planner. `sort_chunk_modules` splits a chunk's modules into the leaf ones — no import records and no `ExecutionOrderSensitive` — and the rest, then hoists that leaf group and sorts it by module id rather than by execution order under `experimental.chunkModulesOrder: 'module-id'`. That consumer has no idea which plan produced the wrap set, so as things stand the same module lands in the leaf group under the default plan and outside it under `onDemandWrapping`, on a question about the module's own content.

Under the default plan the difference is textual for now, and deliberately so: `wrap_all_order_analysis` defers every eligible module, so nothing evaluates at chunk top level and hoisting a leaf cannot move an evaluation. It stops being textual as soon as something reads sensitivity to make a decision under that plan, which is what #10454's exemption for a merged-away entry with an unobservable load does. Landing the completeness first keeps that PR's rule reading the same signal the selective plan already reads.

## The change

Gate the walk on `strictExecutionOrder` rather than on strict plus on-demand wrapping. Non-strict builds keep the old behaviour on purpose: `add_top_level_eager_order_reasons` only ever adds order reasons and never touches the tree-shaking flags, so running it without the flag could only make a build that never asked for an order guarantee pay for one. The three comments that scoped the walk to on-demand builds are updated to match.

Measured on its own, with #10454's entry rule disabled: ComfyUI_frontend and plane are byte-identical, and lobehub is byte-identical raw, with gzip and brotli moving by 30 and 59 bytes out of 19 MB.

Extracted from #10454 so it can land on its own; #10454 drops these hunks when it rebases, and its `order_analysis.rs` change is what reads `is_strict_on_demand_wrapping_enabled` again. Until then the accessor has no in-repo caller, so its doc comment now describes the mode it names rather than the walk it used to gate.

## Tests

`test_extended_top_level_reasons_do_not_leak_to_default_or_wrap_all` becomes `test_extended_top_level_reasons_do_not_leak_to_the_non_strict_default` and now covers both halves at the analyzer level: the non-strict default still collects no reasons, and the two strict plans — wrap-all and on-demand — collect the same one. The full `-p rolldown` suite passes with no snapshot changes.
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