Skip to content

Commit

Permalink
unused: reduce number of calls to types.Implements
Browse files Browse the repository at this point in the history
types.Implements is a relatively expensive operation, producing a lot of
garbage in lookupFieldOrMethod.

When I tested 2019.1 on github.com/cockroachdb/cockroach/pkg/...,
lookupFieldOrMethod allocated 23 GB of garbage.

Updates gh-394
  • Loading branch information
dominikh committed Jan 28, 2019
1 parent 649d24d commit 4dc4b9d
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions unused/unused.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,22 @@ func (c *Checker) processTypes(pkg *lint.Pkg) {
for i := 0; i < iface.NumEmbeddeds(); i++ {
c.graph.markUsedBy(iface.Embedded(i), iface)
}
namedLoop:
for obj, objPtr := range named {
if !types.Implements(obj, iface) && !types.Implements(objPtr, iface) {
continue
switch obj.Underlying().(type) {
case *types.Interface:
// pointers to interfaces have no methods, only checking non-pointer
if !types.Implements(obj, iface) {
continue namedLoop
}
default:
// pointer receivers include the method set of non-pointer receivers,
// only checking pointer
if !types.Implements(objPtr, iface) {
continue namedLoop
}
}

ifaceMethods := make(map[string]struct{}, iface.NumMethods())
n := iface.NumMethods()
for i := 0; i < n; i++ {
Expand Down

0 comments on commit 4dc4b9d

Please sign in to comment.