Using sync.Pool for map[string]interface{}

agolangf · · 742 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m creating a huge quantity of <code>map[string]interface{}</code> (that&#39;s the db signature for BigQuery), and typically I&#39;ll turn to <code>sync.Pool</code> to reduce garbage collection. With slices, it&#39;s easy to reuse them with a simple <code>[:0]</code>, but for a map, you&#39;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&#39;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&#39;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&#39;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

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