Skip to content

Cache the constraint of generic indexed access types - #65664

Draft
Ryan Cavanaugh (RyanCavanaugh) wants to merge 3 commits into
mainfrom
copilot/optimize-indexed-access-check
Draft

Ryan Cavanaugh (RyanCavanaugh) wants to merge 3 commits into
mainfrom
copilot/optimize-indexed-access-check

Conversation

@RyanCavanaugh

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

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:

type ColumnFor<T extends Table> = Columns[T];   // Columns: 300 props, each a named union alias
declare function parent<C extends Column | undefined>(column?: C): C;  // Column: ~1,700 literals

export function parentOf<T extends Table>(column: ColumnFor<T>) {
    return parent(column);
}

Cause

  • typeRelatedToSomeType relates Columns[T] to each member of the target union.
  • For each member, the source type-variable branch of structuredTypeRelatedToWorker calls getConstraintOfType(source), which reaches getConstraintFromIndexedAccess.
  • That function had no cache, so Columns[Table] was rebuilt once per target member: 300 property lookups, then a getUnionType over 300 named unions.
  • Type parameters (tp.constraint) and conditional types (resolvedDefaultConstraint, resolvedConstraintOfDistributive) already cache their constraints. Indexed access types did not.
  • The gap against 5.9 is wider because getUnionTypeWorker's named-union scan binary-searches with CompareTypes, which costs more than Strada's id comparison.

Change

  • Adds a resolvedConstraint field to IndexedAccessType, filled on first use with c.noConstraintType as the "no constraint" sentinel. This follows the existing ConditionalType pattern.
  • The existing body moves unchanged into computeConstraintFromIndexedAccess.
  • The cached value depends only on objectType, indexType and accessFlags. It is computed with no access node, and only after hasNonCircularBaseConstraint(t) in getConstraintOfIndexedAccess, 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.

Repro Check time Peak RSS
Issue gen.mjs, 300 tables 18.45 s → 0.049 s 41.6 → 32.2 MB
200 tables 20.92 s → 0.051 s 37.7 → 31.9 MB
100 tables 5.75 s → 0.028 s 36.8 → 30.6 MB
Inline unions, 300 tables 1.30 s → 0.030 s 43.1 → 33.4 MB

Test

  • Adds indexedAccessConstraintRelatedToLargeUnion.ts: 100 tables × 12 columns, generated with template literal types.
  • It produces no baselines, so it only fails by timing out: about 5 s before the fix and about 0.08 s after.
  • There are no baseline changes elsewhere.

…sed unions

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@typescript-automation typescript-automation Bot added For Uncommitted Bug PR for untriaged, rejected, closed or missing bug and removed For Milestone Bug PRs that fix a bug with a specific milestone labels Nov 25, 2026
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) changed the title [WIP] Optimize TypeScript type-checking against large union of literal aliases Cache the constraint of generic indexed access types Nov 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Checking a generic indexed access against a large union of literal aliases takes 16 s on 7.0.2 (5.9 s on 5.9.3)

2 participants