Hello,
I often punch above my weight trying to understand various problem, so when I saw talk "Understanding Allocations: the Stack and the Heap - GopherCon SG 2019" by Jacob Walker (
https://www.youtube.com/watch?v=ZMZpH4yT7M0), I cannot resist it. I'm basically compiler front-end user and nothing more, but this talk is very clear, so I learn something.
I'm using "go version go1.17 linux/amd64" and I puzzled by what I get when i run "go build -gcflags="-m -l" go_program.go", which should print optimization decisions with disable inlining (I hope that I get it correctly). When Jacob Walker use "println" I follow what he said at the beginning and use "fmt.Println".
For the program presented at 13:20 I get
x does not escape
... argument does not escape
n escapes to heap
while at presentation you have "&n does not escape". It seems to me, that on my computer much more things "escapes to heap". Can anyone point me in the right direction, to understand why I get "n escapes to heap"? Two simple answer that I can think about is "You don't use println" or "Compiler has changed.", but I want to know more about that. (I probably again punch above my weight.)
I also reworked first code shown at 16:47 to form
package main
import "fmt"
func main() {
b := read()
fmt.Println("b:", b)
}
func read() []byte {
b := make([]byte, 32)
return b
}
and get
make([]byte, 32) escapes to heap
... argument does not escape
"b:" escapes to heap
b escapes to heap
I guess fmt.Println can force "b:" to be of type interface{}, but this is still puzzling, why such simple thing "escapes to heap", since in the talk he says that "interfaces may escapes to the heap".
By the way, if someone can explain me what is going on with "println", I would be gratefully. This isn't important to me, since "fmt" give me what I need, but I just curious what Walker used it.
Best
Kamil