Speed up narrowing of literal unions - #64044
Open
Jake Bailey (jakebailey) wants to merge 2 commits into
Open
Conversation
Narrowing a union by a type predicate maps every source constituent over every candidate constituent through the general type relation machinery. Large generated literal unions therefore take quadratic time even though matching literals only requires equality. Intersect unions made entirely of non-enum literals using keyed sets. Keep enum and mixed unions on the existing relation path because they require broader assignability semantics. Also bypass relation work for individual identical literals encountered by that fallback path. Add coverage for narrowing mixed string and number literal unions, including equal-valued literals of different primitive kinds. Fixes microsoft#55948
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes literal-union narrowing for type predicates using direct literal comparisons and lookup-based filtering.
Changes:
- Adds a fast path for non-enum literal unions.
- Adds direct literal comparison during narrowing.
- Adds a compiler regression test and baselines.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
tsc/internal/checker/flow.go |
Implements literal-union narrowing optimizations. |
tsc/testdata/tests/cases/compiler/narrowLiteralUnionByTypePredicate.ts |
Adds the regression test. |
tsc/testdata/baselines/reference/compiler/narrowLiteralUnionByTypePredicate.types |
Records narrowed types. |
tsc/testdata/baselines/reference/compiler/narrowLiteralUnionByTypePredicate.symbols |
Records symbol resolution. |
tsc/testdata/baselines/reference/compiler/narrowLiteralUnionByTypePredicate.js |
Records emitted JavaScript. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+928
to
+933
| if tLiteralFlags != 0 && tLiteralFlags == nLiteralFlags && !(t.flags&TypeFlagsEnumLiteral != 0 && n.flags&TypeFlagsEnumLiteral != 0) { | ||
| if t.AsLiteralType().value == n.AsLiteralType().value { | ||
| return t | ||
| } | ||
| return c.neverType | ||
| } |
The per-literal fast path compared values and retained the source constituent. When a plain string literal was narrowed by a predicate whose candidate was the matching enum member, this lost the enum type and made the result unassignable to that string enum member. Retain the candidate when it carries enum identity and the source does not. Continue using the existing relation machinery when both sides are enum members, where equal underlying values are not sufficient. Add a string-enum predicate regression that requires the narrowed value to remain assignable to the asserted enum member.
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 #55948
fix-55948We'll see if this hurts too much elsewhere