$ go version
go version devel +377a2cb2d2 Tue Mar 27 18:03:39 2018 +0000 linux/amd64
Consider:
func f() {
workBuf := make([]int, 0, 32)
// ... do some local work on workBuf
}
-m says
f make([]int, 0, 32) does not escape
and workBuf is stack-allocated. But if the make line is changed to
n := 32
workBuf := make([]int, 0, n)
the workBuf variable is heap-allocated: make([]int, 0, n) escapes to heap.
You can still get a stack allocation by making n a const:
const n = 32
workBuf := make([]int, 0, n)
it may be worth to extend the stack-allocation optimization to include bounds defined in local, non-escaping variables.
Consider:
-msaysand
workBufis stack-allocated. But if themakeline is changed tothe
workBufvariable is heap-allocated:make([]int, 0, n) escapes to heap.You can still get a stack allocation by making
naconst:it may be worth to extend the stack-allocation optimization to include bounds defined in local, non-escaping variables.