perf(semantic): flatten hoisting_variables to avoid per-scope map allocation#23927
Merged
Dunqing merged 1 commit intoJun 29, 2026
Merged
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
…ocation `hoisting_variables` was a nested `FxHashMap<ScopeId, IdentHashMap<'a, SymbolId>>`, so `entry(scope_id).or_default()` heap-allocated a fresh inner `IdentHashMap` (system allocator) for every scope that hoists a variable. On antd.js this was the single largest system-allocation site in semantic analysis. Flatten to `FxHashMap<(ScopeId, Ident<'a>), SymbolId>`: one insert / one probe per hoisted var, no per-scope inner map, and one hash instead of two. Behaviour is identical — the map is only ever inserted into and point-queried, never iterated. `cargo allocs` (system allocations): semantic antd.js 1020 -> 28 (-97%) minifier antd.js 3041 -> 1040 (-66%; the minifier rebuilds semantic internally)
3922016 to
6e9cd15
Compare
Dunqing
approved these changes
Jun 29, 2026
camc314
added a commit
that referenced
this pull request
Jun 29, 2026
### 💥 BREAKING CHANGES - 94fbacb ast: [**BREAKING**] Only export `AstBuilder` and `NONE` in `builder` module (#23876) (overlookmotel) - 8de5122 ecmascript: [**BREAKING**] Switch to new `AstBuilder` (#23834) (overlookmotel) - dc0ef38 transformer: [**BREAKING**] Switch to new `AstBuilder` (#23831) (overlookmotel) - 88f4455 str: [**BREAKING**] `Str` and `Ident` methods take `&GetAllocator` (#23781) (overlookmotel) - 36009dd allocator: [**BREAKING**] `GetAllocator::allocator` take `&self` (#23676) (overlookmotel) - bd74f9d allocator: [**BREAKING**] Rename `AllocatorAccessor` trait to `GetAllocator` (#23675) (overlookmotel) ### 🚀 Features - 326fe25 transformer_plugins: Support `typeof` `define` keys (#23605) (Alexander Lichter) - f2091b3 ast: Unify old and new `AstBuilder`s (#23875) (overlookmotel) - cd1fd12 codegen: Expose `Codegen::print_string` API (#23785) (camc314) - 785461b ast: Add custom builder methods to AST types (#23651) (overlookmotel) - 05d1357 ast: Add AST creation methods to AST types (#23650) (overlookmotel) - 2580eda str: Add `Str::from_str_in` and `Ident::from_str_in` methods (#23767) (overlookmotel) - 6883fcf minifier: Fold write-once falsy var to false in boolean context (#23540) (Dunqing) - fcbf993 allocator: Add `Vec::from_value_in` method (#23718) (overlookmotel) - 989ddb7 allocator: Add `Vec::from_box_in` method (#23717) (overlookmotel) - 9d1aa7f allocator: Improve `PartialEq` for `Vec` (#23716) (overlookmotel) ### 🐛 Bug Fixes - da0e5bf minifier: Don't reorder a closed-over TDZ read when inlining a var (#23771) (Dunqing) - 0b3021f allocator: Remove `Vec::from_box_in` (#23873) (overlookmotel) - 0ab64ec ast: Silence deprecation warnings within files defining deprecated `AstBuilder` methods (#23889) (overlookmotel) - 8c07cad all: Enable `disable_old_builder` Cargo feature for `oxc_ast` crate in tests (#23888) (overlookmotel) - 3800f01 ast: Legacy `AstBuilder` methods take `self` not `&self` (#23891) (overlookmotel) - 869ac20 semantic/cfg: Connect for update exit to loop test (#23791) (camc314) - d3e92d5 semantic/cfg: Connect while branches from condition exit (#23790) (camc314) - 025045d ast: `ExportNamedDeclaration` plain builder methods return boxed nodes (#23783) (overlookmotel) - 7537c58 ast: Fix name of `AstBuilder` method for `Expression::V8IntrinsicExpression` (#23766) (overlookmotel) - 3f574f5 traverse: Fix unsoundness in `Traverse` walk functions (#23745) (overlookmotel) - 585760f parser: String in AST reference arena (#23721) (overlookmotel) - 7231d55 allocator: Fix unsound lifetime extension in `Box::new_in` (#23685) (overlookmotel) ### ⚡ Performance - d5c916a semantic: Flatten hoisting_variables to avoid per-scope map allocation (#23927) (Lawrence Lin) - e71609d minifier: Bail member-expr folding before the side-effect walk (#23924) (Lawrence Lin) - e1f89ab minifier: Reduce string allocations folding addition (#23846) (overlookmotel) - 9f6ee3b isolated-declarations: Pool scope maps to avoid per-scope alloc/rehash (#23761) (Boshen) - 0b07c4c semantic: Avoid heap alloc for catch-clause binding ids (#23911) (Lawrence Lin) - c5eef8b regular_expression: Skip capturing-group pre-parse when pattern has no `(` (#23908) (Lawrence Lin) - b4f5b4b isolated_declarations: Remove redundant clone of formal parameter pattern (#23912) (Lawrence Lin) - 53d083f isolated_declarations: Use `TakeIn` not `CloneIn` (#23847) (overlookmotel) - 3ea9304 react_compiler: Use faster API to arena allocate strings (#23849) (overlookmotel) - a6d8e45 parser: Avoid span lookup for arrow expression body (#23788) (camc314) - e1886a0 transformer, minifier: Use `static_ident!` macro to create static `Ident`s (#23727) (overlookmotel) - 5527bef transformer/object-rest-spread: Reduce iteration (#23720) (overlookmotel) - 680ffbc transformer: Allocate AST nodes in arena directly (#23711) (overlookmotel) - 1c63c66 parser: Allocate AST nodes in arena directly (#23712) (overlookmotel) - 3855f0c minifier: Allocate AST nodes in arena directly (#23710) (overlookmotel) - d025887 isolated_declarations: Allocate AST nodes in arena directly (#23709) (overlookmotel) - 10b96c6 parser: Remove string search from parsing JSX element name (#23713) (overlookmotel) ### 📚 Documentation - 3d61dea all: Correct capitalization in comments (#23887) (overlookmotel) - aa1ad74 ast: Add `#[deprecated]` to legacy `AstBuilder` methods (#23877) (overlookmotel) - a4676db ast: Correct doc comment for `NONE` (#23765) (overlookmotel) - 419ec80 syntax: Fix typo in doc comment (#23674) (overlookmotel) ### 🛡️ Security - 3cdd18f deps: Update npm packages (#23690) (renovate[bot]) Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com> Co-authored-by: Cameron <cameron.clark@hey.com>
camc314
pushed a commit
that referenced
this pull request
Jul 3, 2026
…ocation (#23927) ## Summary `SemanticBuilder::hoisting_variables` is a nested map `FxHashMap<ScopeId, IdentHashMap<'a, SymbolId>>`. Each `entry(scope_id).or_default()` on a vacant scope heap-allocates a fresh inner `IdentHashMap` via the **system** allocator, and subsequent inserts grow it. On `antd.js` this is the single largest system-allocation site in semantic analysis (`VariableDeclarator::bind` / `Function::bind` in `binder.rs`). This flattens it to a single `FxHashMap<(ScopeId, Ident<'a>), SymbolId>`: - one insert / one point-probe per hoisted variable (was: outer probe → inner-map allocation → inner probe); - no per-scope inner map, so the per-scope system allocations disappear; - one hash instead of two. Behaviour is identical: `hoisting_variables` is only ever inserted into (`binder.rs`) and point-queried in `check_redeclaration` (`builder.rs`) — it is never iterated, so a flat `(scope, name)` key is a drop-in. ## Results — `cargo allocs` (system allocations) | fixture | phase | before | after | | --- | --- | ---: | ---: | | antd.js | semantic | 1020 | **28** (−97%) | | antd.js | minifier | 3041 | **1040** (−66%, rebuilds semantic internally) | | pdf.mjs | semantic | 25 | 16 | | kitchen-sink.tsx | semantic | 41 | 36 | `allocs_semantic.snap` / `allocs_minifier.snap` are updated accordingly. This is on the hot path for tools that run `SemanticBuilder` repeatedly per module (e.g. Rolldown). ## Relation to #19134 #19134 previously tried to move this temporary state (including `hoisting_variables`) to a scratch arena; CodSpeed reported a ~3.6% regression and it was closed. This PR takes a different, minimal approach: it does **not** introduce an arena, does **not** change `SemanticBuilder::new`'s signature, and touches no call sites. It removes a hash and an allocation per hoisted variable (rather than adding arena threading), so it should be CodSpeed-neutral-or-better — please let CI confirm. ## Tests - `cargo test -p oxc_semantic --lib --tests` — 60 passed (incl. redeclaration / var-hoist tests) - `cargo test -p oxc_minifier --lib --tests` — 538 passed ## AI disclosure Prepared with AI assistance (Claude). I reviewed the change, located the hotspot with a per-call-site allocation profile, verified the reduction with `cargo allocs`, and ran the test suites above.
camc314
added a commit
that referenced
this pull request
Jul 3, 2026
### 💥 BREAKING CHANGES - 94fbacb ast: [**BREAKING**] Only export `AstBuilder` and `NONE` in `builder` module (#23876) (overlookmotel) - 8de5122 ecmascript: [**BREAKING**] Switch to new `AstBuilder` (#23834) (overlookmotel) - dc0ef38 transformer: [**BREAKING**] Switch to new `AstBuilder` (#23831) (overlookmotel) - 88f4455 str: [**BREAKING**] `Str` and `Ident` methods take `&GetAllocator` (#23781) (overlookmotel) - 36009dd allocator: [**BREAKING**] `GetAllocator::allocator` take `&self` (#23676) (overlookmotel) - bd74f9d allocator: [**BREAKING**] Rename `AllocatorAccessor` trait to `GetAllocator` (#23675) (overlookmotel) ### 🚀 Features - 326fe25 transformer_plugins: Support `typeof` `define` keys (#23605) (Alexander Lichter) - f2091b3 ast: Unify old and new `AstBuilder`s (#23875) (overlookmotel) - cd1fd12 codegen: Expose `Codegen::print_string` API (#23785) (camc314) - 785461b ast: Add custom builder methods to AST types (#23651) (overlookmotel) - 05d1357 ast: Add AST creation methods to AST types (#23650) (overlookmotel) - 2580eda str: Add `Str::from_str_in` and `Ident::from_str_in` methods (#23767) (overlookmotel) - 6883fcf minifier: Fold write-once falsy var to false in boolean context (#23540) (Dunqing) - fcbf993 allocator: Add `Vec::from_value_in` method (#23718) (overlookmotel) - 989ddb7 allocator: Add `Vec::from_box_in` method (#23717) (overlookmotel) - 9d1aa7f allocator: Improve `PartialEq` for `Vec` (#23716) (overlookmotel) ### 🐛 Bug Fixes - da0e5bf minifier: Don't reorder a closed-over TDZ read when inlining a var (#23771) (Dunqing) - 0b3021f allocator: Remove `Vec::from_box_in` (#23873) (overlookmotel) - 0ab64ec ast: Silence deprecation warnings within files defining deprecated `AstBuilder` methods (#23889) (overlookmotel) - 8c07cad all: Enable `disable_old_builder` Cargo feature for `oxc_ast` crate in tests (#23888) (overlookmotel) - 3800f01 ast: Legacy `AstBuilder` methods take `self` not `&self` (#23891) (overlookmotel) - 869ac20 semantic/cfg: Connect for update exit to loop test (#23791) (camc314) - d3e92d5 semantic/cfg: Connect while branches from condition exit (#23790) (camc314) - 025045d ast: `ExportNamedDeclaration` plain builder methods return boxed nodes (#23783) (overlookmotel) - 7537c58 ast: Fix name of `AstBuilder` method for `Expression::V8IntrinsicExpression` (#23766) (overlookmotel) - 3f574f5 traverse: Fix unsoundness in `Traverse` walk functions (#23745) (overlookmotel) - 585760f parser: String in AST reference arena (#23721) (overlookmotel) - 7231d55 allocator: Fix unsound lifetime extension in `Box::new_in` (#23685) (overlookmotel) ### ⚡ Performance - d5c916a semantic: Flatten hoisting_variables to avoid per-scope map allocation (#23927) (Lawrence Lin) - e71609d minifier: Bail member-expr folding before the side-effect walk (#23924) (Lawrence Lin) - e1f89ab minifier: Reduce string allocations folding addition (#23846) (overlookmotel) - 9f6ee3b isolated-declarations: Pool scope maps to avoid per-scope alloc/rehash (#23761) (Boshen) - 0b07c4c semantic: Avoid heap alloc for catch-clause binding ids (#23911) (Lawrence Lin) - c5eef8b regular_expression: Skip capturing-group pre-parse when pattern has no `(` (#23908) (Lawrence Lin) - b4f5b4b isolated_declarations: Remove redundant clone of formal parameter pattern (#23912) (Lawrence Lin) - 53d083f isolated_declarations: Use `TakeIn` not `CloneIn` (#23847) (overlookmotel) - 3ea9304 react_compiler: Use faster API to arena allocate strings (#23849) (overlookmotel) - a6d8e45 parser: Avoid span lookup for arrow expression body (#23788) (camc314) - e1886a0 transformer, minifier: Use `static_ident!` macro to create static `Ident`s (#23727) (overlookmotel) - 5527bef transformer/object-rest-spread: Reduce iteration (#23720) (overlookmotel) - 680ffbc transformer: Allocate AST nodes in arena directly (#23711) (overlookmotel) - 1c63c66 parser: Allocate AST nodes in arena directly (#23712) (overlookmotel) - 3855f0c minifier: Allocate AST nodes in arena directly (#23710) (overlookmotel) - d025887 isolated_declarations: Allocate AST nodes in arena directly (#23709) (overlookmotel) - 10b96c6 parser: Remove string search from parsing JSX element name (#23713) (overlookmotel) ### 📚 Documentation - 3d61dea all: Correct capitalization in comments (#23887) (overlookmotel) - aa1ad74 ast: Add `#[deprecated]` to legacy `AstBuilder` methods (#23877) (overlookmotel) - a4676db ast: Correct doc comment for `NONE` (#23765) (overlookmotel) - 419ec80 syntax: Fix typo in doc comment (#23674) (overlookmotel) ### 🛡️ Security - 3cdd18f deps: Update npm packages (#23690) (renovate[bot]) Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com> Co-authored-by: Cameron <cameron.clark@hey.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SemanticBuilder::hoisting_variablesis a nested mapFxHashMap<ScopeId, IdentHashMap<'a, SymbolId>>. Eachentry(scope_id).or_default()on a vacant scope heap-allocates a fresh inner
IdentHashMapvia the systemallocator, and subsequent inserts grow it. On
antd.jsthis is the single largestsystem-allocation site in semantic analysis (
VariableDeclarator::bind/Function::bindinbinder.rs).This flattens it to a single
FxHashMap<(ScopeId, Ident<'a>), SymbolId>:allocation → inner probe);
Behaviour is identical:
hoisting_variablesis only ever inserted into (binder.rs)and point-queried in
check_redeclaration(builder.rs) — it is never iterated, so aflat
(scope, name)key is a drop-in.Results —
cargo allocs(system allocations)allocs_semantic.snap/allocs_minifier.snapare updated accordingly. This is on thehot path for tools that run
SemanticBuilderrepeatedly per module (e.g. Rolldown).Relation to #19134
#19134 previously tried to move this temporary state (including
hoisting_variables)to a scratch arena; CodSpeed reported a ~3.6% regression and it was closed. This PR
takes a different, minimal approach: it does not introduce an arena, does not
change
SemanticBuilder::new's signature, and touches no call sites. It removes a hashand an allocation per hoisted variable (rather than adding arena threading), so it
should be CodSpeed-neutral-or-better — please let CI confirm.
Tests
cargo test -p oxc_semantic --lib --tests— 60 passed (incl. redeclaration / var-hoist tests)cargo test -p oxc_minifier --lib --tests— 538 passedAI disclosure
Prepared with AI assistance (Claude). I reviewed the change, located the hotspot with a
per-call-site allocation profile, verified the reduction with
cargo allocs, and ranthe test suites above.