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写道: > > 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). > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/e1d87a1a-fa08-49cd-bcea-9985bca7cc0fo%40googlegroups.com.