Skip to content

fix(minifier): cap if-return ternary collapse for firefox#21841

Merged
Boshen merged 7 commits into
oxc-project:mainfrom
guru-irl:user/guru-irl/minifier-conditional-depth-cap
May 7, 2026
Merged

fix(minifier): cap if-return ternary collapse for firefox#21841
Boshen merged 7 commits into
oxc-project:mainfrom
guru-irl:user/guru-irl/minifier-conditional-depth-cap

Conversation

@guru-irl

@guru-irl guru-irl commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Firefox's SpiderMonkey parser refuses to parse a right-leaning ?: chain above roughly 4000-5000 levels and throws InternalError: too much recursion at script-compile time, with no filename or line number. V8 parses 100 000+ without trouble. This is reproducible standalone in a tiny repro repo:

https://github.com/guru-irl/firefox-ternary-parse-recursion (42 Playwright assertions across Chromium 147 and Firefox 148, three literal types, eight depths)
Mozilla bug: https://bugzilla.mozilla.org/show_bug.cgi?id=2034840

@Boshen Boshen self-assigned this Apr 27, 2026
@Boshen Boshen added P-high Priority - High A-minifier Area - Minifier labels Apr 27, 2026
@sapphi-red

Copy link
Copy Markdown
Member

The problem sounds similar to swc-project/swc#11736, which I came across the other day.

@guru-irl

Copy link
Copy Markdown
Contributor Author

@Boshen gentle reminder for a review :)

@jrmuizel

Copy link
Copy Markdown

I tested Safari's limit to be 6325.

Also, I think it would be good for the limit to be enabled by default. i.e. it's hard for me to imagine situations where people someone would explicitly choose to turn off the limit.

@Boshen Boshen changed the title feat(minifier): add max_conditional_depth to cap if-return ternary collapse fix(minifier): cap if-return ternary collapse for firefox May 7, 2026
guru-irl and others added 6 commits May 7, 2026 11:56
…llapse

The peephole pass in minimize_statements.rs collapses a chain of
consecutive `if (cond) return x;` statements into a single right-leaning
nested ternary (`return cond1 ? x : cond2 ? y : ... : z;`). There is no
depth cap on that collapse, so a function with N such cases produces a
`ConditionalExpression` nested N-1 levels deep.

This is a problem for Firefox. SpiderMonkey's parser refuses to parse
right-leaning ternary chains above roughly 4000-5000 levels, throwing
`InternalError: too much recursion` at script compile time with no
filename or line info. Chromium's V8 parses 100000+ without trouble.
Real-world trigger: webpack's `__webpack_require__.u` with one `if` per
chunk, in a bundle with several thousand chunks.

This change adds a new `Option<u32>` option:

  CompressOptions { max_conditional_depth: Option<u32>, ... }

When set, the `return_loop` and `throw_loop` each measure the depth of
the `ConditionalExpression` already held by the trailing return/throw
argument via `Self::conditional_alternate_depth`, and break out of the
merge loop once that depth reaches the cap. Remaining if-return and
if-throw statements stay as separate statements. Because the check
looks at the actual ternary depth (not a loop counter), it stays
correct across the minifier's re-entrant passes.

Default is `None`. Behaviour unchanged for existing callers.

Test-Plan:
- cargo test -p oxc_minifier, 489/489 pass including the new
  test_max_conditional_depth_caps_return_ternary_chain covering the
  uncapped baseline, cap=2 (return), cap=0, and cap=2 (throw).
- Option plumbed through napi/minify for the NAPI binding.
@codspeed-hq

codspeed-hq Bot commented May 7, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 44 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing guru-irl:user/guru-irl/minifier-conditional-depth-cap (e4c1d7f) with main (7bb00dd)

Open in CodSpeed

Footnotes

  1. 7 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.

@Boshen Boshen force-pushed the user/guru-irl/minifier-conditional-depth-cap branch from d8d16d9 to e4c1d7f Compare May 7, 2026 04:26
@Boshen Boshen merged commit 5753774 into oxc-project:main May 7, 2026
35 checks passed
camc314 added a commit that referenced this pull request May 11, 2026
### 🚀 Features

- 66c9b01 transformer/typescript: Debug_assert that `enum_eval` ran in
semantic (#22252) (Dunqing)
- ffe6475 minifier: Fold `Array` constructor with safe spreads (#22215)
(camc314)

### 🐛 Bug Fixes

- d3d0b18 traverse: Handle `ChainElement::TSNonNullExpression` in
`GatherNodeParts` (#22247) (leaysgur)
- 4e880de transformer/object-rest-spread: Declare temp vars for computed
keys (#22284) (camc314)
- a7c3e22 semantic: Clear member write target for computed keys (#22302)
(camc314)
- 6a8852d codegen: Emit newline after legal-comment orphan flush
(#22304) (Dunqing)
- 5da9fda transformer/explicit-resource-management: Preserve class names
(#22306) (Dunqing)
- b5d970f transformer/explicit-resource-management: Preserve class names
(#22290) (camc314)
- bc54fd4 minifier: Keep function / class names if direct eval is
present in the scope (#22241) (sapphi-red)
- 7a810c0 minifier: Refresh direct eval flags after DCE (#21787)
(Dunqing)
- dd88726 transformer/legacy-decorator: Preserve accessor type
annotation for emitDecoratorMetadata (#21966) (Dunqing)
- 29a3cd7 codegen: Swap mapping/indent order for top-level decls
(#22206) (Dunqing)
- 73b4f40 minifier: Preserve catch binding with direct eval (#22221)
(camc314)
- 0e13d17 minifier: Preserve optional chain base side effects (#22219)
(camc314)
- 0c7c01c transformer/typescript: Inline optional-chain enum member
access (#21834) (Dunqing)
- a6aff7e codegen: Emit block/array/object end mapping at close char
(#22200) (Dunqing)
- a099b03 codegen: Emit call end mapping at `)` position, not past it
(#22199) (Dunqing)
- 5753774 minifier: Cap if-return ternary collapse for firefox (#21841)
(Gurupungav Narayanan)
- 2493bdd codegen: Correct sourcemap end mappings for closing delimiters
(#22001) (Mark Dalgleish)
- 3b385e2 minifier: Bail optimizing `Array` with unknown arg count
(#22188) (camc314)
- 9fa2122 parser: Parse array computed class keys (#22159) (camc314)

### 📚 Documentation

- a4a6892 napi/parser: Correct code comment (#22278) (overlookmotel)
- 9305373 oxc: Update README (#22178) (camc314)

Co-authored-by: Cameron <[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 P-high Priority - High

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants