Skip to content

Commit

Permalink
Cache the result of comparer.identical for a massive speedup. (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobg authored Jun 6, 2024
1 parent 79d9c58 commit 16a7d9c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 7 additions & 1 deletion identical.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ package modver
import "go/types"

// https://golang.org/ref/spec#Type_identity
func (c *comparer) identical(a, b types.Type) bool {
func (c *comparer) identical(a, b types.Type) (res bool) {
tp := typePair{a: a, b: b}
if res, ok := c.identicache[tp]; ok {
return res
}
defer func() { c.identicache[tp] = res }()

if types.Identical(a, b) {
return true
}
Expand Down
10 changes: 7 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ import (

type (
comparer struct {
stack []typePair
cache map[typePair]Result
stack []typePair
cache map[typePair]Result
identicache map[typePair]bool
}
typePair struct{ a, b types.Type }
)

func newComparer() *comparer {
return &comparer{cache: make(map[typePair]Result)}
return &comparer{
cache: make(map[typePair]Result),
identicache: make(map[typePair]bool),
}
}

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

0 comments on commit 16a7d9c

Please sign in to comment.