go/test/escape_make_non_const.go
Keith Randall 7a427143b6 cmd/compile: stack allocate variable-sized makeslice
Instead of always allocating variable-sized "make" calls on the heap,
allocate a small, constant-sized array on the stack and use that array
as the backing store if it is big enough.

Requires the result of the "make" doesn't escape.

  if cap <= K {
      var arr [K]E
      slice = arr[:len:cap]
  } else {
      slice = makeslice(E, len, cap)
  }

Pretty conservatively for now, K = 32/sizeof(E). The slice header is
already 24 bytes, so wasting 32 bytes of stack if the requested size
is too big isn't that bad. Larger would waste more stack space but
maybe avoid more allocations.

This CL also requires the element type be pointer-free.  Maybe we
could relax that at some point, but it is hard. If the element type
has pointers we can get heap->stack pointers (in the case where the
requested size is too big and the slice is heap allocated).

Note that this only handles the case of makeslice called directly from
compiler-generated code. It does not handle slices built in the
runtime on behalf of the program (e.g. in growslice). Some of those
are currently handled by passing in a tmpBuf (e.g. concatstrings),
but we could probably do more.

Change-Id: I8378efad527cd00d25948a80b82a68d88fbd93a1
Reviewed-on: https://go-review.googlesource.com/c/go/+/653856
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
2025-04-04 10:36:58 -07:00

109 lines
2.7 KiB
Go

// errorcheck -0 -m
// Copyright 2025 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 escape
const globalConstSize = 128
var globalVarSize = 128
//go:noinline
func testSlices() {
{
size := 128
_ = make([]byte, size) // ERROR "make\(\[\]byte, 128\) does not escape"
}
{
s := 128
size := s
_ = make([]byte, size) // ERROR "make\(\[\]byte, 128\) does not escape"
}
{
size := 128
_ = make([]byte, size) // ERROR "make\(\[\]byte, 128\) does not escape"
}
{
s := 128
size := s
_ = make([]byte, size) // ERROR "make\(\[\]byte, 128\) does not escape"
}
{
s1 := 128
s2 := 256
_ = make([]byte, s2, s1) // ERROR "make\(\[\]byte, s2, 128\) does not escape"
}
allocLen(256) // ERROR "make\(\[\]byte, 256\) does not escape" "inlining call"
allocCap(256) // ERROR "make\(\[\]byte, 0, 256\) does not escape" "inlining call"
_ = newT(256) // ERROR "make\(\[\]byte, 256\) does not escape" "inlining call"
{
size := globalConstSize
_ = make([]byte, size) // ERROR "make\(\[\]byte, 128\) does not escape"
}
allocLen(globalConstSize) // ERROR "make\(\[\]byte, 128\) does not escape" "inlining call"
allocCap(globalConstSize) // ERROR "make\(\[\]byte, 0, 128\) does not escape" "inlining call"
_ = newT(globalConstSize) // ERROR "make\(\[\]byte, 128\) does not escape" "inlining call"
{
c := 128
s := 256
_ = make([]byte, s, c) // ERROR "make\(\[\]byte, s, 128\) does not escape"
}
{
s := 256
_ = make([]byte, s, globalConstSize) // ERROR "make\(\[\]byte, s, 128\) does not escape"
}
{
_ = make([]byte, globalVarSize) // ERROR "make\(\[\]byte, globalVarSize\) does not escape"
_ = make([]byte, globalVarSize, globalConstSize) // ERROR "make\(\[\]byte, globalVarSize, 128\) does not escape"
}
}
func allocLen(l int) []byte { // ERROR "can inline"
return make([]byte, l) // ERROR "escapes to heap"
}
func allocCap(l int) []byte { // ERROR "can inline"
return make([]byte, 0, l) // ERROR "escapes to heap"
}
type t struct {
s []byte
}
func newT(l int) t { // ERROR "can inline"
return t{make([]byte, l)} // ERROR "make.*escapes to heap"
}
//go:noinline
func testMaps() {
size := 128
_ = make(map[string]int, size) // ERROR "does not escape"
_ = allocMapLen(128) // ERROR "does not escape" "inlining call"
_ = newM(128) // ERROR "does not escape" "inlining call"
}
func allocMapLen(l int) map[string]int { // ERROR "can inline"
return make(map[string]int, l) // ERROR "escapes to heap"
}
type m struct {
m map[string]int
}
func newM(l int) m { // ERROR "can inline"
return m{make(map[string]int, l)} // ERROR "make.*escapes to heap"
}