-
Notifications
You must be signed in to change notification settings - Fork 475
Fix exponential compilation blowup in pattern matching and boolean si… #8078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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
e7e20d7 to
bb596f7
Compare
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
cknitt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
|
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 |
|
My motivation for looking into this further has decreases as the bug reports from users have been fixed. |
|
Fair enough |
…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