Skip to content

Commit 27a3018

Browse files
committed
cmd/compile,runtime: skip zero'ing order array for select statements
The order array was zero initialized by the compiler, but ends up being overwritten by the runtime anyway. So let the runtime takes full responsibility for initializing, save us one instruction per select. Fixes #40399 Change-Id: Iec1eca27ad7180d4fcb3cc9ef97348206b7fe6b8 Reviewed-on: https://go-review.googlesource.com/c/go/+/251517 Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 49bae98 commit 27a3018

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,8 @@ func walkselectcases(cases *Nodes) []*Node {
251251
r = typecheck(r, ctxStmt)
252252
init = append(init, r)
253253

254+
// No initialization for order; runtime.selectgo is responsible for that.
254255
order := temp(types.NewArray(types.Types[TUINT16], 2*int64(ncas)))
255-
r = nod(OAS, order, nil)
256-
r = typecheck(r, ctxStmt)
257-
init = append(init, r)
258256

259257
var pc0, pcs *Node
260258
if flag_race {

src/runtime/select.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func selectgo(cas0 *scase, order0 *uint16, pc0 *uintptr, nsends, nrecvs int, blo
118118
scases := cas1[:ncases:ncases]
119119
pollorder := order1[:ncases:ncases]
120120
lockorder := order1[ncases:][:ncases:ncases]
121+
// NOTE: pollorder/lockorder's underlying array was not zero-initialized by compiler.
121122

122123
// Even when raceenabled is true, there might be select
123124
// statements in packages compiled without -race (e.g.,

test/codegen/select.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// asmcheck
2+
3+
// Copyright 2020 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 codegen
8+
9+
func f() {
10+
ch1 := make(chan int)
11+
ch2 := make(chan int)
12+
for {
13+
// amd64:-`MOVQ\t[$]0, ""..autotmp_3`
14+
select {
15+
case <-ch1:
16+
case <-ch2:
17+
default:
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)