Skip to content

Commit 07f29af

Browse files
authored
perf: add receiver type cache (#94)
Signed-off-by: Timon Wong <[email protected]>
1 parent 858975d commit 07f29af

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

internal/rules/rules.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"go/types"
88
"io"
99
"strings"
10+
11+
"golang.org/x/tools/go/types/typeutil"
1012
)
1113

1214
var ErrInvalidRule = errors.New("invalid rule format")
@@ -41,9 +43,19 @@ func (rs *Ruleset) Match(fn *types.Func) bool {
4143
return false
4244
}
4345

44-
func receiverTypeOf(recvType types.Type) string {
45-
buf := &strings.Builder{}
46+
var receiverTypeCache = typeutil.Map{}
47+
48+
func receiverTypeOf(recvType types.Type) (repr string) {
49+
if val := receiverTypeCache.At(recvType); val != nil {
50+
repr, _ = val.(string)
51+
return repr
52+
}
53+
54+
defer func() {
55+
receiverTypeCache.Set(recvType, repr)
56+
}()
4657

58+
buf := &strings.Builder{}
4759
var recvNamed *types.Named
4860
switch recvType := recvType.(type) {
4961
case *types.Pointer:

0 commit comments

Comments
 (0)