Skip to content

perf(react_compiler): reduce allocations in reactive-places and mutation-aliasing passes#24476

Merged
graphite-app[bot] merged 1 commit into
mainfrom
perf/react-compiler-hot-passes
Jul 14, 2026
Merged

perf(react_compiler): reduce allocations in reactive-places and mutation-aliasing passes#24476
graphite-app[bot] merged 1 commit into
mainfrom
perf/react-compiler-hot-passes

Conversation

@Boshen

@Boshen Boshen commented Jul 14, 2026

Copy link
Copy Markdown
Member

Two allocation-focused optimizations to the React Compiler's two hottest passes. Both are behavior-preserving — the full snapshot suite (tests/snapshot.rs, the ~1288 upstream fixtures) is unchanged, and no snapshots were regenerated.

Profiling the compiler on real component files showed ~99% of compile time is the compiler core (parse/semantic/codegen are <1% combined), and the core is allocation-bound: compiling a 35 KB file did ~1.2M heap allocations. These two changes remove most of that.

1. Memoize post-dominator frontiers (infer_reactive_places)

is_reactive_controlled_block recomputed each block's post-dominator frontier — a reverse-CFG walk that allocates several sets — on every call, which happens once per block and once per phi operand on every fixpoint iteration. The frontier depends only on the CFG, which is invariant across the fixpoint, so it is now computed once per block up front and looked up.

2. Inline SmallVec for AbstractValue.reason (infer_mutation_aliasing_effects)

AbstractValue.reason was an FxIndexSet<ValueReason>, which heap-allocates on every clone. During inference these values are cloned constantly (once per entry on every InferenceState clone), so the reason sets dominated allocation in the pass. The sets are tiny — usually a single element, never more than one per ValueReason variant — so they are now backed by an inline, insertion-ordered SmallVec: clones no longer allocate, and primary_reason (first non-Other reason) keeps its ordering.

Results

Measured on real-world React component files (from bluesky-social/social-app), compile pass only, system allocator:

File before after allocations
Explore.tsx (35 KB) 49.6 ms 23.5 ms (2.1×) 1.24M → 220K (−82%)
MessageItem.tsx (30 KB) 28.4 ms 20.9 ms (1.36×) 542K → 297K (−45%)
Drawer.tsx (21 KB) 9.7 ms 8.7 ms (1.11×) 183K → 155K (−15%)

#2 is the larger lever — the reason-set clones dominated allocation in the pass. #1 is a complementary ~6–10% CPU win from avoiding the redundant frontier recomputation.

🤖 Generated with Claude Code

@codspeed-hq

codspeed-hq Bot commented Jul 14, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 4.03%

⚡ 4 improved benchmarks
✅ 58 untouched benchmarks
⏩ 9 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation react_compiler[kitchen-sink.tsx] 216.4 ms 205.3 ms +5.39%
Simulation react_compiler[RadixUIAdoptionSection.jsx] 3.1 ms 3 ms +4.38%
Simulation linter[kitchen-sink.tsx] 360.8 ms 349.3 ms +3.28%
Simulation linter[RadixUIAdoptionSection.jsx] 3.6 ms 3.5 ms +3.1%

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-hot-passes (9462f0e) with main (edf781d)

Open in CodSpeed

Footnotes

  1. 9 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 14, 2026

Boshen commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

Merge activity

…ion-aliasing passes (#24476)

Two allocation-focused optimizations to the React Compiler's two hottest passes. Both are behavior-preserving — the full snapshot suite (`tests/snapshot.rs`, the ~1288 upstream fixtures) is unchanged, and no snapshots were regenerated.

Profiling the compiler on real component files showed ~99% of compile time is the compiler core (parse/semantic/codegen are <1% combined), and the core is allocation-bound: compiling a 35 KB file did ~1.2M heap allocations. These two changes remove most of that.

### 1. Memoize post-dominator frontiers (`infer_reactive_places`)

`is_reactive_controlled_block` recomputed each block's post-dominator frontier — a reverse-CFG walk that allocates several sets — on every call, which happens once per block and once per phi operand on every fixpoint iteration. The frontier depends only on the CFG, which is invariant across the fixpoint, so it is now computed once per block up front and looked up.

### 2. Inline `SmallVec` for `AbstractValue.reason` (`infer_mutation_aliasing_effects`)

`AbstractValue.reason` was an `FxIndexSet<ValueReason>`, which heap-allocates on every clone. During inference these values are cloned constantly (once per entry on every `InferenceState` clone), so the reason sets dominated allocation in the pass. The sets are tiny — usually a single element, never more than one per `ValueReason` variant — so they are now backed by an inline, insertion-ordered `SmallVec`: clones no longer allocate, and `primary_reason` (first non-`Other` reason) keeps its ordering.

### Results

Measured on real-world React component files (from `bluesky-social/social-app`), compile pass only, system allocator:

| File | before | after | allocations |
| --- | ---: | ---: | ---: |
| Explore.tsx (35 KB) | 49.6 ms | 23.5 ms (**2.1×**) | 1.24M → 220K (**−82%**) |
| MessageItem.tsx (30 KB) | 28.4 ms | 20.9 ms (**1.36×**) | 542K → 297K (**−45%**) |
| Drawer.tsx (21 KB) | 9.7 ms | 8.7 ms (1.11×) | 183K → 155K (−15%) |

\#2 is the larger lever — the reason-set clones dominated allocation in the pass. \#1 is a complementary ~6–10% CPU win from avoiding the redundant frontier recomputation.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@graphite-app
graphite-app Bot force-pushed the perf/react-compiler-hot-passes branch from 9462f0e to 1e3e626 Compare July 14, 2026 03:53
@graphite-app
graphite-app Bot merged commit 1e3e626 into main Jul 14, 2026
30 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label Jul 14, 2026
@graphite-app
graphite-app Bot deleted the perf/react-compiler-hot-passes branch July 14, 2026 03:57
graphite-app Bot pushed a commit that referenced this pull request Jul 14, 2026
…24480)

Follow-up to #24476. Profiling the mutation-aliasing pass after that PR showed the `SmallVec<[ValueReason; 4]>` I introduced for `AbstractValue.reason` at **~7% of compile self-time** — it no longer allocates, but it's still copied on every `AbstractValue` clone, which happens once per entry on every `InferenceState` clone in the fixpoint.

There are only 12 `ValueReason` variants, so the whole set fits in a `u16` bitmask. `ReasonSet` — and therefore `AbstractValue` — becomes `Copy`, so copying it is a register move instead of a memcpy, and membership / union / superset collapse to single bitwise ops.

The one behavioral wrinkle is `primary_reason`, which returns the first non-`Other` reason: it now iterates in `ValueReason` declaration order rather than insertion order. The full snapshot suite (`tests/snapshot.rs`, the ~1288 upstream fixtures) is **unchanged**, confirming the ordering difference is not observable in output.

### Results

Real-world React component files (from `bluesky-social/social-app`), compile pass only:

| File | before | after |
| --- | ---: | ---: |
| Explore.tsx (35 KB) | 23.1 ms | **21.2 ms** (−8%) |
| MessageItem.tsx (30 KB) | 21.3 ms | **20.5 ms** (−4%) |
| Drawer.tsx (21 KB) | 8.6 ms | 8.7 ms (noise; few reasons) |

Allocation count is unchanged — the `SmallVec` was already inline, so this is purely a copy/`memcpy` win.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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