feat(minifier): fold boolean-literal ternary branches in value contexts#24110
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
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. |
3260236 to
cf589f0
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Monitor OxcCommit:
|
Merge activity
|
…ts (#24110) ## What Fold a ternary with one boolean literal branch into `&&` / `||`, in value positions (call arguments, returns, assignments). | Input | Output | Condition | |---|---|---| | `c ? false : x` | `!c && x` | always safe | | `c ? x : true` | `!c \|\| x` | always safe | | `c ? true : x` | `c \|\| x` | only when `c` is a boolean | | `c ? x : false` | `c && x` | only when `c` is a boolean | We already do this inside `if` tests. This PR does it everywhere else. The first two rows produce the exact same value for any `c`. The last two rows would return `c` itself instead of `true`/`false`, so they need a boolean-typed `c`. ## How New match arms in `minimize_conditional_expression`, next to the existing `a ? true : false` => `!!a` block. The dropped branch must be side-effect free (same check the existing block uses). The fold is skipped when it would make the output longer, e.g. `a || b ? false : c` would need `!(a || b)`, so it stays as is. Equality tests invert for free: `a === b ? false : x` => `a !== b && x`. ## Example ```js // input use(foo() ? false : bar()); use(a === b ? false : compute()); ``` ```js // before use(foo()?!1:bar()),use(a===b?!1:compute()); // after use(!foo()&&bar()),use(a!==b&&compute()); ``` Found by re-minifying oxc's minsize output with `terser -c`: typescript.js alone has ~290 of these. All minsize fixtures stay equal or smaller. Implemented with Claude Code, reviewed and verified by me.
cf589f0 to
2d9b0b3
Compare
### 🚀 Features - 260425f semantic/examples: Include unresolved references (#24214) (camc314) - 2d9b0b3 minifier: Fold boolean-literal ternary branches in value contexts (#24110) (Dunqing) - 61fbf10 ast: Implement `ReplaceWith` on all AST types (#24013) (overlookmotel) - 7db7a29 allocator: Add `ReplaceWith` trait (#24012) (overlookmotel) - 4eb074e mangler: Add `reserved` option for names that must not be mangled (#24041) (Dunqing) - 2e62012 data_structures: Add `StringExt` trait (#24006) (overlookmotel) - 60e7160 minifier: Drop side-effect-free IIFEs whose result is unused (#23967) (Dunqing) - 26dd9e2 ast: Add method to widen inherited enum ref to parent ref (#23961) (overlookmotel) ### 🐛 Bug Fixes - e8b50ee transformer: Clean up semantics for stripped TypeScript syntax (#24180) (camc314) - d966d0b react_compiler: Remove clippy allows (#24168) (Boshen) - 854ef8d react_compiler: Compile generic functions instead of over-bailing on type-param hoisting (#24158) (Boshen) - 093586c react_compiler: Align memoization cache-slot allocation with Babel (#24157) (Boshen) - 09c8f59 react_compiler: Normalize snapshot fixture paths (#24142) (camc314) - f13df97 react_compiler: Drop stray empty statement from catch bindings (#24133) (Boshen) - cb2a505 react_compiler: Codegen destructuring reassignment targets (#24131) (Boshen) - b82c394 react_compiler: Propagate codegen invariants instead of emitting empty bodies (#24128) (Boshen) - 5771982 react_compiler: Render unchanged programs as source in fixture snapshots (#24129) (Boshen) - 4b16e1a transformer/async-to-generator: Preserve direct eval scope flags (#24136) (camc314) - 4e9194f react_compiler: Lower `delete obj.prop` to Property/ComputedDelete (#24123) (Boshen) - 0b25582 ast: Type binding node `typeAnnotation` as `TSTypeAnnotation | null` (#23113) (Boshen) - 018c0e5 transformer: Hoist lowered async declarations (#22770) (camc314) - 652fbaf mangler: Keep names of destructured exported bindings (#24036) (Dunqing) - e274415 minifier: Don't drop global calls that throw despite pure arguments (#23917) (Dunqing) - 59abb30 minifier: Only merge string literals in `try_fold_add` when the inner operator is `+` (#23622) (Jerry Zhao) ### ⚡ Performance - c5ca77b transformer: Avoid cloning refresh options (#24191) (camc314) - bf1a151 react_compiler: Compile out debug printers (#24184) (Boshen) - abb44a0 transformer: Build fixed object-rest arguments (#24190) (camc314) - a4db731 isolated_declarations: Use `ReplaceWith` instead of `TakeIn` (#24016) (overlookmotel) - ff10855 transformer: Use `ReplaceWith` instead of `TakeIn` (#24015) (overlookmotel) - bd49aff ecmascript: Avoid heap-allocating Math.min/max/imul operands (#23941) (Lawrence Lin) - e4b708b react_compiler: Skip compiled files before prefilters (#24171) (Boshen) - c59f2fe rust: Return impl ExactSizeIterator from slice-backed accessors (#24144) (Boshen) - 5d6d04a codegen: SWAR-skip boring byte runs in sourcemap line/column scan (#24023) (Boshen) - a55e0be traverse: Reduce string operations in `get_var_name_from_node` (#24007) (overlookmotel) - e6d48e1 transformer/nullish_coalescing: Move cold path into separate function (#23989) (overlookmotel) - c4e35b5 transformer/object_rest_spread: Pre-allocate capacity in `Vec` (#23988) (overlookmotel) - 527b8e5 transformer/decorators: Narrow type earlier (#23987) (overlookmotel) ### 📚 Documentation - 30d17f5 allocator: Clarify docs for `TakeIn::take_in_box` (#24093) (overlookmotel) - 675e6a8 ast: Correct doc comment for `PrivateFieldExpression` (#24008) (overlookmotel) - e4c30e6 minifier: Explain what `dce` mode means (#23994) (Dunqing) - 37cbf88 ast_macros: Document fields of `StructDetails` (#23959) (overlookmotel) - 4de3e54 ast: Correct doc comment (#23948) (overlookmotel) Co-authored-by: Boshen <[email protected]>

What
Fold a ternary with one boolean literal branch into
&&/||, in value positions (call arguments, returns, assignments).c ? false : x!c && xc ? x : true!c || xc ? true : xc || xcis a booleanc ? x : falsec && xcis a booleanWe already do this inside
iftests. This PR does it everywhere else. The first two rows produce the exact same value for anyc. The last two rows would returncitself instead oftrue/false, so they need a boolean-typedc.How
New match arms in
minimize_conditional_expression, next to the existinga ? true : false=>!!ablock. The dropped branch must be side-effect free (same check the existing block uses). The fold is skipped when it would make the output longer, e.g.a || b ? false : cwould need!(a || b), so it stays as is. Equality tests invert for free:a === b ? false : x=>a !== b && x.Example
Found by re-minifying oxc's minsize output with
terser -c: typescript.js alone has ~290 of these. All minsize fixtures stay equal or smaller.Implemented with Claude Code, reviewed and verified by me.