Re: [go-nuts] Re: Why does this struct escape to the heap?

2018-09-06 Thread pauldice
Yes, I came across that post when looking for info on method values and allocations. I'm sure that's the root of the problem. But I don't understand why the compiler can't figure out that no heap allocation is needed. If t doesn't escape when calling t.M(), why can't the compiler work out

Re: [go-nuts] Re: Why does this struct escape to the heap?

2018-09-06 Thread Tristan Colgate
Perhaps... https://groups.google.com/forum/m/#!topic/golang-nuts/aMicHmoOH1c On Thu, 6 Sep 2018, 17:26 , wrote: > Using a pointer receiver (as in your noEscape example) just pushes the > problem up the stack. When you try to call it, e.g. > > > func parent() bool { > var opts options >

Re: [go-nuts] Re: Why does this struct escape to the heap?

2018-09-06 Thread pauldice
Using a pointer receiver (as in your noEscape example) just pushes the problem up the stack. When you try to call it, e.g. func parent() bool { var opts options return noEscape('0', ) } you find that escapes to the heap in the parent function instead. I haven't opened an issue yet

Re: [go-nuts] Re: Why does this struct escape to the heap?

2018-09-06 Thread Tristan Colgate
I think this has to do with the pointer reciever, vs the pass by value: func noEscape(r rune, opts *options) bool { f := opts.isDigit return f(r) } opts here does not escape, but in: func escapes(r rune, opts options) bool { f := opts.isDigit return f(r) } opts is copied, so it is the

[go-nuts] Re: Why does this struct escape to the heap?

2018-09-05 Thread pauldice
I wonder if this is to do with method values. According to the spec , when you declare a method value like x.M: The expression x is evaluated and saved during the evaluation of the method > value; the saved copy is then used as the receiver in any

Re: [go-nuts] Re: Why does this struct escape to the heap?

2018-09-05 Thread Silviu Capota Mera
Hi Paul, Perhaps my answer was a bit simplistic, and I admit that I answered without looking too much into it. Basically, in your index1, the opts.isDigit passed to IndexFunc is > syntactic sugar for ().isDigit -> then the compiler needs to move opts > on the heap. "*Why? Taking a reference to

[go-nuts] Re: Why does this struct escape to the heap?

2018-09-05 Thread pauldice
FYI, here is the escape analysis output. $ go build -gcflags="-l -m=2" ./escape.go:20:10: capturing by ref: opts (addr=true assign=false width= 4) ./escape.go:9:38: (*options).isDigit opts does not escape ./escape.go:15:34: opts escapes to heap ./escape.go:15:34: from opts.isDigit (call

[go-nuts] Re: Why does this struct escape to the heap?

2018-09-05 Thread pauldice
Hi Silviu, Thanks for your reply. I'm not sure about the points you raise though. Basically, in your index1, the opts.isDigit passed to IndexFunc is > syntactic sugar for ().isDigit -> then the compiler needs to move opts > on the heap. Why? Taking a reference to a function argument

[go-nuts] Re: Why does this struct escape to the heap?

2018-09-04 Thread silviucapota
Hi Paul, Check out: https://golang.org/doc/effective_go.html#pointers_vs_values Basically, in your index1, the opts.isDigit passed to IndexFunc is syntactic sugar for ().isDigit -> then the compiler needs to move opts on the heap. You can either a) define an alternate isDigitVal method,