1
0
mirror of https://github.com/golang/go synced 2024-11-22 20:40:03 -07:00

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 <cuong.manhle.vn@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Cuong Manh Le 2020-08-29 03:25:21 +07:00
parent 49bae98495
commit 27a30186ab
3 changed files with 22 additions and 3 deletions

View File

@ -251,10 +251,8 @@ func walkselectcases(cases *Nodes) []*Node {
r = typecheck(r, ctxStmt)
init = append(init, r)
// No initialization for order; runtime.selectgo is responsible for that.
order := temp(types.NewArray(types.Types[TUINT16], 2*int64(ncas)))
r = nod(OAS, order, nil)
r = typecheck(r, ctxStmt)
init = append(init, r)
var pc0, pcs *Node
if flag_race {

View File

@ -118,6 +118,7 @@ func selectgo(cas0 *scase, order0 *uint16, pc0 *uintptr, nsends, nrecvs int, blo
scases := cas1[:ncases:ncases]
pollorder := order1[:ncases:ncases]
lockorder := order1[ncases:][:ncases:ncases]
// NOTE: pollorder/lockorder's underlying array was not zero-initialized by compiler.
// Even when raceenabled is true, there might be select
// statements in packages compiled without -race (e.g.,

20
test/codegen/select.go Normal file
View File

@ -0,0 +1,20 @@
// asmcheck
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package codegen
func f() {
ch1 := make(chan int)
ch2 := make(chan int)
for {
// amd64:-`MOVQ\t[$]0, ""..autotmp_3`
select {
case <-ch1:
case <-ch2:
default:
}
}
}