Skip to content

Commit 16a7d9c

Browse files
authored
Cache the result of comparer.identical for a massive speedup. (#26)
1 parent 79d9c58 commit 16a7d9c

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

identical.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ package modver
33
import "go/types"
44

55
// https://golang.org/ref/spec#Type_identity
6-
func (c *comparer) identical(a, b types.Type) bool {
6+
func (c *comparer) identical(a, b types.Type) (res bool) {
7+
tp := typePair{a: a, b: b}
8+
if res, ok := c.identicache[tp]; ok {
9+
return res
10+
}
11+
defer func() { c.identicache[tp] = res }()
12+
713
if types.Identical(a, b) {
814
return true
915
}

types.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ import (
1212

1313
type (
1414
comparer struct {
15-
stack []typePair
16-
cache map[typePair]Result
15+
stack []typePair
16+
cache map[typePair]Result
17+
identicache map[typePair]bool
1718
}
1819
typePair struct{ a, b types.Type }
1920
)
2021

2122
func newComparer() *comparer {
22-
return &comparer{cache: make(map[typePair]Result)}
23+
return &comparer{
24+
cache: make(map[typePair]Result),
25+
identicache: make(map[typePair]bool),
26+
}
2327
}
2428

2529
func (c *comparer) compareTypes(older, newer types.Type) (res Result) {

0 commit comments

Comments
 (0)