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 too pessimistic for value-captures at closure evaluation. #17388

Open
dr2chase opened this Issue Oct 8, 2016 · 0 comments

Comments

Projects
None yet
4 participants
@dr2chase
Contributor

dr2chase commented Oct 8, 2016

package main

var s int

//go:noinline
func qux(ops func(i, j int) int, j int) (err *int) {
    enqueue := func(i int) *int {
        s = ops(i, j)
        return &s
    }
    err = enqueue(4)
    return
}

//go:noinline
func barfun(i, j int, ops func(i, j int) int) *int {
    s = ops(i, j)
    return &s
}

//go:noinline
func bar(ops func(i, j int) int, j int) (err *int) {
    err = barfun(4, j, ops)
    return
}

func main() {
    q := func(i, j int) int {
        return i + j
    }
    println("*qux(q,3) =", *qux(q, 3))

    b := func(i, j int) int {
        return i + j
    }
    println("*bar(b,3) =", *bar(b, 3))
}

The parameter ops to qux is inferred to escape, incorrectly,
compared to the equivalent rewriting into bar and barfun.
go build -gcflags '-m -l' e2.go yields

./e2.go:7: func literal escapes to heap
./e2.go:6: leaking param: ops to result err level=0
./e2.go:9: &s escapes to heap
./e2.go:6: leaking param: ops
./e2.go:18: &s escapes to heap
./e2.go:16: barfun ops does not escape
./e2.go:22: bar ops does not escape
./e2.go:28: func literal escapes to heap
./e2.go:28: func literal escapes to heap
./e2.go:33: main func literal does not escape

I think this results from a very simple model of closure behavior; anything captured is regarded as transmitted to any result, regardless of the actual behavior of the closure.

It is plausible that the escape behavior of closures could be modeled in the same way as the escape behavior for ordinary functions, with captured-by-value parameters treated equivalently.

@quentinmit quentinmit added the NeedsFix label Oct 9, 2016

@quentinmit quentinmit added this to the Go1.8 milestone Oct 9, 2016

@rsc rsc modified the milestones: Go1.9Early, Go1.8 Oct 21, 2016

@bradfitz bradfitz added the Performance label May 3, 2017

@bradfitz bradfitz modified the milestones: Go1.10Early, Go1.9Early May 3, 2017

@bradfitz bradfitz modified the milestones: Go1.10Early, Go1.10 Jun 14, 2017

@bradfitz bradfitz modified the milestones: Go1.10, Go1.11 Nov 28, 2017

@bradfitz bradfitz modified the milestones: Go1.11, Go1.12, Unplanned May 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment