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

Closed
dr2chase opened this issue Oct 8, 2016 · 2 comments
Labels
early-in-cycle A change that should be done early in the 3 month dev cycle. FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Performance
Milestone

Comments

@dr2chase
Copy link
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 The path to resolution is known, but the work has not been done. 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 modified the milestones: Go1.10Early, Go1.9Early May 3, 2017
@bradfitz bradfitz added early-in-cycle A change that should be done early in the 3 month dev cycle. and removed early-in-cycle A change that should be done early in the 3 month dev cycle. labels Jun 14, 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
@mariecurried
Copy link

I think this issue has been fixed by @mdempsky's escape analysis rewrite.

@mdempsky
Copy link
Member

@marigonzes Thanks. Agreed, fixed and tested implicitly through the updates to test/fixedbugs/issue17318.go.

@golang golang locked and limited conversation to collaborators Apr 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
early-in-cycle A change that should be done early in the 3 month dev cycle. FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Performance
Projects
None yet
Development

No branches or pull requests

7 participants