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: escape analysis miss #12588

Closed
davecheney opened this issue Sep 11, 2015 · 4 comments
Closed

cmd/compile: escape analysis miss #12588

davecheney opened this issue Sep 11, 2015 · 4 comments
Milestone

Comments

@davecheney
Copy link
Contributor

package main

type A struct {
        b [3]uint64
}

func f(a A) int {
        for i, x := range &a.b { // & to avoid making a copy of a.b
                if x != 0 {
                        return 64*i + int(x)
                }
        }
        return 0
}

func main() {
        var a A
        f(a)
}
% go build -gcflags=-m escape.go
# command-line-arguments
./escape.go:7: moved to heap: a
./escape.go:8: &a.b escapes to heap

Why does taking the address of field of a local value cause the formal parameter to escape ?

/cc @dr2chase

@cznic
Copy link
Contributor

cznic commented Sep 11, 2015

I'd guess int(x) causes that. At least at some (early) stage it's not distinguished from a function call.

@dr2chase dr2chase self-assigned this Sep 11, 2015
@dr2chase dr2chase added this to the Go1.6 milestone Sep 11, 2015
@dr2chase
Copy link
Contributor

My guess is that we don't do the right thing for range and that escapes &a.b, and thus a nail was wanting and the kingdom falls. I'll put it on my list for after SSA coverage is done.

@gopherbot
Copy link

CL https://golang.org/cl/14501 mentions this issue.

davecheney added a commit that referenced this issue Sep 12, 2015
Although bnum was being called with a Bits value, a limitation
of the escape analyser (#12588) meant that taking the
address of the Bits.b array in the range statement caused the
formal parameter to escape to the heap.

Passing the a pointer to a Bits, as with all the other Bits helper
methods avoids the allocation.

Before:
BenchmarkBnum1-4        20000000                69.6 ns/op            32 B/op          1 allocs/op

After:
BenchmarkBnum1-4        100000000               10.1 ns/op             0 B/op          0 allocs/op

Change-Id: I673bd57ddc032ee67d09474156d795fb1ba72018
Reviewed-on: https://go-review.googlesource.com/14501
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@gopherbot
Copy link

CL https://golang.org/cl/15130 mentions this issue.

@golang golang locked and limited conversation to collaborators Oct 4, 2016
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

4 participants