Skip to content

Commit 566e3e0

Browse files
committed
cmd/compile: avoid runtime call during switch string(byteslice)
This triggers three times while building std, once in image/png and twice in go/internal/gccgoimporter. There are no instances in std in which a more aggressive optimization would have triggered. This doesn't necessarily avoid an allocation, because escape analysis is already able in many cases to use a temporary backing for the string, but it does at a minimum avoid the runtime call and copy. Fixes #24937 Change-Id: I7019e85638ba8cd7e2f03890e672558b858579bc Reviewed-on: https://go-review.googlesource.com/108035 Run-TryBot: Josh Bleecher Snyder <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent f6ca6ed commit 566e3e0

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/cmd/compile/internal/gc/swt.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,34 @@ func (s *exprSwitch) walk(sw *Node) {
254254
}
255255
}
256256

257+
// Given "switch string(byteslice)",
258+
// with all cases being constants (or the default case),
259+
// use a zero-cost alias of the byte slice.
260+
// In theory, we could be more aggressive,
261+
// allowing any side-effect-free expressions in cases,
262+
// but it's a bit tricky because some of that information
263+
// is unavailable due to the introduction of temporaries during order.
264+
// Restricting to constants is simple and probably powerful enough.
265+
// Do this before calling walkexpr on cond,
266+
// because walkexpr will lower the string
267+
// conversion into a runtime call.
268+
// See issue 24937 for more discussion.
269+
if cond.Op == OARRAYBYTESTR {
270+
ok := true
271+
for _, cas := range sw.List.Slice() {
272+
if cas.Op != OCASE {
273+
Fatalf("switch string(byteslice) bad op: %v", cas.Op)
274+
}
275+
if cas.Left != nil && !Isconst(cas.Left, CTSTR) {
276+
ok = false
277+
break
278+
}
279+
}
280+
if ok {
281+
cond.Op = OARRAYBYTESTRTMP
282+
}
283+
}
284+
257285
cond = walkexpr(cond, &sw.Ninit)
258286
t := sw.Type
259287
if t == nil {

test/fixedbugs/issue24937.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run
2+
3+
// Copyright 2018 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
func main() {
10+
x := []byte{'a'}
11+
switch string(x) {
12+
case func() string { x[0] = 'b'; return "b" }():
13+
panic("FAIL")
14+
}
15+
}

0 commit comments

Comments
 (0)