<p>(Compared to a pointer receiver)
As quoted "A value receiver can reduce the amount of garbage that can be generated; if a value is passed to a value method, an on-stack copy can be used instead of allocating on the heap." in <a href="https://github.com/golang/go/wiki/CodeReviewComments">https://github.com/golang/go/wiki/CodeReviewComments</a></p>
<p>From what I imagine a pointer receiver will be treated just like a value receiver whose value is a pointer. And that pointer value is stored in stack rather than generated on heap. Am I missing something?</p>
<hr/>**评论:**<br/><br/>dbud: <pre><p>This is more of an escape analysis issue. If the compiler can't tell if the thing you are calling stores the pointer, it has to put your object on the heap so the pointer can live past that single function invocation.</p>
<p>Not sure what all the analyzer can determine, so that may be an older issue. I believe the compiler has flags to report escape analysis issues, not sure.</p></pre>ctcherry: <pre><p><code>-gcflags -m</code></p></pre>
Why when calling a function with value receiver can reduce the amount of garbage that can be generated?
agolangf · · 372 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传