Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/binary"
"fmt"
"hash/maphash"
"iter"
"maps"
"math"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -22064,17 +22066,59 @@ 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
Comment on lines +22073 to +22074
}
prop := c.createUnionOrIntersectionProperty(t, name, skipObjectFunctionPropertyAugment)
if prop != nil {
if skipObjectFunctionPropertyAugment {
ast.GetSymbolTable(&data.propertyCacheWithoutFunctionPropertyAugment).Set(name, prop)
} 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
Expand Down
10 changes: 10 additions & 0 deletions tsc/internal/checker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading