Why are escape analysis results of these two programs different?

96 views
Skip to first unread message

Cholerae Hu

unread,
Jun 5, 2020, 6:38:56 AM6/5/20
to golang-nuts
https://play.golang.org/p/PL0FCLCv3qU

You'll find that x1() and x2() are different. Then I run `go tool compile -m -m`:
```
unsafestr.go:11:11: make([]byte, 12) escapes to heap:
unsafestr.go:11:11:   flow: b = &{storage for make([]byte, 12)}:
unsafestr.go:11:11:     from make([]byte, 12) (spill) at unsafestr.go:11:11
unsafestr.go:11:11:     from b := make([]byte, 12) (assign) at unsafestr.go:11:4
unsafestr.go:11:11:   flow: pbytes = &b:
unsafestr.go:11:11:     from &b (address-of) at unsafestr.go:14:50
unsafestr.go:11:11:     from pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b)) (assign) at unsafestr.go:14:9
unsafestr.go:11:11:   flow: {heap} = *pbytes:
unsafestr.go:11:11:     from pbytes.Data (reflect.Header.Data) at unsafestr.go:16:23
unsafestr.go:11:11:     from pstring.Data = pbytes.Data (assign) at unsafestr.go:16:15
unsafestr.go:11:11: make([]byte, 12) escapes to heap
unsafestr.go:22:11: make([]byte, 12) does not escape
```
What's the difference between x1 and x2? Both in x1 and x2, b's data pointers are assigned to an uintptr(reflect.StringHeader::Data).

Cholerae Hu

unread,
Jun 5, 2020, 7:10:26 AM6/5/20
to golang-nuts
I've dug a little bit deeper into this. The root cause is https://github.com/golang/go/blob/master/src/cmd/compile/internal/gc/escape.go#L725 here, this line only pass when dst.Op == ODOTPTR rather than ODOTPTR || ODOT. I have a very simple patch to solve this, but I don't know whether this is a bug or a feature.

在 2020年6月5日星期五 UTC+8下午6:38:56,Cholerae Hu写道:
Reply all
Reply to author
Forward
0 new messages