From 3bb9299f9c1c5730317be667fe3947697dd3d6b8 Mon Sep 17 00:00:00 2001 From: Dominik Honnef Date: Mon, 28 Jan 2019 03:39:17 +0100 Subject: [PATCH] unused: reduce number of calls to types.Implements 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 --- unused/unused.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/unused/unused.go b/unused/unused.go index 9c69277ee..c16f3fe3a 100644 --- a/unused/unused.go +++ b/unused/unused.go @@ -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++ {