From 47112996619da0683eb15e611a1e2df85416feee Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Thu, 17 Aug 2023 14:20:21 -0700 Subject: cmd/compile: use jump tables for large type switches MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For large interface -> concrete type switches, we can use a jump table on some bits of the type hash instead of a binary search on the type hash. name old time/op new time/op delta SwitchTypePredictable-24 1.99ns ± 2% 1.78ns ± 5% -10.87% (p=0.000 n=10+10) SwitchTypeUnpredictable-24 11.0ns ± 1% 9.1ns ± 2% -17.55% (p=0.000 n=7+9) Change-Id: Ida4768e5d62c3ce1c2701288b72664aaa9e64259 Reviewed-on: https://go-review.googlesource.com/c/go/+/521497 Reviewed-by: Keith Randall Auto-Submit: Keith Randall TryBot-Result: Gopher Robot Reviewed-by: Cherry Mui Run-TryBot: Keith Randall --- test/codegen/switch.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'test/codegen') diff --git a/test/codegen/switch.go b/test/codegen/switch.go index 603e0befbb..556d02a162 100644 --- a/test/codegen/switch.go +++ b/test/codegen/switch.go @@ -99,3 +99,22 @@ func mimetype(ext string) string { return "" } } + +// use jump tables for type switches to concrete types. +func typeSwitch(x any) int { + // amd64:`JMP\s\(.*\)\(.*\)$` + // arm64:`MOVD\s\(R.*\)\(R.*<<3\)`,`JMP\s\(R.*\)$` + switch x.(type) { + case int: + return 0 + case int8: + return 1 + case int16: + return 2 + case int32: + return 3 + case int64: + return 4 + } + return 7 +} -- cgit v1.3-5-g9baa