Skip to content

Conversation

@cristianoc
Copy link
Collaborator

…mplification

This PR fixes two sources of exponential compilation time:

  1. Boolean expression simplification (js_exp_make.ml): The simplify_and_ function had O(3^n) recursive behavior when simplifying nested AND expressions. Added a depth limit (10) to prevent exponential blowup while preserving optimizations for normal cases.

    Fixes the issue reported in Fix infinite recursion in E.econd when optimizing nested conditionals #8039 and repro large variant timeout #8042 (large unboxed variants). Note: PR Fix infinite recursion in E.econd when optimizing nested conditionals #8039's diagnosis of "infinite recursion" was incorrect - the actual issue was exponential blowup, not infinite loops.

  2. Exhaustiveness checking (parmatch.ml): The exhaust_gadt function had exponential complexity (~4^n) when checking exhaustiveness for dict pattern matching. Added a call count limit (1000) that conservatively reports non-exhaustive when exceeded, preventing hangs while maintaining correctness.

Performance improvements:

  • Large unboxed variants (28 cases): 3.79s -> 0.03s (126x faster)
  • Dict pattern matching (6 cases): 0.94s -> 0.04s (24x faster)

Added test cases for both scenarios.

Closes #8074
Closes #8039
Closes #8042

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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

…mplification

This PR fixes two sources of exponential compilation time:

1. **Boolean expression simplification** (`js_exp_make.ml`):
   The `simplify_and_` function had O(3^n) recursive behavior when
   simplifying nested AND expressions. Added a depth limit (10) to
   prevent exponential blowup while preserving optimizations for
   normal cases.

   Fixes the issue reported in #8039 and #8042 (large unboxed variants).
   Note: PR #8039's diagnosis of "infinite recursion" was incorrect -
   the actual issue was exponential blowup, not infinite loops.

2. **Exhaustiveness checking** (`parmatch.ml`):
   The `exhaust_gadt` function had exponential complexity (~4^n) when
   checking exhaustiveness for dict pattern matching. Added a call
   count limit (1000) that conservatively reports non-exhaustive when
   exceeded, preventing hangs while maintaining correctness.

Performance improvements:
- Large unboxed variants (28 cases): 3.79s -> 0.03s (126x faster)
- Dict pattern matching (6 cases): 0.94s -> 0.04s (24x faster)

Added test cases for both scenarios.

Closes #8074
Closes #8039
Closes #8042
@cristianoc cristianoc force-pushed the fix-exponential-compilation-blowup branch from e7e20d7 to bb596f7 Compare December 9, 2025 09:11
@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 9, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@8078

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@8078

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@8078

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@8078

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@8078

@rescript/runtime

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/runtime@8078

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@8078

commit: bb596f7

Copy link
Member

@cknitt cknitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@cristianoc cristianoc merged commit 0c49e79 into master Dec 9, 2025
36 of 37 checks passed
@cristianoc cristianoc deleted the fix-exponential-compilation-blowup branch December 9, 2025 13:13
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.

JSON destructuring causes stack overflow in compiler

3 participants