Skip to content

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

Closed

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.

Activity

  1. added this to the Unplanned milestone on Mar 28, 2018
  2. josharian commented on Mar 28, 2018

    @josharian
    Contributor

    Please see also #20533

  3. ALTree commented on Mar 28, 2018

    @ALTree
    MemberAuthor

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

  4. locked and limited conversation to collaborators on Mar 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions