Skip to content

perf(minifier): bail member-expr folding before the side-effect walk#23924

Merged
Dunqing merged 1 commit into
oxc-project:mainfrom
linyiru:perf/minifier-member-fold-order
Jun 29, 2026
Merged

perf(minifier): bail member-expr folding before the side-effect walk#23924
Dunqing merged 1 commit into
oxc-project:mainfrom
linyiru:perf/minifier-member-fold-order

Conversation

@linyiru

@linyiru linyiru commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

What

fold_static_member_expr and fold_computed_member_expr ran the recursive may_have_side_effects(object) check on every member access, before calling evaluate_value. But evaluate_value can only fold a narrow set of member accesses — currently .length on a constant string / non-spread array literal — and bails cheaply otherwise.

This reorders the two functions so the cheap evaluate_value gates the side-effect walk:

let Some(value) = e.evaluate_value(ctx) else { return };
if e.object.may_have_side_effects(ctx) { return; }
let changed = ctx.value_to_expr(e.span, value);
ctx.replace_expression(expr, changed);

Using evaluate_value itself as the gate (rather than hard-coding the .length check) keeps it in sync if more foldable member patterns are added later.

Why it's behavior-preserving

Folding happens iff evaluate_value is Some and the object has no side effects — true in both the old and new ordering. evaluate_value is a pure read-only query (no mutation, no diagnostics), so evaluating it before the side-effect check changes nothing observable. All 538 oxc_minifier tests pass; cargo fmt --check clean; no new clippy warnings.

Impact

In real code, .length folding on a constant is virtually nonexistent, so almost every member-fold attempt previously paid for a wasted may_have_side_effects(object) walk. Measured deterministically (temporary instrumentation counting calls) on cached benchmark files, the share of fold attempts that now skip the object side-effect walk:

file fold attempts skipped side-effect walks of which member-chain (recursive)
react.development.js 1,078 1,078 (100%) 65
App.tsx 19,272 19,271 5,566
kitchen-sink.tsx 59,431 59,426 8,954
antd.js 173,257 173,257 (100%) 12,952

The biggest savings are on member-expression chains (e.g. a.b.c.prop), where the skipped may_have_side_effects walk recurses over the whole chain. I'm relying on CodSpeed CI for the wall-clock delta — my local machine is too noisy for a trustworthy criterion measurement.

AI usage disclosure

This change was developed with assistance from an AI tool (Claude). I (the contributor) reviewed, tested, and validated it: I verified the behavior-equivalence argument, confirmed the full oxc_minifier test suite passes, and produced the work-reduction measurements above.

`fold_static_member_expr` / `fold_computed_member_expr` ran the recursive
`may_have_side_effects(object)` check on *every* member access, even though
`evaluate_value` can only fold a narrow set (`.length` on constant
strings/arrays) and bails cheaply otherwise.

Reorder so the cheap `evaluate_value` gates the side-effect walk. Folding
still only happens when `evaluate_value` is `Some` and the object has no
side effects, so behavior is unchanged (`evaluate_value` is a pure
read-only query). Using `evaluate_value` itself as the gate (rather than
hard-coding the `.length` check) keeps it in sync if more foldable member
patterns are added later.

In practice ~100% of member-fold attempts bail at `evaluate_value`
(`.length` folding is virtually nonexistent in real code), so the
object's side-effect walk is now skipped for essentially all of them —
e.g. one minify pass of antd.js avoids ~173k `may_have_side_effects`
calls, ~13k of which recurse over member chains.
@codspeed-hq

codspeed-hq Bot commented Jun 29, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 4.66%

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

⚡ 3 improved benchmarks
✅ 49 untouched benchmarks
⏩ 19 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation minifier[App.tsx] 12.2 ms 11.4 ms +6.85%
Simulation minifier[react.development.js] 2.4 ms 2.4 ms +4.03%
Simulation minifier[kitchen-sink.tsx] 53 ms 51.4 ms +3.15%

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 linyiru:perf/minifier-member-fold-order (1c599c7) with main (dbf2719)

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.

@linyiru linyiru marked this pull request as ready for review June 29, 2026 05:03
@linyiru linyiru requested a review from Dunqing as a code owner June 29, 2026 05:03

@Dunqing Dunqing 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.

Nice improvements!

@Dunqing Dunqing merged commit e71609d into oxc-project:main Jun 29, 2026
36 checks passed
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
…23924)

## What

`fold_static_member_expr` and `fold_computed_member_expr` ran the
recursive `may_have_side_effects(object)` check on **every** member
access, before calling `evaluate_value`. But `evaluate_value` can only
fold a narrow set of member accesses — currently `.length` on a constant
string / non-spread array literal — and bails cheaply otherwise.

This reorders the two functions so the cheap `evaluate_value` gates the
side-effect walk:

```rust
let Some(value) = e.evaluate_value(ctx) else { return };
if e.object.may_have_side_effects(ctx) { return; }
let changed = ctx.value_to_expr(e.span, value);
ctx.replace_expression(expr, changed);
```

Using `evaluate_value` itself as the gate (rather than hard-coding the
`.length` check) keeps it in sync if more foldable member patterns are
added later.

## Why it's behavior-preserving

Folding happens iff `evaluate_value` is `Some` **and** the object has no
side effects — true in both the old and new ordering. `evaluate_value`
is a pure read-only query (no mutation, no diagnostics), so evaluating
it before the side-effect check changes nothing observable. All 538
`oxc_minifier` tests pass; `cargo fmt --check` clean; no new clippy
warnings.

## Impact

In real code, `.length` folding on a constant is virtually nonexistent,
so almost every member-fold attempt previously paid for a wasted
`may_have_side_effects(object)` walk. Measured deterministically
(temporary instrumentation counting calls) on cached benchmark files,
the share of fold attempts that now skip the object side-effect walk:

| file | fold attempts | skipped side-effect walks | of which
member-chain (recursive) |
|---|---|---|---|
| react.development.js | 1,078 | 1,078 (100%) | 65 |
| App.tsx | 19,272 | 19,271 | 5,566 |
| kitchen-sink.tsx | 59,431 | 59,426 | 8,954 |
| antd.js | 173,257 | 173,257 (100%) | 12,952 |

The biggest savings are on member-expression chains (e.g. `a.b.c.prop`),
where the skipped `may_have_side_effects` walk recurses over the whole
chain. I'm relying on CodSpeed CI for the wall-clock delta — my local
machine is too noisy for a trustworthy criterion measurement.

## AI usage disclosure

This change was developed with assistance from an AI tool (Claude). I
(the contributor) reviewed, tested, and validated it: I verified the
behavior-equivalence argument, confirmed the full `oxc_minifier` test
suite passes, and produced the work-reduction measurements 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>
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.

2 participants