Skip to content

Commit

Permalink
ruleguard/typematch: replace Split with Index to avoid allocs (#414)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
quasilyte authored Oct 1, 2022
1 parent 92ca799 commit ae42498
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions ruleguard/typematch/typematch.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,14 @@ func (p *Pattern) matchIdentical(state *MatcherState, sub *pattern, typ types.Ty
}
pkgPath := sub.value.([2]string)[0]
typeName := sub.value.([2]string)[1]
// obj.Pkg().Path() may be in a vendor directory.
path := strings.SplitAfter(obj.Pkg().Path(), "/vendor/")
return path[len(path)-1] == pkgPath && typeName == obj.Name()
if typeName != obj.Name() {
return false
}
objPath := obj.Pkg().Path()
if vendorPos := strings.Index(objPath, "/vendor/"); vendorPos != -1 {
objPath = objPath[vendorPos+len("/vendor/"):]
}
return objPath == pkgPath

case opFuncNoSeq:
typ, ok := typ.(*types.Signature)
Expand Down

0 comments on commit ae42498

Please sign in to comment.