Repository navigation
Build set instead of binary searching for denormalized origins - #65619
Open
Jake Bailey (jakebailey) wants to merge 1 commit into
Open
Jake Bailey (jakebailey) wants to merge 1 commit into
Jake Bailey (jakebailey) wants to merge 1 commit into
Conversation
When getUnionTypeWorker builds a denormalized origin type, it looks up each member of the new union in every named union it was built from. Those lookups binary-search with CompareTypes, which compares flags, names, alias type arguments and intersection member lists before falling back to type IDs. On unions built from large unions of intersections, these structural comparisons dominate check time; in crossProductUnionIntersectionRepeatedPropDeclarationsNoOOM1.ts, check time drops from about 5.7s to 2.1s. Type IDs are unique per checker, so CompareTypes returns 0 only for identical types. A set of the named unions' members therefore answers the same membership question with pointer lookups.
Member
Author
|
TypeScript Bot (@typescript-bot) perf test this faster |
Contributor
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Small reduced unions still trigger map construction over every member of large aliases, causing avoidable linear work and allocations instead of a few binary searches.
1 open issue
What changed in this PR
This PR optimizes denormalized union-origin construction in the TypeScript checker, targeting costly membership checks in large union/intersection workloads.
Changes:
- Builds a pointer-keyed set of named-union members.
- Replaces repeated binary searches with set membership checks.
| File | Description |
|---|---|
tsc/internal/checker/checker.go |
Uses set lookups when identifying members outside named unions. |
Review effort: Balanced
Comment on lines
+26671
to
+26673
| for _, u := range namedUnions { | ||
| for _, t := range u.Types() { | ||
| namedTypes.Add(t) |
Member
Author
There was a problem hiding this comment.
Hm, no? We already did similar work above for typeSet
Contributor
|
Jake Bailey (@jakebailey) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This branch has not been deployed
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.

CompareTypesis designed in such a way thatCompareTypes(a, b) == 0impliesa == b, which means that anywhere we binary search a sorted array, it would also be valid to create a set of types and then search those.Normally that's not very helpful, since binary searching is typically faster. But when
getUnionTypeWorkerbuilds a denormalized origin type, it looks up each member of the new union in every named union it was built from. This can actually dominate check time, for example,crossProductUnionIntersectionRepeatedPropDeclarationsNoOOM1.ts.So in this case, we can build the set, then check membership. This makes this test drop from about 5.7s to 2.1s.
(Note that this only works because
CompareTypesends with basicallya.id - b.id, so two types of different IDs obviously cannot be the same by pointer, but this ordering is nondeterministic, something I am nearly done fixing in #65064. Thankfully, ordering does not matter for this one trick!)