Fix exponential compilation blowup in pattern matching and boolean si…#8078
Merged
cristianoc merged 1 commit intomasterfrom Dec 9, 2025
Merged
Fix exponential compilation blowup in pattern matching and boolean si…#8078cristianoc merged 1 commit intomasterfrom
cristianoc merged 1 commit intomasterfrom
Conversation
There was a problem hiding this comment.
💡 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
e7e20d7 to
bb596f7
Compare
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
Member
|
This lead to some unfortunate output when upgrading: For k->Context.onKeyRelease(key => {
switch key {
| Space => fly(bird)
| _ => ()
}
})I now get: k.onKeyRelease(key => {
if ((key === "f6" || key === " " || key === "f7" || key === "space" || key === "f8" || key === "meta" || key === "f9" || key === "alt" || key === "f10" || key === "control" || key === "f11" || key === "tab" || key === "f12" || key === "enter" || key === "`" || key === "backspace" || key === "1" || key === "escape" || key === "2" || key === "/" || key === "3" || key === "." || key === "4" || key === "," || key === "5" || key === "m" || key === "6" || key === "n" || key === "7" || key === "b" || key === "8" || key === "v" || key === "9" || key === "c" || key === "0" || key === "x" || key === "-" || key === "z" || key === "+" || key === "'" || key === "=" || key === ";" || key === "q" || key === "l" || key === "w" || key === "k" || key === "e" || key === "j" || key === "r" || key === "h" || key === "t" || key === "g" || key === "y" || key === "f" || key === "u" || key === "d" || key === "i" || key === "s" || key === "o" || key === "a" || key === "p" || key === "\\" || key === "[" || key === "]") && key === "space") {
return fly(bird);
}
});where I had: k.onKeyRelease(key => {
if (key === "space") {
return fly(bird);
}
});and |
Collaborator
Author
|
My motivation for looking into this further has decreases as the bug reports from users have been fixed. |
Member
|
Fair enough |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…mplification
This PR fixes two sources of exponential compilation time:
Boolean expression simplification (
js_exp_make.ml): Thesimplify_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.
Exhaustiveness checking (
parmatch.ml): Theexhaust_gadtfunction 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:
Added test cases for both scenarios.
Closes #8074
Closes #8039
Closes #8042