Skip to content

feat(minifier): drop write-only property assignments to unused local bindings by default#24112

Merged
graphite-app[bot] merged 1 commit into
mainfrom
minifier-write-only-prop-dce
Jul 8, 2026
Merged

feat(minifier): drop write-only property assignments to unused local bindings by default#24112
graphite-app[bot] merged 1 commit into
mainfrom
minifier-write-only-prop-dce

Conversation

@Dunqing

@Dunqing Dunqing commented Jul 3, 2026

Copy link
Copy Markdown
Member

What

Drop o.x = value when o is a local variable that is never read — by default. Until now this only happened with the treeshake.property_write_side_effects: false opt-in. terser does this by default; esbuild does not do it at all.

This matters because one leftover write keeps a lot of code alive. antd bundles 820 icon modules that each end with o.displayName = 'UserOutlined'; that single write kept each module's whole require-chain alive. antd.js: 2.22 MB -> 2.13 MB minified (458 kB -> 455 kB gzip). oxc now beats esbuild on antd by ~180 kB.

How

The drop itself uses machinery that already exists for the opt-in path (is_member_assign_to_unused_binding: the object must be a fresh value — function/class/object/array literal — not exported, never written as a whole, and every read of it must be the target of a property write).

What is new is making it safe without the opt-in:

  • A program-wide fact table (PersistentSymbolFacts, bit SymbolFact::MEMBER_WRITE_HAZARD) is filled once by Normalize, before the compress loop. The container is generic on purpose: monotone symbol facts, seeded pre-loop, never cleared — a stale set bit only forgoes an optimization. A follow-up migrates proto_write_symbols into it as PROTO_WRITTEN, fixing that field's execution-order TODO. A symbol goes in when any of its property writes could be observed: read-modify ops (o.x += 1 reads the property), chained writes (a.b.c = 1 reads a.b, so dropping a.b = {} would throw), and __proto__ or computed keys (may install setters). A hazarded symbol never gets its writes dropped.
  • The set also grows mid-loop when a pass rewrites o.x = o.x + 1 into o.x += 1.
  • Only plain = with a single-level, non-__proto__ key qualifies. o[k()] = v bails.
  • A side-effectful right side is kept: o.x = impure() becomes impure().

Once the write is gone, the variable is unused and the existing passes delete the declaration and everything only it referenced.

Behavior change

The only assumption this makes (documented in docs/ASSUMPTIONS.md): setters installed on Object.prototype / Function.prototype / Array.prototype are not observed by these writes — the same assumption the existing opt-in flag makes, and terser's default.

A 98-case audit (node ground truth vs oxc vs terser, strict and sloppy) hardened everything else. Writes that throw or have exotic effects are now kept even though the binding is unused, via a kind-aware key denylist: caller/arguments/name/length on function- and class-valued bindings, prototype on class-valued (class prototypes are non-writable; plain function prototype writes still drop — they are writable and a real size win), length on array-valued (the array length setter coerces its RHS and can throw RangeError), private-field writes (brand-check TypeError), and classes with static blocks, decorators, or static accessors are not treated as fresh. Post-audit the matrix shows zero unsound cases beyond the documented prototype-setter class — stricter than terser, which for example still drops a.length = -1 (losing the RangeError) where oxc now keeps it. No size cost: the minsize corpus is byte-identical, antd keeps the full 90 kB win.

Example

// input
(function() {
  var r = require('react');
  var i = require('./asn/UserOutlined');
  var o = function(e, t) { return r.createElement(i.a, e, t); };
  o.displayName = 'UserOutlined';
})();
// before (everything kept)
(function(){var e=require("react"),t=require("./asn/UserOutlined"),n=function(n,r){return e.createElement(t.a,n,r)};n.displayName=`UserOutlined`})();

// after (only the side effects remain)
(function(){require("react"),require("./asn/UserOutlined")})();

Found by re-minifying oxc's minsize output with terser -c; this was the single largest gap between oxc and terser. Full conformance suite passes with zero snapshot changes; minsize stays a fixed point (antd takes one extra compress iteration).

Implemented with Claude Code, reviewed and verified by me.

@github-actions github-actions Bot added the A-minifier Area - Minifier label Jul 3, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 3, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 9 skipped benchmarks1


Comparing minifier-write-only-prop-dce (144288b) with main (b521b9b)

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.

@Dunqing
Dunqing changed the base branch from main to graphite-base/24112 July 3, 2026 09:20
@Dunqing
Dunqing force-pushed the minifier-write-only-prop-dce branch from 035a3cb to ae445d3 Compare July 3, 2026 09:20
@Dunqing
Dunqing changed the base branch from graphite-base/24112 to minifier-guard-inversion July 3, 2026 09:20

Dunqing commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of the merge queue

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@Dunqing Dunqing added the run-monitor-oxc Add to a PR to dispatch oxc-project/monitor-oxc CI against it label Jul 3, 2026
@oxc-guard

oxc-guard Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

