aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/walk
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 /src/cmd/compile/internal/walk
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 'src/cmd/compile/internal/walk')
-rw-r--r--src/cmd/compile/internal/walk/order.go8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/walk/order.go b/src/cmd/compile/internal/walk/order.go
index 858fc706ab..8967b7dbba 100644
--- a/src/cmd/compile/internal/walk/order.go
+++ b/src/cmd/compile/internal/walk/order.go
@@ -334,6 +334,14 @@ func (o *orderState) mapKeyTemp(outerPos src.XPos, t *types.Type, n ir.Node) ir.
// It would be nice to handle these generally, but because
// []byte keys are not allowed in maps, the use of string(k)
// comes up in important cases in practice. See issue 3512.
+//
+// Note that this code does not handle the case:
+//
+// s := string(k)
+// x = m[s]
+//
+// Cases like this are handled during SSA, search for slicebytetostring
+// in ../ssa/_gen/generic.rules.
func mapKeyReplaceStrConv(n ir.Node) bool {
var replaced bool
switch n.Op() {