]> Cypherpunks repositories - gostls13.git/commit
cmd/compile: move []byte->string map key optimization to ssa
authorKeith Randall <khr@golang.org>
Mon, 6 Jan 2025 23:28:22 +0000 (15:28 -0800)
committerKeith Randall <khr@golang.org>
Thu, 13 Feb 2025 21:03:07 +0000 (13:03 -0800)
commit89c2f282dc84a9b3842dca375a4635305c86ad9b
treede028e17b08467700248ad72c89fb9d51889bc25
parent43b7e670401401b2e7536b4931df8b29a25994c7
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>
src/cmd/compile/internal/ssa/_gen/generic.rules
src/cmd/compile/internal/ssa/rewrite.go
src/cmd/compile/internal/ssa/rewritegeneric.go
src/cmd/compile/internal/walk/order.go
test/codegen/maps.go