<p>I'm creating a huge quantity of <code>map[string]interface{}</code> (that's the db signature for BigQuery), and typically I'll turn to <code>sync.Pool</code> to reduce garbage collection. With slices, it's easy to reuse them with a simple <code>[:0]</code>, but for a map, you'd have to do something like:</p>
<p><code>
for k := range m {
delete(m, k)
}
</code></p>
<p>Is that likely going to pay off? I know that benchmarking my specific use case is the real answer, but I'm curious about the underlying mechanisms. Is there really no good way to reduce garbage when using maps?</p>
<p>Further Reading:
<a href="http://stackoverflow.com/questions/13812121/how-to-clear-a-map-in-go" rel="nofollow">http://stackoverflow.com/questions/13812121/how-to-clear-a-map-in-go</a></p>
<hr/>**评论:**<br/><br/>tzcczt: <pre><p>No, that won't fully clear the map of all backing data (at least not yet), see <a href="https://github.com/golang/go/issues/20138" rel="nofollow">https://github.com/golang/go/issues/20138</a> </p></pre>Ploobers: <pre><p>That link is almost perfect... and will be perfect once the issue is fixed. :) Thanks!</p></pre>ChristophBerger: <pre><blockquote>
<p>I know that benchmarking my specific use case is the real answer, but I'm curious about the underlying mechanisms. Is there really no good way to reduce garbage when using maps?</p>
</blockquote>
<p>I think you already answered your question. Reducing garbage is only of value if the garbage collection you want to avoid can be replaced by something more efficient. Is looping over a map to delete all its entries more efficient then letting the GB do its job? Only a benchmark can tell.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传