@oxc-guard oxc-guard Bot removed the run-monitor-oxc Add to a PR to dispatch oxc-project/monitor-oxc CI against it label Jul 3, 2026
@Dunqing
Dunqing force-pushed the minifier-guard-inversion branch from abad106 to caca822 Compare July 3, 2026 09:56
@Dunqing
Dunqing force-pushed the minifier-write-only-prop-dce branch from ae445d3 to 9621c74 Compare July 3, 2026 09:56
@Dunqing
Dunqing force-pushed the minifier-guard-inversion branch from caca822 to a77d6c4 Compare July 6, 2026 07:36
@Dunqing
Dunqing force-pushed the minifier-write-only-prop-dce branch from 9621c74 to 45a99cb Compare July 6, 2026 07:36
@graphite-app
graphite-app Bot force-pushed the minifier-guard-inversion branch 2 times, most recently from 9e679b3 to f39d74e Compare July 6, 2026 08:31
@graphite-app
graphite-app Bot force-pushed the minifier-write-only-prop-dce branch from 45a99cb to d7c31b4 Compare July 6, 2026 08:31
@Dunqing
Dunqing changed the base branch from minifier-guard-inversion to graphite-base/24112 July 6, 2026 08:49
@Dunqing
Dunqing force-pushed the minifier-write-only-prop-dce branch from d7c31b4 to d59bcc1 Compare July 6, 2026 08:49
@Dunqing
Dunqing force-pushed the graphite-base/24112 branch from f39d74e to 2d9b0b3 Compare July 6, 2026 08:49
@Dunqing
Dunqing changed the base branch from graphite-base/24112 to main July 6, 2026 08:49
@Dunqing Dunqing added the run-monitor-oxc Add to a PR to dispatch oxc-project/monitor-oxc CI against it label Jul 7, 2026
@oxc-guard oxc-guard Bot removed the run-monitor-oxc Add to a PR to dispatch oxc-project/monitor-oxc CI against it label Jul 7, 2026
@Dunqing
Dunqing force-pushed the minifier-write-only-prop-dce branch 3 times, most recently from 3e1637a to e5478f8 Compare July 7, 2026 03:06
@Dunqing Dunqing added the run-monitor-oxc Add to a PR to dispatch oxc-project/monitor-oxc CI against it label Jul 7, 2026
@oxc-guard oxc-guard Bot removed the run-monitor-oxc Add to a PR to dispatch oxc-project/monitor-oxc CI against it label Jul 7, 2026
@Dunqing
Dunqing force-pushed the minifier-write-only-prop-dce branch 3 times, most recently from b1c8859 to 144288b Compare July 7, 2026 14:58
@Dunqing
Dunqing marked this pull request as ready for review July 8, 2026 02:25
@Dunqing
Dunqing requested a review from overlookmotel as a code owner July 8, 2026 02:25
@graphite-app graphite-app Bot added the 0-merge Merge with Graphite Merge Queue label Jul 8, 2026
@graphite-app

graphite-app Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Merge activity

…bindings by default (#24112)

## What

Drop `o.x = value` when `o` is a local variable that is never read — by default. Until now this only happened with the `treeshake.property_write_side_effects: false` opt-in. terser does this by default; esbuild does not do it at all.

This matters because one leftover write keeps a lot of code alive. antd bundles 820 icon modules that each end with `o.displayName = 'UserOutlined'`; that single write kept each module's whole require-chain alive. **antd.js: 2.22 MB -> 2.13 MB minified** (458 kB -> 455 kB gzip). oxc now beats esbuild on antd by ~180 kB.

## How

The drop itself uses machinery that already exists for the opt-in path (`is_member_assign_to_unused_binding`: the object must be a fresh value — function/class/object/array literal — not exported, never written as a whole, and every read of it must be the target of a property write).

What is new is making it safe without the opt-in:

- A program-wide fact table (`PersistentSymbolFacts`, bit `SymbolFact::MEMBER_WRITE_HAZARD`) is filled once by `Normalize`, before the compress loop. The container is generic on purpose: monotone symbol facts, seeded pre-loop, never cleared — a stale set bit only forgoes an optimization. A follow-up migrates `proto_write_symbols` into it as `PROTO_WRITTEN`, fixing that field's execution-order TODO. A symbol goes in when any of its property writes could be observed: read-modify ops (`o.x += 1` reads the property), chained writes (`a.b.c = 1` reads `a.b`, so dropping `a.b = {}` would throw), and `__proto__` or computed keys (may install setters). A hazarded symbol never gets its writes dropped.
- The set also grows mid-loop when a pass rewrites `o.x = o.x + 1` into `o.x += 1`.
- Only plain `=` with a single-level, non-`__proto__` key qualifies. `o[k()] = v` bails.
- A side-effectful right side is kept: `o.x = impure()` becomes `impure()`.

Once the write is gone, the variable is unused and the existing passes delete the declaration and everything only it referenced.

## Behavior change

The only assumption this makes (documented in `docs/ASSUMPTIONS.md`): setters installed on `Object.prototype` / `Function.prototype` / `Array.prototype` are not observed by these writes — the same assumption the existing opt-in flag makes, and terser's default.

A 98-case audit (node ground truth vs oxc vs terser, strict and sloppy) hardened everything else. Writes that throw or have exotic effects are now kept even though the binding is unused, via a kind-aware key denylist: `caller`/`arguments`/`name`/`length` on function- and class-valued bindings, `prototype` on class-valued (class prototypes are non-writable; plain function `prototype` writes still drop — they are writable and a real size win), `length` on array-valued (the array length setter coerces its RHS and can throw RangeError), private-field writes (brand-check TypeError), and classes with static blocks, decorators, or static accessors are not treated as fresh. Post-audit the matrix shows zero unsound cases beyond the documented prototype-setter class — stricter than terser, which for example still drops `a.length = -1` (losing the RangeError) where oxc now keeps it. No size cost: the minsize corpus is byte-identical, antd keeps the full 90 kB win.

## Example

```js
// input
(function() {
  var r = require('react');
  var i = require('./asn/UserOutlined');
  var o = function(e, t) { return r.createElement(i.a, e, t); };
  o.displayName = 'UserOutlined';
})();
```

```js
// before (everything kept)
(function(){var e=require("react"),t=require("./asn/UserOutlined"),n=function(n,r){return e.createElement(t.a,n,r)};n.displayName=`UserOutlined`})();

// after (only the side effects remain)
(function(){require("react"),require("./asn/UserOutlined")})();
```

Found by re-minifying oxc's minsize output with `terser -c`; this was the single largest gap between oxc and terser. Full conformance suite passes with zero snapshot changes; minsize stays a fixed point (antd takes one extra compress iteration).

Implemented with Claude Code, reviewed and verified by me.
@graphite-app
graphite-app Bot force-pushed the minifier-write-only-prop-dce branch from 144288b to 34ff7b4 Compare July 8, 2026 02:28
@graphite-app
graphite-app Bot merged commit 34ff7b4 into main Jul 8, 2026
30 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label Jul 8, 2026
@graphite-app
graphite-app Bot deleted the minifier-write-only-prop-dce branch July 8, 2026 02:32
graphite-app Bot pushed a commit that referenced this pull request Jul 8, 2026
…24292)

## Issue

The `Allocations` CI job can fail on PRs with a 1-allocation `Sys allocs` difference that local `cargo allocs` cannot reproduce. #22621 fixed one instance of this in the transformer stage. #24112 and #24017 hit the same failure again in the minifier stage: `kitchen-sink.tsx` measures 2309 Sys allocs on macOS/arm64 but 2310 on Linux/x64, so whichever platform regenerates the snapshot last breaks CI for the other.

## Why

The tracker counts every call to the system allocator. That includes the chunks bump arenas request. Whether an arena needs one more chunk depends on the total number of bytes in the arena, and byte totals are not the same on every target. For example, hashbrown tables use 16-byte control groups on x86_64 but 8-byte groups on aarch64. When an arena's content size sits close to a chunk boundary, one target crosses it and the other does not.

In the observed failure, the minifier's mangler-phase semantic build creates a fresh `Scoping`. Its arena holds symbol names, resolved-reference vectors, and the per-scope `bindings` hash tables, which are slightly larger on x86_64. The traced Linux-only allocation happened under `Scoping::add_resolved_reference -> ArenaVec::push -> Arena::new_chunk`, when the push overflowed the current chunk and requested a new 720880-byte chunk. On aarch64 the same content fit in the existing chunk, so the count stayed one lower.

Every other allocation class in the measurement grows on element counts, which are identical across platforms. Logging every allocation size during the minifier stage on macOS/arm64, Linux/arm64, and Linux/x64 showed the three sequences match one-to-one except for arena chunk requests.

Any PR that shifts arena content near a chunk boundary can trip this again in any stage, so fixing one stage at a time does not close the class.

## Fix

Count chunk allocations in `oxc_allocator` under the tracker-only `track_allocations` feature, and subtract them from the reported Sys allocs in the tracker. The counter is global rather than per-arena because short-lived arenas (like the one backing `Scoping`) are created and dropped inside the measured operation.

Arena usage is still measured by the `Arena allocs` / `Arena reallocs` columns; only the platform-sensitive chunk-request count is excluded from `Sys allocs`.

Parser and transformer rows are unchanged (the main arena is pre-warmed, and #22621 already removed transformer scoping growth). Semantic, minifier, and formatter rows drop by their per-stage chunk counts.

## Validation

`cargo allocs` produces byte-identical snapshots on macOS/arm64, Linux/arm64 (Docker), and Linux/x64 (Docker), and is idempotent across reruns. `cargo test -p oxc_allocator` passes; clippy is clean with `track_allocations` both on and off, and the counter compiles out entirely without the feature.

AI usage: AI assistance was used to investigate, implement, and validate this change.
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-minifier Area - Minifier

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant