Why when calling a function with value receiver can reduce the amount of garbage that can be generated?

agolangf · · 372 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>(Compared to a pointer receiver) As quoted &#34;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.&#34; 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&#39;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>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

372 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传