Skip to content

perf(transformer_plugins): dispatch global defines by trailing name#23666

Merged
graphite-app[bot] merged 1 commit into
mainfrom
perf/replace-global-defines-dispatch
Jul 13, 2026
Merged

perf(transformer_plugins): dispatch global defines by trailing name#23666
graphite-app[bot] merged 1 commit into
mainfrom
perf/replace-global-defines-dispatch

Conversation

@Boshen

@Boshen Boshen commented Jun 20, 2026

Copy link
Copy Markdown
Member

Summary

ReplaceGlobalDefines matched every configured define against every AST node by linearly scanning the full define list — O(nodes × defines). For each identifier reference it scanned all identifier defines; for each member expression it scanned all dot defines and all meta defines.

This keys defines by their trailing segment (the outermost property name) — the only segment a node can match on — so each node becomes a single hash lookup instead of a full scan:

  • identifier defines → FxHashMap<name, value>
  • dot defines (process.env.NODE_ENV) → FxHashMap<last_part, Vec<DotDefine>>
  • non-wildcard meta defines (import.meta.env.MODE) → FxHashMap<last_part, Vec<MetaPropertyDefine>>
  • wildcard meta defines (import.meta.env.*) match a variable trailing name, so they can't be keyed; they stay a small linear list, scanned only for import.meta-rooted expressions.

The config-time sort is removed — "specific wins over wildcard" is now structural (the map is consulted before the wildcard list). Computed members (a["b"]) extract the static key once and skip entirely when the key is dynamic.

Correctness

is_meta_property_define now verifies import / meta at its MetaProperty arm, so new.target.env is no longer wrongly replaced by an import.meta.env define (previously it matched any meta property — import.meta or new.target). Added a regression test.

Benchmark

Isolated ReplaceGlobalDefines::build() (re-parsing outside the timer) with a ~60-entry Vite-like define config:

file before after speedup
RadixUIAdoptionSection.jsx 2.38 µs 0.64 µs 3.7×
react.development.js 72.2 µs 39.0 µs 1.9×
App.tsx 852 µs 307 µs 2.8×
binder.ts 243 µs 88 µs 2.8×
kitchen-sink.tsx 1345 µs 677 µs 2.0×

Speedup scales with the number of defines. With a single define the two are roughly even (an FxHash of a short property name ≈ the previous single leaf-compare), so no regression on small configs.

🤖 Generated with Claude Code

@codspeed-hq

codspeed-hq Bot commented Jun 20, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 4.48%

⚠️ 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

⚡ 2 improved benchmarks
✅ 50 untouched benchmarks
⏩ 19 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation pipeline[App.tsx] 35.8 ms 33.9 ms +5.53%
Simulation pipeline[binder.ts] 13.9 ms 13.4 ms +3.44%

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/replace-global-defines-dispatch (d631a5b) with main (bd6edfe)

Open in CodSpeed

Footnotes

  1. 19 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 force-pushed the perf/replace-global-defines-dispatch branch from b7985b4 to 914f547 Compare June 21, 2026 07:03
@Boshen
Boshen force-pushed the perf/replace-global-defines-dispatch branch from 914f547 to ffb97da Compare July 13, 2026 02:07
@github-actions github-actions Bot added the A-transformer Area - Transformer / Transpiler label Jul 13, 2026
@Boshen
Boshen marked this pull request as ready for review July 13, 2026 06:59
@Boshen
Boshen requested a review from Dunqing as a code owner July 13, 2026 06:59
@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Jul 13, 2026

Boshen commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

Merge activity

…23666)

## Summary

`ReplaceGlobalDefines` matched every configured define against every AST node by linearly scanning the full define list — `O(nodes × defines)`. For each identifier reference it scanned all identifier defines; for each member expression it scanned all dot defines and all meta defines.

This keys defines by their **trailing segment** (the outermost property name) — the only segment a node can match on — so each node becomes a single hash lookup instead of a full scan:

- `identifier` defines → `FxHashMap<name, value>`
- `dot` defines (`process.env.NODE_ENV`) → `FxHashMap<last_part, Vec<DotDefine>>`
- non-wildcard `meta` defines (`import.meta.env.MODE`) → `FxHashMap<last_part, Vec<MetaPropertyDefine>>`
- wildcard `meta` defines (`import.meta.env.*`) match a *variable* trailing name, so they can't be keyed; they stay a small linear list, scanned only for `import.meta`-rooted expressions.

The config-time `sort` is removed — "specific wins over wildcard" is now structural (the map is consulted before the wildcard list). Computed members (`a["b"]`) extract the static key once and skip entirely when the key is dynamic.

## Correctness

`is_meta_property_define` now verifies `import` / `meta` at its `MetaProperty` arm, so `new.target.env` is no longer wrongly replaced by an `import.meta.env` define (previously it matched *any* meta property — `import.meta` **or** `new.target`). Added a regression test.

## Benchmark

Isolated `ReplaceGlobalDefines::build()` (re-parsing outside the timer) with a ~60-entry Vite-like define config:

| file | before | after | speedup |
|---|--:|--:|--:|
| RadixUIAdoptionSection.jsx | 2.38 µs | 0.64 µs | 3.7× |
| react.development.js | 72.2 µs | 39.0 µs | 1.9× |
| App.tsx | 852 µs | 307 µs | 2.8× |
| binder.ts | 243 µs | 88 µs | 2.8× |
| kitchen-sink.tsx | 1345 µs | 677 µs | 2.0× |

Speedup scales with the number of defines. With a single define the two are roughly even (an `FxHash` of a short property name ≈ the previous single leaf-compare), so no regression on small configs.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@graphite-app
graphite-app Bot force-pushed the perf/replace-global-defines-dispatch branch from d631a5b to 5b26643 Compare July 13, 2026 07:06
@graphite-app
graphite-app Bot merged commit 5b26643 into main Jul 13, 2026
29 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label Jul 13, 2026
@graphite-app
graphite-app Bot deleted the perf/replace-global-defines-dispatch branch July 13, 2026 07:11
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

A-transformer Area - Transformer / Transpiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant