]> Cypherpunks repositories - gostls13.git/commit
runtime/chan.go: improve closed channel receive performance
authorchamply <champly1993@gmail.com>
Tue, 12 Apr 2022 01:54:45 +0000 (01:54 +0000)
committerKeith Randall <khr@golang.org>
Tue, 12 Apr 2022 02:18:23 +0000 (02:18 +0000)
commitca7c6ef33d9eca2dbc7eb46601a051dc7dc4e411
tree2661d7a569a672e6a97df26c6c6705a32c7d540d
parenta362d5461483190cbaf995e6cb7aaa47c32ebe36
runtime/chan.go: improve closed channel receive performance

Use this benchmark ut:

```go
func BenchmarkReceiveDataFromClosedChan(b *testing.B) {
count := b.N
ch := make(chan struct{}, count)
for i := 0; i < count; i++ {
ch <- struct{}{}
}

b.ResetTimer()
for range ch {
}
}
```

Benchmark 10 times(`go test -bench=.`), and then use `benchstat` got the result:

```shell
name                         old time/op  new time/op  delta
ReceiveDataFromClosedChan-5  12.0ns ± 1%  11.4ns ± 0%  -5.54%  (p=0.000 n=10+8)
```

Fixes: #52067
Change-Id: I8db398cc8c04a46cb66ffb6768ab72a87903812f
GitHub-Last-Rev: 1e0142416f223c1ebfc4a7c136bb8fca242d7934
GitHub-Pull-Request: golang/go#52068
Reviewed-on: https://go-review.googlesource.com/c/go/+/396884
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Keith Randall <khr@google.com>
Auto-Submit: Keith Randall <khr@google.com>
src/runtime/chan.go
src/runtime/chan_test.go