Skip to content

Commit b0e9073

Browse files
committed
Compare object literal types by content directly
Interning object literal types by content hashed every property name and type of each literal checked into a key, and looked the key up in a map keyed by that hash. The key starts with the node, so the types created for one literal are only ever compared with each other. Keep the types created for each object literal node instead, and compare their content with the new content directly. Few literals are checked more than once, so this is usually an empty list, and nothing is hashed. Comparing content directly also cannot confuse two contents whose hashes collide. PERF-PLACEHOLDER
1 parent f871455 commit b0e9073

2 files changed

Lines changed: 49 additions & 43 deletions

File tree

‎tsc/internal/ast/symbol.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ func (m *SymbolMap) symbolAt(i int) *Symbol {
223223
return symbol
224224
}
225225

226+
// At returns the name and symbol of the i-th entry, in insertion order.
227+
func (m *SymbolMap) At(i int) (string, *Symbol) {
228+
return m.keyAt(i), m.symbolAt(i)
229+
}
230+
226231
func (m *SymbolMap) find(key string) int {
227232
if m == nil {
228233
return -1

‎tsc/internal/checker/checker.go‎

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ type memoStore struct {
870870
unionTypes memoMap[CacheHashKey, *Type]
871871
unionOfUnionTypes memoMap[UnionOfUnionKey, *Type]
872872
intersectionTypes memoMap[CacheHashKey, *Type]
873-
objectLiteralTypes memoMap[CacheHashKey, *Type]
873+
objectLiteralTypes memoMap[*ast.Node, []objectLiteralType]
874874
spreadTypes memoMap[CacheHashKey, *Type]
875875
objectContentTypes memoMap[CacheHashKey, *Type]
876876
spreadSymbols memoMap[spreadSymbolKey, *ast.Symbol]
@@ -13896,16 +13896,18 @@ func (c *Checker) checkObjectLiteral(node *ast.Node, checkMode CheckMode) *Type
1389613896
if patternWithComputedProperties {
1389713897
flags |= ObjectFlagsObjectLiteralPatternWithComputedProperties
1389813898
}
13899-
key := c.getObjectLiteralTypeKey(node, flags, propertiesTable, indexInfos)
13900-
if existing, _ := c.objectLiteralTypes.get(c, key); existing != nil {
13901-
return existing
13899+
existingTypes, _ := c.objectLiteralTypes.peek(node)
13900+
for _, existing := range existingTypes {
13901+
if c.isSameObjectLiteralType(existing, flags, propertiesTable, indexInfos) {
13902+
return existing.t
13903+
}
1390213904
}
1390313905
result := c.newAnonymousType(node.Symbol(), propertiesTable, nil, nil, indexInfos)
1390413906
result.objectFlags |= flags
1390513907
if inDestructuringPattern {
1390613908
c.patternForType.intern(result, node)
1390713909
}
13908-
c.objectLiteralTypes.intern(key, result)
13910+
c.objectLiteralTypes.intern(node, append(existingTypes, objectLiteralType{flags, propertiesTable, indexInfos, result}))
1390913911
return result
1391013912
}
1391113913
for _, memberDecl := range node.Properties() {
@@ -18841,50 +18843,49 @@ func (b *keyBuilder) writeNode(node *ast.Node) {
1884118843
}
1884218844
}
1884318845

18844-
// getObjectLiteralTypeKey identifies the content of an object literal type, so
18845-
// that checking the same object literal again with the same results yields the
18846-
// same type.
18847-
func (c *Checker) getObjectLiteralTypeKey(node *ast.Node, flags ObjectFlags, properties ast.SymbolTable, indexInfos []*IndexInfo) CacheHashKey {
18848-
var b keyBuilder
18849-
b.writeNode(node)
18850-
b.writeUint32(uint32(flags))
18851-
b.writeInt(properties.Len())
18852-
for name, prop := range properties.All() {
18853-
b.writeString(name)
18854-
b.writeByte(0)
18855-
b.writeUint32(uint32(prop.Flags()))
18856-
b.writeUint32(uint32(prop.CheckFlags()))
18857-
// Members created by this object literal have their types set at
18846+
// An objectLiteralType is a type created for an object literal, with the
18847+
// content it was created from; see isSameObjectLiteralType.
18848+
type objectLiteralType struct {
18849+
flags ObjectFlags
18850+
properties ast.SymbolTable
18851+
indexInfos []*IndexInfo
18852+
t *Type
18853+
}
18854+
18855+
// isSameObjectLiteralType reports whether an object literal type created
18856+
// before has the given content, so that checking the same object literal
18857+
// again with the same results yields the same type.
18858+
func (c *Checker) isSameObjectLiteralType(e objectLiteralType, flags ObjectFlags, properties ast.SymbolTable, indexInfos []*IndexInfo) bool {
18859+
if e.flags != flags || e.properties.Len() != properties.Len() || len(e.indexInfos) != len(indexInfos) {
18860+
return false
18861+
}
18862+
for i, info := range indexInfos {
18863+
other := e.indexInfos[i]
18864+
if info.keyType != other.keyType || info.valueType != other.valueType || info.isReadonly != other.isReadonly || !slices.Equal(info.components, other.components) {
18865+
return false
18866+
}
18867+
}
18868+
for i := range properties.Len() {
18869+
name, prop := properties.At(i)
18870+
otherName, other := e.properties.At(i)
18871+
if name != otherName || prop.Flags() != other.Flags() || prop.CheckFlags() != other.CheckFlags() {
18872+
return false
18873+
}
18874+
// Members created by an object literal have their types set at
1885818875
// creation; other members (accessors) are the declared symbols.
1885918876
links := c.valueSymbolLinks.TryGet(prop)
18860-
if links == nil || links.target == nil {
18861-
b.writeByte(2)
18862-
b.writeSymbol(prop)
18877+
otherLinks := c.valueSymbolLinks.TryGet(other)
18878+
if links == nil || links.target == nil || otherLinks == nil || otherLinks.target == nil {
18879+
if prop != other {
18880+
return false
18881+
}
1886318882
continue
1886418883
}
18865-
if nameType := links.getNameType(); nameType != nil {
18866-
b.writeByte(1)
18867-
b.writeType(nameType)
18868-
} else {
18869-
b.writeByte(0)
18870-
}
18871-
b.writeType(links.resolvedType.peek())
18872-
}
18873-
b.writeInt(len(indexInfos))
18874-
for _, info := range indexInfos {
18875-
b.writeType(info.keyType)
18876-
b.writeType(info.valueType)
18877-
if info.isReadonly {
18878-
b.writeByte(1)
18879-
} else {
18880-
b.writeByte(0)
18881-
}
18882-
b.writeInt(len(info.components))
18883-
for _, n := range info.components {
18884-
b.writeNode(n)
18884+
if links.getNameType() != otherLinks.getNameType() || links.resolvedType.peek() != otherLinks.resolvedType.peek() {
18885+
return false
1888518886
}
1888618887
}
18887-
return b.hash()
18888+
return true
1888818889
}
1888918890

1889018891
func getTypeListKey(types []*Type) CacheHashKey {

0 commit comments

Comments
 (0)