feat(minifier): apply De Morgan's law to negated comparison chains in jump guards and loop tests#24279
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via 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. |
Merging this PR will not alter performance
Comparing Footnotes
|
Monitor OxcCommit:
|
|
@copilot resolve the merge conflicts in this pull request |
Resolved in ff252f8. |
Merge activity
|
… jump guards and loop tests (#24279) ### What Terser rewrites `!(a==b||c==d)` into `a!=b&&c!=d`. oxc only removed the `!` when a later pass could consume it for free, such as swapping if-else branches. In guards that end in a jump (`throw`, `return`, `break`) and in loop tests there is nothing to swap, so the negation stayed in the output. ### How `minimize_unary` gets a `LogicalExpression` arm that distributes the `!` over `&&`/`||` chains. It only fires when every comparison in the chain can invert its operator in place (`==`, `!=`, `===`, `!==`; relational operators are excluded because `!(a<b)` is not `a>=b` under NaN) and when flipping `&&`/`||` does not add parentheses. Under those rules the fold always saves bytes and is its own inverse, so shapes that re-negate the test later (branch swaps, `!!` collapses) still reach their old output. DCE mode is untouched; `minimize_unary` only runs in full minify. ### Example Before: ```js export function f(e,t,n,r){if(!(e==t||n==r))throw Error(`x`)}export function g2(e,t,n,r){for(;!(e==t||n==r);)h()} ``` After: ```js export function f(e,t,n,r){if(e!=t&&n!=r)throw Error(`x`)}export function g2(e,t,n,r){for(;e!=t&&n!=r;)h()} ``` ### Size jquery, d3, three, victory, echarts, typescript, react and bundle.min each shrink by 0.01–0.02 kB minified; no fixture grows. jquery's gzip column ticks up 0.01 kB even though its raw output shrank — a compression artifact of an equal-or-smaller input. Arena allocation counts rise slightly (+1 to +11 per fixture) from a transient `!` wrapper that re-negation builds and immediately unwraps.
ff252f8 to
b79eef7
Compare
### 🚀 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]>

What
Terser rewrites
!(a==b||c==d)intoa!=b&&c!=d. oxc only removed the!when a later pass could consume it for free, such as swapping if-else branches. In guards that end in a jump (throw,return,break) and in loop tests there is nothing to swap, so the negation stayed in the output.How
minimize_unarygets aLogicalExpressionarm that distributes the!over&&/||chains. It only fires when every comparison in the chain can invert its operator in place (==,!=,===,!==; relational operators are excluded because!(a<b)is nota>=bunder NaN) and when flipping&&/||does not add parentheses. Under those rules the fold always saves bytes and is its own inverse, so shapes that re-negate the test later (branch swaps,!!collapses) still reach their old output.DCE mode is untouched;
minimize_unaryonly runs in full minify.Example
Before:
After:
Size
jquery, d3, three, victory, echarts, typescript, react and bundle.min each shrink by 0.01–0.02 kB minified; no fixture grows. jquery's gzip column ticks up 0.01 kB even though its raw output shrank — a compression artifact of an equal-or-smaller input. Arena allocation counts rise slightly (+1 to +11 per fixture) from a transient
!wrapper that re-negation builds and immediately unwraps.