mirror of
https://github.com/golang/go.git
synced 2025-05-29 03:11:26 +00:00
When appending, if the backing store doesn't escape and a constant-sized backing store is big enough, use a constant-sized stack-allocated backing store instead of allocating it from the heap. cmd/go is <0.1% bigger. As an example of how this helps, if you edit strings/strings.go:FieldsFunc to replace spans := make([]span, 0, 32) with var spans []span then this CL removes the first 2 allocations that are part of the growth sequence: │ base │ exp │ │ allocs/op │ allocs/op vs base │ FieldsFunc/ASCII/16-24 3.000 ± ∞ ¹ 2.000 ± ∞ ¹ -33.33% (p=0.008 n=5) FieldsFunc/ASCII/256-24 7.000 ± ∞ ¹ 5.000 ± ∞ ¹ -28.57% (p=0.008 n=5) FieldsFunc/ASCII/4096-24 11.000 ± ∞ ¹ 9.000 ± ∞ ¹ -18.18% (p=0.008 n=5) FieldsFunc/ASCII/65536-24 18.00 ± ∞ ¹ 16.00 ± ∞ ¹ -11.11% (p=0.008 n=5) FieldsFunc/ASCII/1048576-24 30.00 ± ∞ ¹ 28.00 ± ∞ ¹ -6.67% (p=0.008 n=5) FieldsFunc/Mixed/16-24 2.000 ± ∞ ¹ 2.000 ± ∞ ¹ ~ (p=1.000 n=5) FieldsFunc/Mixed/256-24 7.000 ± ∞ ¹ 5.000 ± ∞ ¹ -28.57% (p=0.008 n=5) FieldsFunc/Mixed/4096-24 11.000 ± ∞ ¹ 9.000 ± ∞ ¹ -18.18% (p=0.008 n=5) FieldsFunc/Mixed/65536-24 18.00 ± ∞ ¹ 16.00 ± ∞ ¹ -11.11% (p=0.008 n=5) FieldsFunc/Mixed/1048576-24 30.00 ± ∞ ¹ 28.00 ± ∞ ¹ -6.67% (p=0.008 n=5) (Of course, people have spotted and fixed a bunch of allocation sites like this, but now we're ~automatically doing it everywhere going forward.) No significant increases in frame sizes in cmd/go. Change-Id: I301c4d9676667eacdae0058960321041d173751a Reviewed-on: https://go-review.googlesource.com/c/go/+/664299 Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Keith Randall <khr@golang.org>
182 lines
4.1 KiB
Go
182 lines
4.1 KiB
Go
// errorcheck -0 -m -l
|
|
|
|
// Copyright 2015 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.
|
|
|
|
// Test escape analysis for slices.
|
|
|
|
package escape
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
var sink interface{}
|
|
|
|
func slice0() {
|
|
var s []*int
|
|
// BAD: i should not escape
|
|
i := 0 // ERROR "moved to heap: i"
|
|
s = append(s, &i) // ERROR "append does not escape"
|
|
_ = s
|
|
}
|
|
|
|
func slice1() *int {
|
|
var s []*int
|
|
i := 0 // ERROR "moved to heap: i"
|
|
s = append(s, &i) // ERROR "append does not escape"
|
|
return s[0]
|
|
}
|
|
|
|
func slice2() []*int {
|
|
var s []*int
|
|
i := 0 // ERROR "moved to heap: i"
|
|
s = append(s, &i) // ERROR "append escapes to heap"
|
|
return s
|
|
}
|
|
|
|
func slice3() *int {
|
|
var s []*int
|
|
i := 0 // ERROR "moved to heap: i"
|
|
s = append(s, &i) // ERROR "append does not escape"
|
|
for _, p := range s {
|
|
return p
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func slice4(s []*int) { // ERROR "s does not escape"
|
|
i := 0 // ERROR "moved to heap: i"
|
|
s[0] = &i
|
|
}
|
|
|
|
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"
|
|
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"
|
|
s[0] = &i
|
|
_ = s
|
|
}
|
|
|
|
func slice7() *int {
|
|
s := make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
|
|
i := 0 // ERROR "moved to heap: i"
|
|
s[0] = &i
|
|
return s[0]
|
|
}
|
|
|
|
func slice8() {
|
|
i := 0
|
|
s := []*int{&i} // ERROR "\[\]\*int{...} does not escape"
|
|
_ = s
|
|
}
|
|
|
|
func slice9() *int {
|
|
i := 0 // ERROR "moved to heap: i"
|
|
s := []*int{&i} // ERROR "\[\]\*int{...} does not escape"
|
|
return s[0]
|
|
}
|
|
|
|
func slice10() []*int {
|
|
i := 0 // ERROR "moved to heap: i"
|
|
s := []*int{&i} // ERROR "\[\]\*int{...} escapes to heap"
|
|
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 slice12(x []int) *[1]int { // ERROR "leaking param: x to result ~r0 level=0$"
|
|
return (*[1]int)(x)
|
|
}
|
|
|
|
func slice13(x []*int) [1]*int { // ERROR "leaking param: x to result ~r0 level=1$"
|
|
return [1]*int(x)
|
|
}
|
|
|
|
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{...} does not escape"
|
|
}
|
|
|
|
func mergeEnvLists(in, out []string) []string { // ERROR "leaking param content: in" "leaking param content: out" "leaking param: out to result ~r0 level=0"
|
|
NextVar:
|
|
for _, inkv := range in {
|
|
k := strings.SplitAfterN(inkv, "=", 2)[0]
|
|
for i, outkv := range out {
|
|
if strings.HasPrefix(outkv, k) {
|
|
out[i] = inkv
|
|
continue NextVar
|
|
}
|
|
}
|
|
out = append(out, inkv) // ERROR "append escapes to heap"
|
|
}
|
|
return out
|
|
}
|
|
|
|
const (
|
|
IPv4len = 4
|
|
IPv6len = 16
|
|
)
|
|
|
|
var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
|
|
|
|
func IPv4(a, b, c, d byte) IP {
|
|
p := make(IP, IPv6len) // ERROR "make\(IP, 16\) escapes to heap"
|
|
copy(p, v4InV6Prefix)
|
|
p[12] = a
|
|
p[13] = b
|
|
p[14] = c
|
|
p[15] = d
|
|
return p
|
|
}
|
|
|
|
type IP []byte
|
|
|
|
type IPAddr struct {
|
|
IP IP
|
|
Zone string // IPv6 scoped addressing zone
|
|
}
|
|
|
|
type resolveIPAddrTest struct {
|
|
network string
|
|
litAddrOrName string
|
|
addr *IPAddr
|
|
err error
|
|
}
|
|
|
|
var resolveIPAddrTests = []resolveIPAddrTest{
|
|
{"ip", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
|
|
{"ip4", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
|
|
{"ip4:icmp", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
|
|
}
|
|
|
|
func setupTestData() {
|
|
resolveIPAddrTests = append(resolveIPAddrTests, // ERROR "append escapes to heap"
|
|
[]resolveIPAddrTest{ // ERROR "\[\]resolveIPAddrTest{...} does not escape"
|
|
{"ip",
|
|
"localhost",
|
|
&IPAddr{IP: IPv4(127, 0, 0, 1)}, // ERROR "&IPAddr{...} escapes to heap"
|
|
nil},
|
|
{"ip4",
|
|
"localhost",
|
|
&IPAddr{IP: IPv4(127, 0, 0, 1)}, // ERROR "&IPAddr{...} escapes to heap"
|
|
nil},
|
|
}...)
|
|
}
|