aboutsummaryrefslogtreecommitdiff
path: root/test/codegen
diff options
context:
space:
mode:
authorKeith Randall <khr@golang.org>2025-01-06 15:28:22 -0800
committerKeith Randall <khr@golang.org>2025-02-13 13:03:07 -0800
commit89c2f282dc84a9b3842dca375a4635305c86ad9b (patch)
treede028e17b08467700248ad72c89fb9d51889bc25 /test/codegen
parent43b7e670401401b2e7536b4931df8b29a25994c7 (diff)
downloadgo-89c2f282dc84a9b3842dca375a4635305c86ad9b.tar.xz
cmd/compile: move []byte->string map key optimization to ssa
If we call slicebytetostring immediately (with no intervening writes) before calling map access or delete functions with the resulting string as the key, then we can just use the ptr/len of the slicebytetostring argument as the key. This avoids an allocation. Fixes #44898 Update #71132 There's old code in cmd/compile/internal/walk/order.go that handles some of these cases. 1. m[string(b)] 2. s := string(b); m[s] 3. m[[2]string{string(b1),string(b2)}] The old code handled cases 1&3. The new code handles cases 1&2. We'll leave the old code around to keep 3 working, although it seems not terribly common. Case 2 happens particularly after inlining, so it is pretty common. Change-Id: I8913226ca79d2c65f4e2bd69a38ac8c976a57e43 Reviewed-on: https://go-review.googlesource.com/c/go/+/640656 Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'test/codegen')
-rw-r--r--test/codegen/maps.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/test/codegen/maps.go b/test/codegen/maps.go
index c4aed33545..860b2c2cbd 100644
--- a/test/codegen/maps.go
+++ b/test/codegen/maps.go
@@ -66,6 +66,28 @@ func LookupStringConversionKeyedArrayLit(m map[[2]string]int, bytes []byte) int
return m[[2]string{0: string(bytes)}]
}
+func LookupStringConversion1(m map[string]int, bytes []byte) int {
+ // amd64:-`.*runtime\.slicebytetostring\(`
+ s := string(bytes)
+ return m[s]
+}
+func LookupStringConversion2(m *map[string]int, bytes []byte) int {
+ // amd64:-`.*runtime\.slicebytetostring\(`
+ s := string(bytes)
+ return (*m)[s]
+}
+func LookupStringConversion3(m map[string]int, bytes []byte) (int, bool) {
+ // amd64:-`.*runtime\.slicebytetostring\(`
+ s := string(bytes)
+ r, ok := m[s]
+ return r, ok
+}
+func DeleteStringConversion(m map[string]int, bytes []byte) {
+ // amd64:-`.*runtime\.slicebytetostring\(`
+ s := string(bytes)
+ delete(m, s)
+}
+
// ------------------- //
// Map Clear //
// ------------------- //