aboutsummaryrefslogtreecommitdiff
path: root/test/escape_slice.go
diff options
context:
space:
mode:
authorCuong Manh Le <cuong.manhle.vn@gmail.com>2020-03-29 01:19:50 +0700
committerMatthew Dempsky <mdempsky@google.com>2020-03-31 21:51:51 +0000
commit7b30a2d268ccb56221d0d8b149300548ce0308e1 (patch)
tree58f12d07412a379750989bf05c9b250b52c1d51d /test/escape_slice.go
parent8114242359a32dbbfe44cf6cc83c48cca7d6c126 (diff)
downloadgo-7b30a2d268ccb56221d0d8b149300548ce0308e1.tar.gz
go-7b30a2d268ccb56221d0d8b149300548ce0308e1.zip
cmd/compile: make isSmallMakeSlice checks slice cap only
If slice cap is not set, it will be equal to slice len. So isSmallMakeSlice only needs to check whether slice cap is constant. While at it, also add test to make sure panicmakeslicecap is called when make slice contains invalid non-constant len. For this benchmark: func BenchmarkMakeSliceNonConstantLen(b *testing.B) { len := 1 for i := 0; i < b.N; i++ { s := make([]int, len, 2) _ = s } } Result compare with parent: name old time/op new time/op delta MakeSliceNonConstantLen-12 18.4ns ± 1% 0.2ns ± 2% -98.66% (p=0.008 n=5+5) Fixes #37975 Change-Id: I4bc926361bc2ffeab4cfaa888ef0a30cbc3b80e8 Reviewed-on: https://go-review.googlesource.com/c/go/+/226278 Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'test/escape_slice.go')
-rw-r--r--test/escape_slice.go22
1 files changed, 15 insertions, 7 deletions
diff --git a/test/escape_slice.go b/test/escape_slice.go
index 03053cf326..d2cdaa6a01 100644
--- a/test/escape_slice.go
+++ b/test/escape_slice.go
@@ -18,28 +18,28 @@ var sink interface{}
func slice0() {
var s []*int
// BAD: i should not escape
- i := 0 // ERROR "moved to heap: i"
+ i := 0 // ERROR "moved to heap: i"
s = append(s, &i)
_ = s
}
func slice1() *int {
var s []*int
- i := 0 // ERROR "moved to heap: i"
+ i := 0 // ERROR "moved to heap: i"
s = append(s, &i)
return s[0]
}
func slice2() []*int {
var s []*int
- i := 0 // ERROR "moved to heap: i"
+ i := 0 // ERROR "moved to heap: i"
s = append(s, &i)
return s
}
func slice3() *int {
var s []*int
- i := 0 // ERROR "moved to heap: i"
+ i := 0 // ERROR "moved to heap: i"
s = append(s, &i)
for _, p := range s {
return p
@@ -48,7 +48,7 @@ func slice3() *int {
}
func slice4(s []*int) { // ERROR "s does not escape"
- i := 0 // ERROR "moved to heap: i"
+ i := 0 // ERROR "moved to heap: i"
s[0] = &i
}
@@ -56,14 +56,14 @@ func slice5(s []*int) { // ERROR "s does not escape"
if s != nil {
s = make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
}
- i := 0 // ERROR "moved to heap: i"
+ i := 0 // ERROR "moved to heap: i"
s[0] = &i
}
func slice6() {
s := make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
// BAD: i should not escape
- i := 0 // ERROR "moved to heap: i"
+ i := 0 // ERROR "moved to heap: i"
s[0] = &i
_ = s
}
@@ -93,6 +93,14 @@ func slice10() []*int {
return s
}
+func slice11() {
+ i := 2
+ s := make([]int, 2, 3) // ERROR "make\(\[\]int, 2, 3\) does not escape"
+ s = make([]int, i, 3) // ERROR "make\(\[\]int, i, 3\) does not escape"
+ s = make([]int, i, 1) // ERROR "make\(\[\]int, i, 1\) does not escape"
+ _ = s
+}
+
func envForDir(dir string) []string { // ERROR "dir does not escape"
env := os.Environ()
return mergeEnvLists([]string{"PWD=" + dir}, env) // ERROR ".PWD=. \+ dir escapes to heap" "\[\]string literal does not escape"