Fix exponential LIR blowup for long list-pattern matches#9707
Merged
Conversation
A match on a List desugared each branch into a tree of nested if/match expressions whose miss arm was the shared "rest of the match" expression. The Monotype IR kept that shared by id (so it stayed linear), but LIR lowering re-lowers each referenced expression, so a k-element list pattern re-materialized the entire fallback k+1 times, compounding to roughly (elements+1)^branches LIR statements and exhausting memory at compile time. List patterns are now first-class through the post-check pipeline and are lowered by the same decision machinery as tags/tuples/records, sharing one miss join per branch via lowerBranchChain. Compilation is now linear in the pattern size.
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.
Fixes #9700.
A
matchon aListwas desugared into a tree of nestedif/two-armed-matchexpressions whose miss arm pointed at the shared "rest of the match" expression. The Monotype IR kept that fallback shared by id, so it stayed linear, but LIR lowering re-lowers each referenced expression, so ak-element list pattern re-materialized the entire fallbackk+1times (one per element test plus the length test). Composed across branches this produced roughly(elements+1)^branchesLIR statements, which exhausted memory or hung the compiler — e.g. the codon example in the issue grew ~4x per added branch (1136 → 4272 → 16816 statements for 3 → 4 → 5 branches). String-literal branches were unaffected because they share a singleon_miss, and tag/tuple/record patterns were unaffected because they already share each branch's miss through a join point.List patterns are now first-class through the post-check pipeline (Monotype, Lifted, Lambda Solved, Lambda Mono) and are lowered by the same decision machinery as every other pattern:
lowerListPatternThenemits a length test, per-elementlist_get_unsafeextraction, and the optional captured rest, all jumping to the one shared miss join thatlowerBranchChainalready creates per branch. The rest slice useslist_take_first/list_take_lastwith scalar counts rather than reconstructing a{start, len}record. This makes the generated LIR linear in the pattern size, so the issue's program now compiles in well under a second instead of running out of memory. The separate refutable let-binding path for list patterns is unchanged.