1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:14:45 -07:00

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 <dominik@honnef.co>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Dominik Honnef 2020-05-03 07:01:05 +02:00
parent 9f0e5ee6c7
commit 26f46d2f7e

View File

@ -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 {