Skip to content

perf(transform): reserve scoping headroom for the transformer - #9481

Closed
Dunqing wants to merge 2 commits into
mainfrom
perf/scoping-headroom-for-transformer
Closed

Dunqing wants to merge 2 commits into
mainfrom
perf/scoping-headroom-for-transformer

Conversation

@Dunqing

@Dunqing Dunqing commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Reserve 50% excess capacity on the SemanticBuilder calls that feed Transformer::build_with_scoping. The transformer grows the scoping Vecs 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 on is_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_ast on 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.

ratio modules still reallocating
0.00 49 (4.10%)
0.20 6 (0.50%)
0.50 1 (0.08%)
2.00 0 — but regresses perf (3× memory cost on every module to save one extra realloc)

Dunqing commented May 20, 2026

Copy link
Copy Markdown
Contributor Author

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.

@netlify

netlify Bot commented May 20, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit 52418c2
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/6a49ad68ffd59200081966d2

@Dunqing
Dunqing marked this pull request as ready for review May 20, 2026 10:12
@codspeed

codspeed Bot commented May 20, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing perf/scoping-headroom-for-transformer (52418c2) with main (2d9f81c)

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. ↩

@Dunqing
Dunqing force-pushed the perf/scoping-headroom-for-transformer branch from be065ee to b690dbb Compare May 20, 2026 10:32
@Dunqing Dunqing changed the title perf(rolldown): reserve scoping headroom for transformer growth perf(transform): reserve scoping headroom for the transformer May 20, 2026
@Dunqing
Dunqing force-pushed the perf/scoping-headroom-for-transformer branch 2 times, most recently from 9c284f8 to 4b6da65 Compare May 20, 2026 11:47
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]>
@Dunqing
Dunqing force-pushed the perf/scoping-headroom-for-transformer branch from 4b6da65 to 71f76ab Compare May 20, 2026 15:34
@Dunqing
Dunqing requested a review from shulaoda May 21, 2026 08:44
@shulaoda shulaoda self-assigned this May 21, 2026
hyfdev
hyfdev previously approved these changes Jul 6, 2026

@hyfdev hyfdev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We need have a way to track this, otherwise the optimization might be break without notice in the future change.

@shulaoda
shulaoda marked this pull request as draft July 6, 2026 03:26
// Run transformer on JS files containing `</script` to handle tagged template literals.
|| contains_script_closing_tag(ast.source().as_bytes())
{
if will_run_transformer {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  • define changes can make scoping become None before the transformer.
  • Then this path rebuilds via recreate_scoping.
  • recreate_scoping uses with_stats(self.stats), so with_excess_capacity(0.5) does not apply.
  • Browser builds commonly have the default process.env.NODE_ENV define, so this is not a cold path.
  • Can we reserve the same 50% headroom for the transformer rebuild path too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will take a careful look later.

@hyfdev
hyfdev dismissed their stale review July 6, 2026 04:18

PR turned to draft

@shulaoda
shulaoda marked this pull request as ready for review July 6, 2026 07:37
@shulaoda

shulaoda commented Jul 6, 2026

Copy link
Copy Markdown
Member

Discuss with @Dunqing some details, no blockers for me

@Dunqing
Dunqing marked this pull request as draft July 7, 2026 02:19
@shulaoda

shulaoda commented Nov 4, 2026

Copy link
Copy Markdown
Member

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.

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