Skip to content

cmd/compile: conflicting escape analysis result and benchmark result output messages #46908

Closed

Description

@zigo101

What version of Go are you using (go version)?

$ go version
go version go1.16.5 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

package concat

import (
    "testing"
)

var s33 = []byte{32: 'b'}
var a = string(s33)


func Benchmark_e_33(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _ = a + a // a + a does not escape
    }
}

What did you expect to see?

No allocations. Or says "a + a escapes to heap"

What did you see instead?

$ go test -gcflags=-m
..
./concat_test.go:13:15: a + a does not escape
..
$ go test -bench=. -benchmem 
...
Benchmark_e_33-4   	14399670	        75.39 ns/op	      80 B/op	       1 allocs/op

Activity

  1. martisch commented on Jun 24, 2021

    @martisch
    Contributor

    I dont think the interpretation of heap allocation if and only if escapes is correct. The stack is only used for small and fixed size allocations currently. Therefore some variables/expressions might not escape but are still allocated on the heap. Examples I can think of in the current go gc implementation are big fixed size slice literals, maps past the initial small size, string concatenations larger than 32 bytes. Even with escape analysis not changing the compiler might choose to do heap allocations for different situations and cut offs. Stack allocation is an optimisation and escape analysis is just one factor to decide if stack allocation should be done but the final decision depends on compiler implementation outside escape analysis. The compiler might choose to allocate everything on the heap in a very simplistic version.

  2. toothrot commented on Jun 24, 2021

    @toothrot
    Contributor

    I agree with martisch's assesment. pprof may be a more helpful tool for understanding why this string concatenation is triggering an allocation.

    I don't think this is an issue with Go or the tooling, so please see https://golang.org/wiki/Questions for further answers.

  3. zigo101 commented on Jun 25, 2021

    @zigo101
    ContributorAuthor

    @toothrot
    pprof is too heavy to the task.

    @martisch
    I thought it again, and now think the message should be "a + a does not escape" instead. The accurate meaning of it is "the header part of a + a does not escape". As you have explained, it is hard (impossible actually) for the compiler to determine whether or not the underlying part of "a + a" will escape to heap.

  4. zigo101 commented on Jun 25, 2021

    @zigo101
    ContributorAuthor

    Here is another example, for which the escape analysis messages are good in my opinion.
    At run time, the underlying part of y does escape, but the underlying part of x doesn't.

    package main
    
    var bs32 = make([]byte, 32)
    var bs33 = make([]byte, 33)
    
    func main() {
    	x := string(bs32) // string(bs32) does not escape
    	y := string(bs33) // string(bs32) does not escape
    	println(x)
    	println(y)
    }
  5. locked and limited conversation to collaborators on Jun 25, 2022
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions