Skip to content

perf(react_compiler): replace all compiled functions in a single AST walk#24403

Merged
graphite-app[bot] merged 1 commit into
mainfrom
perf/react-compiler-batch-fn-replace
Jul 12, 2026
Merged

perf(react_compiler): replace all compiled functions in a single AST walk#24403
graphite-app[bot] merged 1 commit into
mainfrom
perf/react-compiler-batch-fn-replace

Conversation

@Boshen

@Boshen Boshen commented Jul 12, 2026

Copy link
Copy Markdown
Member

What

OxcReplaceFnVisitor walked the entire program from the root once per compiled function — O(replacements × AST size). This collects all non-gated replacements into an FxHashMap<ScopeId, &CodegenFunction> and applies them in a single traversal, with a remaining counter that stops the walk early once every target has been applied.

Why it's equivalent

  • Non-gated replacement is a pure in-place field edit of a function node — it never inserts or removes program.body statements — so running the whole batch before the outlined-declaration insertion and gating loop (which do restructure program.body) cannot affect their results.
  • The gated path is untouched: it stays in the loop because it replaces a function with a gating() ? … : … conditional and clones the original body. Each replacement is gated or non-gated, never both.
  • A top-down walk reaches an outer function before any nested one and does not recurse into a replaced node, preserving the "outer wins, inner clobbered" behavior the N separate walks already produced — independent of list order or nesting.

Verification

  • cargo test -p oxc_react_compiler: the 1806-fixture snapshot corpus passes unchanged (byte-identical output), plus all transform integration tests.

Net: N full-program traversals → 1, scaling with the number of compiled functions per file.

@codspeed-hq

codspeed-hq Bot commented Jul 12, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 7.62%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 9 untouched benchmarks
⏩ 61 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation react_compiler[kitchen-sink.tsx] 289.3 ms 268.8 ms +7.62%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing perf/react-compiler-batch-fn-replace (b382f0c) with main (71945a2)

Open in CodSpeed

Footnotes

  1. 61 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.

@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Jul 12, 2026

Boshen commented Jul 12, 2026

Copy link
Copy Markdown
Member Author

Merge activity

…walk (#24403)

## What

`OxcReplaceFnVisitor` walked the entire program from the root **once per compiled function** — O(replacements × AST size). This collects all non-gated replacements into an `FxHashMap<ScopeId, &CodegenFunction>` and applies them in a **single traversal**, with a `remaining` counter that stops the walk early once every target has been applied.

## Why it's equivalent

- Non-gated replacement is a pure **in-place field edit** of a function node — it never inserts or removes `program.body` statements — so running the whole batch before the outlined-declaration insertion and gating loop (which *do* restructure `program.body`) cannot affect their results.
- The **gated** path is untouched: it stays in the loop because it replaces a function with a `gating() ? … : …` conditional and clones the *original* body. Each replacement is gated or non-gated, never both.
- A top-down walk reaches an outer function before any nested one and does not recurse into a replaced node, preserving the "outer wins, inner clobbered" behavior the N separate walks already produced — independent of list order or nesting.

## Verification

- `cargo test -p oxc_react_compiler`: the 1806-fixture snapshot corpus passes **unchanged** (byte-identical output), plus all transform integration tests.

Net: N full-program traversals → 1, scaling with the number of compiled functions per file.
@graphite-app
graphite-app Bot force-pushed the perf/react-compiler-batch-fn-replace branch from b382f0c to dce0f29 Compare July 12, 2026 15:14
@graphite-app
graphite-app Bot merged commit dce0f29 into main Jul 12, 2026
29 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label Jul 12, 2026
@graphite-app
graphite-app Bot deleted the perf/react-compiler-batch-fn-replace branch July 12, 2026 15:19
Boshen added a commit that referenced this pull request Jul 14, 2026
### 🚀 Features

- 616bfa2 minifier: Remove unreachable code after terminating statements
(#24441) (Dunqing)
- ddab89a data_structures: Add `likely` and `unlikely` functions
(#24368) (overlookmotel)
- a3a39f9 react_compiler: Implement enableEmitHookGuards codegen
(#24329) (Boshen)
- b79eef7 minifier: Apply De Morgan's law to negated comparison chains
in jump guards and loop tests (#24279) (Dunqing)
- 34ff7b4 minifier: Drop write-only property assignments to unused local
bindings by default (#24112) (Dunqing)
- 1b829d8 semantic: Record const enums in EnumData (#24268) (Dunqing)
- ba0944c semantic: Add `Scoping::set_symbol_span` (#24221) (camc314)

### 🐛 Bug Fixes

- 7d33363 minifier: Preserve guaranteed throws from class heritage
evaluation (#24349) (Dunqing)
- 058a62f semantic: Track ambient contexts in `SemanticBuilder` (#24327)
(camc314)
- 721eb0b transformer/decorator: Scope accessor class binding (#24330)
(camc314)
- 1ebdce3 semantic: Allow reserved keywords in ambient declaration types
(#24325) (camc314)
- 460176a track-memory-allocations: Exclude arena chunks from Sys allocs
(#24292) (Dunqing)
- af4922b transformer: Clear lowered namespace redeclarations (#24300)
(camc314)
- ffd2765 semantic: Mark declared computed `MethodDefinition`s as type
references (#24296) (camc314)
- f17514b isolated-declarations: Emit const readonly fields as types
(#24288) (camc314)
- 40f769d minifier: Make `__proto__` write tracking execution-order
independent (#24280) (Dunqing)
- 6371fed transformer: Remove stale enum member bindings (#24272)
(camc314)
- f05dfab transformer: Correct symbol flags for lowered namespaces
(#24271) (Dunqing)
- 84eeb55 transformer: Correct symbol flags for lowered enums (#24269)
(Dunqing)
- c3057da transformer: Preserve generated class binding spans (#24220)
(camc314)
- 8260096 transformer: Correct span for lowered namespace symbol
(#24222) (camc314)
- 42d00d3 semantic: Mark declared class heritage as type references
(#24237) (camc314)
- 588d997 semantic: Mark TS `PropertyDefinition`s computed fields as
type references (#24233) (camc314)
- 9b95632 semantic: Mark computed method keys in `TSMethodSignature`s as
type references (#24232) (camc314)

### ⚡ Performance

- 5b26643 transformer_plugins: Dispatch global defines by trailing name
(#23666) (Boshen)
- dce0f29 react_compiler: Replace all compiled functions in a single AST
walk (#24403) (Boshen)
- f85f0d8 ast: Delegate inherited enum variants in clone_in and estree
derives (#23555) (Boshen)
- 3ff0234 allocator: Remove `unwrap` from `ReplaceWith` (#24365)
(overlookmotel)
- ab22e80 transformer: Fix Rust 1.97 performance regression (#24354)
(camc314)
- b47585c parser: Use `ReplaceWith` instead of `TakeIn` (#24018)
(overlookmotel)
- b227a06 minifier: Use `ReplaceWith` instead of `TakeIn` (#24017)
(overlookmotel)

Co-authored-by: Boshen <[email protected]>
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.

1 participant