aboutsummaryrefslogtreecommitdiff
path: root/test/chancap.go
diff options
context:
space:
mode:
authorMartin Möhrmann <moehrmann@google.com>2017-08-13 20:03:02 +0200
committerMartin Möhrmann <moehrmann@google.com>2017-08-15 05:54:24 +0000
commit8a6e51aede746d1f7ffec5982c153bf3cdabce4f (patch)
treeddab941bd0c77d2235f9d477ac3349da4a2ec38e /test/chancap.go
parent4c55774304e5c6aecae55839ca34981acccebd85 (diff)
downloadgo-8a6e51aede746d1f7ffec5982c153bf3cdabce4f.tar.gz
go-8a6e51aede746d1f7ffec5982c153bf3cdabce4f.zip
cmd/compile: generate makechan calls with int arguments
Where possible generate calls to runtime makechan with int arguments during compile time instead of makechan with int64 arguments. This eliminates converting arguments for calls to makechan with int64 arguments for platforms where int64 values do not fit into arguments of type int. A similar optimization for makeslice was introduced in CL golang.org/cl/27851. 386: name old time/op new time/op delta MakeChan/Byte 52.4ns ± 6% 45.0ns ± 1% -14.14% (p=0.000 n=10+10) MakeChan/Int 54.5ns ± 1% 49.1ns ± 1% -9.87% (p=0.000 n=10+10) MakeChan/Ptr 150ns ± 1% 143ns ± 0% -4.38% (p=0.000 n=9+7) MakeChan/Struct/0 49.2ns ± 2% 43.2ns ± 2% -12.27% (p=0.000 n=10+10) MakeChan/Struct/32 81.7ns ± 2% 76.2ns ± 1% -6.71% (p=0.000 n=10+10) MakeChan/Struct/40 88.4ns ± 2% 82.5ns ± 2% -6.60% (p=0.000 n=10+10) AMD64: name old time/op new time/op delta MakeChan/Byte 83.4ns ± 8% 80.8ns ± 3% ~ (p=0.171 n=10+10) MakeChan/Int 101ns ± 3% 101ns ± 2% ~ (p=0.412 n=10+10) MakeChan/Ptr 128ns ± 1% 128ns ± 1% ~ (p=0.191 n=10+10) MakeChan/Struct/0 67.6ns ± 3% 68.7ns ± 4% ~ (p=0.224 n=10+10) MakeChan/Struct/32 138ns ± 1% 139ns ± 1% ~ (p=0.185 n=10+9) MakeChan/Struct/40 154ns ± 1% 154ns ± 1% -0.55% (p=0.027 n=10+9) Change-Id: Ie854cb066007232c5e9f71ea7d6fe27e81a9c050 Reviewed-on: https://go-review.googlesource.com/55140 Run-TryBot: Martin Möhrmann <moehrmann@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'test/chancap.go')
-rw-r--r--test/chancap.go43
1 files changed, 41 insertions, 2 deletions
diff --git a/test/chancap.go b/test/chancap.go
index b3e40233f5..b08478a13c 100644
--- a/test/chancap.go
+++ b/test/chancap.go
@@ -8,8 +8,17 @@
package main
+import (
+ "strings"
+ "unsafe"
+)
+
+type T chan int
+
+const ptrSize = unsafe.Sizeof((*byte)(nil))
+
func main() {
- c := make(chan int, 10)
+ c := make(T, 10)
if len(c) != 0 || cap(c) != 10 {
println("chan len/cap ", len(c), cap(c), " want 0 10")
panic("fail")
@@ -23,9 +32,39 @@ func main() {
panic("fail")
}
- c = make(chan int)
+ c = make(T)
if len(c) != 0 || cap(c) != 0 {
println("chan len/cap ", len(c), cap(c), " want 0 0")
panic("fail")
}
+
+ n := -1
+ shouldPanic("makechan: size out of range", func() { _ = make(T, n) })
+ shouldPanic("makechan: size out of range", func() { _ = make(T, int64(n)) })
+ if ptrSize == 8 {
+ n = 1 << 20
+ n <<= 20
+ shouldPanic("makechan: size out of range", func() { _ = make(T, n) })
+ n <<= 20
+ shouldPanic("makechan: size out of range", func() { _ = make(T, n) })
+ } else {
+ n = 1<<31 - 1
+ shouldPanic("makechan: size out of range", func() { _ = make(T, n) })
+ shouldPanic("makechan: size out of range", func() { _ = make(T, int64(n)) })
+ }
+}
+
+func shouldPanic(str string, f func()) {
+ defer func() {
+ err := recover()
+ if err == nil {
+ panic("did not panic")
+ }
+ s := err.(error).Error()
+ if !strings.Contains(s, str) {
+ panic("got panic " + s + ", want " + str)
+ }
+ }()
+
+ f()
}