Repository navigation
Cache the constraint of generic indexed access types - #65664
Conversation
…sed unions Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
|
TypeScript Bot (@typescript-bot) test it |
|
Hey Ryan Cavanaugh (@RyanCavanaugh), the results of running the DT tests are ready. Everything looks the same! |
|
Ryan Cavanaugh (@RyanCavanaugh) Here are the results of running the user tests with tsc comparing Everything looks good! |
|
Ryan Cavanaugh (@RyanCavanaugh) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Ryan Cavanaugh (@RyanCavanaugh) Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
Ryan Cavanaugh (RyanCavanaugh)
left a comment
There was a problem hiding this comment.
Ready for review (I would approve this but that's no longer legal for some reason)
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the indexed-access constraint caching change.
No open issues 🎉
What changed in this PR
Addresses #65654 by caching generic indexed-access constraints in the TypeScript checker, avoiding repeated union construction when comparing against large union targets.
Changes:
- Adds a cached constraint field with a sentinel for “no constraint.”
- Separates constraint computation from cache lookup without changing the computation logic.
- Adds a compiler regression case covering 100 tables with 12 columns each.
| File | Description |
|---|---|
tsc/testdata/tests/cases/compiler/indexedAccessConstraintRelatedToLargeUnion.ts |
Adds a large-union regression case for generic indexed access. |
tsc/internal/checker/types.go |
Adds resolvedConstraint to IndexedAccessType. |
tsc/internal/checker/checker.go |
Caches constraint results and extracts the existing computation into a helper. |
Review effort: Balanced
Relating a generic indexed access to a large union target takes seconds when the object's properties are named union aliases. The issue's repro takes about 16–18 s to check:
Cause
typeRelatedToSomeTyperelatesColumns[T]to each member of the target union.structuredTypeRelatedToWorkercallsgetConstraintOfType(source), which reachesgetConstraintFromIndexedAccess.Columns[Table]was rebuilt once per target member: 300 property lookups, then agetUnionTypeover 300 named unions.tp.constraint) and conditional types (resolvedDefaultConstraint,resolvedConstraintOfDistributive) already cache their constraints. Indexed access types did not.getUnionTypeWorker's named-union scan binary-searches withCompareTypes, which costs more than Strada's id comparison.Change
resolvedConstraintfield toIndexedAccessType, filled on first use withc.noConstraintTypeas the "no constraint" sentinel. This follows the existingConditionalTypepattern.computeConstraintFromIndexedAccess.objectType,indexTypeandaccessFlags. It is computed with no access node, and only afterhasNonCircularBaseConstraint(t)ingetConstraintOfIndexedAccess, the only caller.getUnionTypeWorker's named-union scan is unchanged. It is no longer reached repeatedly in this scenario, so speeding it up is left for a follow-up.Results
Check time from
--extendedDiagnostics; peak RSS from/usr/bin/time -v.gen.mjs, 300 tablesTest
indexedAccessConstraintRelatedToLargeUnion.ts: 100 tables × 12 columns, generated with template literal types.