Skip to content
Merged
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
54 changes: 54 additions & 0 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,8 @@ type Checker struct {
skipDirectInferenceNodes collections.Set[*ast.Node]
ctx context.Context
packagesMap map[string]bool
activeMappers []*TypeMapper
activeTypeMappersCaches []map[string]*Type
Comment on lines +859 to +860
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other "global" caches don't use two separate lists in the Go port, instead scoping them together, but I'm keeping them separate since I'm trying to reuse the maps.

}

func NewChecker(program Program) *Checker {
Expand Down Expand Up @@ -21174,14 +21176,66 @@ func (c *Checker) instantiateTypeWithAlias(t *Type, m *TypeMapper, alias *TypeAl
c.error(c.currentNode, diagnostics.Type_instantiation_is_excessively_deep_and_possibly_infinite)
return c.errorType
}
index := c.findActiveMapper(m)
if index == -1 {
c.pushActiveMapper(m)
}
var b KeyBuilder
b.WriteType(t)
b.WriteAlias(alias)
key := b.String()
cache := c.activeTypeMappersCaches[core.IfElse(index != -1, index, len(c.activeTypeMappersCaches)-1)]
if cachedType, ok := cache[key]; ok {
return cachedType
}
c.TotalInstantiationCount++
c.instantiationCount++
c.instantiationDepth++
result := c.instantiateTypeWorker(t, m, alias)
if index == -1 {
c.popActiveMapper()
} else {
cache[key] = result
}
c.instantiationDepth--
return result
}

func (c *Checker) pushActiveMapper(mapper *TypeMapper) {
c.activeMappers = append(c.activeMappers, mapper)

lastIndex := len(c.activeTypeMappersCaches)
if cap(c.activeTypeMappersCaches) > lastIndex {
// The cap may contain an empty map from popActiveMapper; reuse it.
c.activeTypeMappersCaches = c.activeTypeMappersCaches[:lastIndex+1]
if c.activeTypeMappersCaches[lastIndex] == nil {
c.activeTypeMappersCaches[lastIndex] = make(map[string]*Type, 1)
}
} else {
c.activeTypeMappersCaches = append(c.activeTypeMappersCaches, make(map[string]*Type, 1))
}
}

func (c *Checker) popActiveMapper() {
c.activeMappers[len(c.activeMappers)-1] = nil
c.activeMappers = c.activeMappers[:len(c.activeMappers)-1]

// Clear the map, but leave it in the list for later reuse.
lastIndex := len(c.activeTypeMappersCaches) - 1
clear(c.activeTypeMappersCaches[lastIndex])
c.activeTypeMappersCaches = c.activeTypeMappersCaches[:lastIndex]
}

func (c *Checker) findActiveMapper(mapper *TypeMapper) int {
return core.FindLastIndex(c.activeMappers, func(m *TypeMapper) bool { return m == mapper })
}

func (c *Checker) clearActiveMapperCaches() {
for _, cache := range c.activeTypeMappersCaches {
clear(cache)
}
}

// Return true if the given type could possibly reference a type parameter for which
// we perform type inference (i.e. a type parameter of a generic function). We cache
// results for union and intersection types for performance reasons.
Expand Down
1 change: 1 addition & 0 deletions internal/checker/inference.go
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,7 @@ func (c *Checker) getInferredType(n *InferenceContext, index int) *Type {
}
inference.inferredType = inferredType
}
c.clearActiveMapperCaches()
}
return inference.inferredType
}
Expand Down