Skip to content

cmd/compile: stack-allocate arrays of len/cap defined in local, non-escaping variable #24577

@ALTree

Description

@ALTree
$ 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions