From 07afcb0aaaf8321d5c3561c8d1b8250d1d6b47b7 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 10 Nov 2026 14:22:37 -0800 Subject: [PATCH] Cache the names a union or intersection lacks Many property lookups on a union are of names it lacks, such as when narrowing by a discriminant, and each one looked through every constituent again, since only properties found were cached. Most missing names are looked up only once, though, and caching every one grows the caches of large projects such as MUI's docs considerably, so a name is cached once it is missed a second time, which a small filter of earlier misses tells. Symbol tables cannot hold a missing entry, so the missing names are kept apart, for each kind of lookup. --- tsc/internal/checker/checker.go | 44 +++++++++++++++++++++++++++++++++ tsc/internal/checker/types.go | 10 ++++++++ 2 files changed, 54 insertions(+) diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 950b9a8e30fb8..ce6fad766307c 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -4,6 +4,7 @@ import ( "context" "encoding/binary" "fmt" + "hash/maphash" "iter" "maps" "math" @@ -646,6 +647,7 @@ type Checker struct { thisExpandoLocations map[*ast.Symbol]*ast.Node subtypeReductionCache map[CacheHashKey][]*Type cachedTypes map[CachedTypeKey]*Type + missedProperties []uint64 // Filter of union and intersection property names looked up and missed; see missedBefore cachedSignatures map[CachedSignatureKey]*Signature undefinedProperties map[*ast.Symbol]*ast.Symbol narrowedTypes map[NarrowedTypeKey]*Type @@ -22064,6 +22066,13 @@ func (c *Checker) getUnionOrIntersectionProperty(t *Type, name string, skipObjec return prop } } + lookup := propertyLookupAugmented + if skipObjectFunctionPropertyAugment { + lookup = propertyLookupWithoutFunctionPropertyAugment + } + if data.missingProperties[name]&lookup != 0 { + return nil + } prop := c.createUnionOrIntersectionProperty(t, name, skipObjectFunctionPropertyAugment) if prop != nil { if skipObjectFunctionPropertyAugment { @@ -22071,10 +22080,45 @@ func (c *Checker) getUnionOrIntersectionProperty(t *Type, name string, skipObjec } else { ast.GetSymbolTable(&data.propertyCache).Set(name, prop) } + } else if c.missedBefore(t, name, skipObjectFunctionPropertyAugment) { + // Many lookups of a union's properties are of names it lacks, such + // as when narrowing by a discriminant, which would otherwise look + // through every constituent each time. + if data.missingProperties == nil { + data.missingProperties = make(map[string]propertyLookups) + } + data.missingProperties[name] |= lookup } return prop } +// missedBefore reports whether a lookup of name on the union or intersection +// t found no property before, recording that this one found none. Most names +// a union lacks are looked up once, so caching every one would keep many +// entries that are never read; a name is cached once it is missed twice. The +// misses are recorded in a filter of fixed size, which may report a miss +// that did not happen, caching the name sooner. +func (c *Checker) missedBefore(t *Type, name string, skipObjectFunctionPropertyAugment bool) bool { + if c.missedProperties == nil { + c.missedProperties = make([]uint64, missedPropertiesWords) + } + h := maphash.String(missedPropertiesSeed, name) ^ uint64(t.id)*0x9E3779B97F4A7C15 + if skipObjectFunctionPropertyAugment { + h = ^h + } + i := h % (missedPropertiesWords * 64) + word, bit := &c.missedProperties[i/64], uint64(1)<<(i%64) + if *word&bit != 0 { + return true + } + *word |= bit + return false +} + +const missedPropertiesWords = 1 << 17 + +var missedPropertiesSeed = maphash.MakeSeed() + func (c *Checker) createUnionOrIntersectionProperty(containingType *Type, name string, skipObjectFunctionPropertyAugment bool) *ast.Symbol { propFlags := ast.SymbolFlagsNone var singleProp *ast.Symbol diff --git a/tsc/internal/checker/types.go b/tsc/internal/checker/types.go index bfa846b876822..6c61b6b596d8d 100644 --- a/tsc/internal/checker/types.go +++ b/tsc/internal/checker/types.go @@ -1471,9 +1471,19 @@ type UnionOrIntersectionType struct { types []*Type propertyCache ast.SymbolTable // Augmented entries, except complete ones already in the non-augmented cache. propertyCacheWithoutFunctionPropertyAugment ast.SymbolTable + missingProperties map[string]propertyLookups // Names no property was found for, by the lookups that found none resolvedProperties []*ast.Symbol } +// propertyLookups are the ways getUnionOrIntersectionProperty looks up a +// property, with or without the members of Object and Function. +type propertyLookups uint8 + +const ( + propertyLookupAugmented propertyLookups = 1 << iota + propertyLookupWithoutFunctionPropertyAugment +) + func (t *UnionOrIntersectionType) AsUnionOrIntersectionType() *UnionOrIntersectionType { return t } func (t *UnionOrIntersectionType) Types() []*Type {