Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ALTree opened this issue Mar 28, 2018 · 2 comments

Comments

@ALTree
Copy link
Member

ALTree commented Mar 28, 2018

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

@ALTree ALTree added this to the Unplanned milestone Mar 28, 2018
@josharian
Copy link
Contributor

Please see also #20533

@ALTree
Copy link
Member Author

ALTree commented Mar 28, 2018

Ah! Thank you. Basically a dup of that one... closing here.

@ALTree ALTree closed this as completed Mar 28, 2018
@golang golang locked and limited conversation to collaborators Mar 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants