Repository navigation
Conversation
How to use the Graphite Merge QueueAdd 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. |
✅ Deploy Preview for rolldown-rs canceled.
|
Merging this PR will not alter performance
Comparing Footnotes
|
be065ee to
b690dbb
Compare
9c284f8 to
4b6da65
Compare
The transformer mutates the `Scoping` it's handed in place — TS enum lowering wraps each enum in an IIFE (+1 function scope, +1 parameter symbol, +N member references), decorator lowering adds helpers, JSX/ES lowering add temps and refs. The underlying `Vec`s in `Scoping` realloc mid-traversal without headroom. Reserve 50% excess capacity on every `SemanticBuilder` that feeds the transformer: - `crates/rolldown/src/utils/pre_process_ecma_ast.rs` — the bundler hot path. Gated on `will_run_transformer` (`is_not_js || should_transform_js || contains_script_closing_tag`), hoisted out of Step 3 so the gate and the headroom branch share one expression. Plain JS files that skip the transformer don't pay the upfront cost (`with_excess_capacity(0.0)` is the default and a no-op). - `crates/rolldown_plugin_vite_transform/src/lib.rs` — runs once per module in every Vite build. The plugin's only job is to transform, so the transformer always runs; no gate. - `crates/rolldown_common/src/utils/enhanced_transform.rs` — the standalone transform API exposed via `rolldown_binding`. Two sites feed the transformer (first build, post-define fallback). The inject-only builder is left untouched: inject typically adds a handful of bindings, far below the 50% threshold, so the headroom would just waste memory. `SemanticBuilder::with_excess_capacity` is documented as a no-op when `with_stats` is also chained (`oxc_semantic-0.132.0/src/builder.rs:242`); none of these sites pass `with_stats`, so the excess takes effect. Ratio chosen empirically. Instrumenting `pre_process_ecma_ast` against the rome_ts fixture (1195 TS modules) shows 96% of modules undergo zero in-place growth through the transformer; the remaining 4% grow up to 50% on scopes / 25% on symbols / 20% on refs. `0.5` covers all but one module (99.92% coverage) with modest upfront memory overhead — 0.2 leaves 6 modules still reallocating, 2.0 wastes 4× the memory for one additional covered module. Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
4b6da65 to
71f76ab
Compare
hyfdev
left a comment
There was a problem hiding this comment.
We need have a way to track this, otherwise the optimization might be break without notice in the future change.
| // Run transformer on JS files containing `</script` to handle tagged template literals. | ||
| || contains_script_closing_tag(ast.source().as_bytes()) | ||
| { | ||
| if will_run_transformer { |
There was a problem hiding this comment.
definechanges can makescopingbecomeNonebefore the transformer.- Then this path rebuilds via
recreate_scoping. recreate_scopinguseswith_stats(self.stats), sowith_excess_capacity(0.5)does not apply.- Browser builds commonly have the default
process.env.NODE_ENVdefine, so this is not a cold path. - Can we reserve the same 50% headroom for the transformer rebuild path too?
There was a problem hiding this comment.
I will take a careful look later.
|
Discuss with @Dunqing some details, no blockers for me |
|
IIRC, we concluded this optimization belongs on the oxc side, so we’re not considering it for now. I may be wrong, so feel free to reopen anytime. |

Summary
Reserve 50% excess capacity on the
SemanticBuildercalls that feedTransformer::build_with_scoping. The transformer grows the scopingVecs in place — TS enums, decorators, JSX and ES lowering all add symbols/refs/scopes — so pre-reserving headroom avoids realloc mid-traversal.Sites
crates/rolldown/src/utils/pre_process_ecma_ast.rs— gated onis_not_js || should_transform_js || contains_script_closing_tag. Plain JS modules skip the headroom (with_excess_capacity(0.0)is the default no-op).crates/rolldown_plugin_vite_transform/src/lib.rs— always transforms, no gate.crates/rolldown_common/src/utils/enhanced_transform.rs— two sites: first build + post-define fallback. The inject-only build is left alone (handful of bindings, not worth 50% headroom).Ratio
Picked empirically by instrumenting
pre_process_ecma_aston rome_ts (1195 TS modules). 96% see zero growth through the transformer; the remaining 4% grow up to 50% on scopes / 25% on symbols / 20% on refs.