]> Cypherpunks.ru repositories - gostls13.git/commit
cmd/compile: add a cache to interface type switches
authorKeith Randall <khr@golang.org>
Wed, 6 Sep 2023 20:59:35 +0000 (13:59 -0700)
committerKeith Randall <khr@golang.org>
Fri, 6 Oct 2023 15:44:08 +0000 (15:44 +0000)
commit39263f34a307814a74823b280a313829dad374e5
tree00b7319dae03b08bbab8b8fbf3d15b583f49a72f
parent28f4ea16a240af6c5a417e20be77745329f817f1
cmd/compile: add a cache to interface type switches

That way we don't need to call into the runtime when the type being
switched on has been seen many times before.

The cache is just a hash table of a sample of all the concrete types
that have been switched on at that source location.  We record the
matching case number and the resulting itab for each concrete input
type.

The caches seldom get large. The only two in a run of all.bash that
get more than 100 entries, even with the sampling rate set to 1, are

test/fixedbugs/issue29264.go, with 101
test/fixedbugs/issue29312.go, with 254

Both happen at the type switch in fmt.(*pp).handleMethods, perhaps
unsurprisingly.

name                                 old time/op  new time/op  delta
SwitchInterfaceTypePredictable-24    25.8ns ± 2%   2.5ns ± 3%  -90.43%  (p=0.000 n=10+10)
SwitchInterfaceTypeUnpredictable-24  37.5ns ± 2%  11.2ns ± 1%  -70.02%  (p=0.000 n=10+10)

Change-Id: I4961ac9547b7f15b03be6f55cdcb972d176955eb
Reviewed-on: https://go-review.googlesource.com/c/go/+/526658
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@google.com>
src/cmd/compile/internal/ssagen/ssa.go
src/cmd/compile/internal/test/inl_test.go
src/cmd/compile/internal/walk/switch.go
src/internal/abi/switch.go
src/runtime/iface.go
test/codegen/switch.go