1
0
mirror of https://github.com/golang/go synced 2024-09-30 12:08:32 -06:00
go/src/cmd
Robert Findley ddd8bc1b07 go/types, types2: optimize instance lookup in LookupFieldOrMethod
LookupFieldOrMethod appears as a hotspot when benchmarking gopls'
auto-completion. In particular, instanceLookup.add was allocating in the
common case of structs with no embedding.

This is easily fixed, by using a small array in front of the map inside
of instanceLookup. Do this, and additionally add a microbenchmark.

The benchmark improvement is significant:

name                    old time/op    new time/op    delta
LookupFieldOrMethod-12     388µs ± 6%     154µs ± 3%  -60.36%  (p=0.000 n=10+10)

name                    old alloc/op   new alloc/op   delta
LookupFieldOrMethod-12     152kB ± 0%       2kB ± 0%  -98.77%  (p=0.000 n=9+10)

name                    old allocs/op  new allocs/op  delta
LookupFieldOrMethod-12     1.41k ± 0%     0.07k ± 0%  -95.38%  (p=0.000 n=10+10)

It should also be noted that instanceLookup is used elsewhere, in
particular by validType. In those contexts, the scope is not just the
current type but the entire package, and so the newly added buffer is
likely to simply cause extra Identical checks. Nevertheless, those
checks are cheap, and on balance the improved LookupFieldOrMethod
performance leads overall to improved type-checking performance.
Standard library benchmark results varied by package, but type checking
speed for many packages improved by ~5%, with allocations improved by
~10%. If this weren't the case we could let the caller control the
buffer size, but that optimization doesn't seem necessary at this time.

For example:

Check/http/funcbodies/noinfo-12            71.5ms ± 4%    67.3ms ± 2%   -5.90%  (p=0.000 n=20+20)
Check/http/funcbodies/noinfo-12              244k ± 0%      219k ± 0%  -10.36%  (p=0.000 n=19+19)

Updates golang/go#53992

Change-Id: I10b6deb3819ab562dbbe1913f12b977cf956dd50
Reviewed-on: https://go-review.googlesource.com/c/go/+/423935
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
2022-10-13 15:31:35 +00:00
..
addr2line all: gofmt main repo 2022-04-11 16:34:30 +00:00
api cmd/api: skip tests on platforms that cannot exec 2022-09-27 18:26:56 +00:00
asm cmd/asm: use strings.TrimSuffix 2022-09-29 09:05:01 +00:00
buildid cmd/buildid: reject rewriting legacy buildids 2022-06-06 14:30:53 +00:00
cgo cmd/cgo, cmd/compile, cmd/link: remove old style build tags 2022-10-04 19:36:17 +00:00
compile go/types, types2: optimize instance lookup in LookupFieldOrMethod 2022-10-13 15:31:35 +00:00
covdata cmd/{cover,covdata}: minor code cleanups 2022-10-12 12:50:46 +00:00
cover cmd/{cover,covdata}: minor code cleanups 2022-10-12 12:50:46 +00:00
dist cmd/compile/internal/types2: use internal/types/errors instead of local error codes 2022-10-10 16:01:34 +00:00
doc cmd/doc: use strings.Builder 2022-09-06 17:12:04 +00:00
fix cmd/fix: use strings.Cut 2022-09-29 22:58:43 +00:00
go cmd/go: add Context parameter to download function 2022-10-12 14:36:26 +00:00
gofmt cmd/gofmt: use SkipObjectResolution with -s as well 2022-05-19 20:06:14 +00:00
internal all: remove redundant type conversion 2022-10-06 04:37:00 +00:00
link cmd/link: optimize PPC64 inline plt sequences if local 2022-10-05 14:12:24 +00:00
nm cmd/nm: use print-style not printf-style 2022-09-29 09:05:07 +00:00
objdump cmd/internal/sys: migrate support.go functions to new internal pkg 2022-10-04 18:08:15 +00:00
pack cmd/pack: use strings.Builder 2022-09-08 14:33:43 +00:00
pprof cmd/pprof: fix addr calculation for Windows 2022-08-20 00:56:30 +00:00
test2json cmd/test2json: add signal handler 2022-08-26 17:38:40 +00:00
trace all: use fmt.Appendf 2022-10-04 22:43:12 +00:00
vendor cmd: update vendored golang.org/x/tools for multiple error wrapping 2022-09-23 15:58:01 +00:00
vet all: replace [0-9] with \d in regexps 2022-09-27 14:14:42 +00:00
go.mod cmd: update vendored golang.org/x/tools for multiple error wrapping 2022-09-23 15:58:01 +00:00
go.sum cmd: update vendored golang.org/x/tools for multiple error wrapping 2022-09-23 15:58:01 +00:00
README.vendor

See src/README.vendor for information on loading vendored packages
and updating the vendor directory.