Skip to content

Commit ae42498

Browse files
authored
ruleguard/typematch: replace Split with Index to avoid allocs (#414)
We don't need to create `[]string` to compare the substring. This change reduces the amount of allocations when comparing named types via typematch package.
1 parent 92ca799 commit ae42498

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

ruleguard/typematch/typematch.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,14 @@ func (p *Pattern) matchIdentical(state *MatcherState, sub *pattern, typ types.Ty
507507
}
508508
pkgPath := sub.value.([2]string)[0]
509509
typeName := sub.value.([2]string)[1]
510-
// obj.Pkg().Path() may be in a vendor directory.
511-
path := strings.SplitAfter(obj.Pkg().Path(), "/vendor/")
512-
return path[len(path)-1] == pkgPath && typeName == obj.Name()
510+
if typeName != obj.Name() {
511+
return false
512+
}
513+
objPath := obj.Pkg().Path()
514+
if vendorPos := strings.Index(objPath, "/vendor/"); vendorPos != -1 {
515+
objPath = objPath[vendorPos+len("/vendor/"):]
516+
}
517+
return objPath == pkgPath
513518

514519
case opFuncNoSeq:
515520
typ, ok := typ.(*types.Signature)

0 commit comments

Comments
 (0)