From 26f46d2f7ef8d36fd8840d0a2d8e1a7ff1dad367 Mon Sep 17 00:00:00 2001 From: Dominik Honnef Date: Sun, 3 May 2020 07:01:05 +0200 Subject: [PATCH] go/types/objectpath: cache result of call to scope.Names Names is a somewhat expensive method, which sorts the returned slice. Avoiding the extra call helps clients that use objectpath a lot. Change-Id: I49a3445bb4f0056d1b915a2104e136925ee9dbf9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/231518 Run-TryBot: Dominik Honnef TryBot-Result: Gobot Gobot Reviewed-by: Michael Matloob --- go/types/objectpath/objectpath.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/go/types/objectpath/objectpath.go b/go/types/objectpath/objectpath.go index 882e3b3d8a..cffd7acbee 100644 --- a/go/types/objectpath/objectpath.go +++ b/go/types/objectpath/objectpath.go @@ -226,7 +226,8 @@ func For(obj types.Object) (Path, error) { // the best paths because non-types may // refer to types, but not the reverse. empty := make([]byte, 0, 48) // initial space - for _, name := range scope.Names() { + names := scope.Names() + for _, name := range names { o := scope.Lookup(name) tname, ok := o.(*types.TypeName) if !ok { @@ -253,7 +254,7 @@ func For(obj types.Object) (Path, error) { // Then inspect everything else: // non-types, and declared methods of defined types. - for _, name := range scope.Names() { + for _, name := range names { o := scope.Lookup(name) path := append(empty, name...) if _, ok := o.(*types.TypeName); !ok {