TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Simple techniques to optimise Go programs

273 点作者 sjwhitworth将近 6 年前

9 条评论

kevinconaway将近 6 年前
Most of these techniques sacrifice readability so before implementing them, you should really profile to make sure that the code in question is a hot spot in terms of allocations or CPU usage.<p>That said, the example using sync.Pool is not quite right. The <i>New</i> function should always return a pointer type to avoid incurring an additional allocation on <i>Get</i> [0]<p>The code should look like:<p><pre><code> var bufpool = sync.Pool{ New: func() interface{} { buf := make([]byte, 512) return &amp;buf }} b := *bufpool.Get().(*[]byte) defer bufpool.Put(&amp;b) </code></pre> [0] see example in <a href="https:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;sync&#x2F;#Pool" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;sync&#x2F;#Pool</a>
评论 #20214650 未加载
评论 #20214833 未加载
评论 #20217115 未加载
评论 #20214587 未加载
kstenerud将近 6 年前
The JSON marshaler actually caches struct serializations so that you don&#x27;t incur the reflection cost on every run.
cafxx将近 6 年前
&gt; a previous version of this blog post did not specify that the New() function should return a pointer type. This avoids an extra allocation when returning through the interface{} type.<p>While this is good advice, it&#x27;s not entirely correct. Even with the current go compiler there are ways to use sync.Pool with non-pointer values without incurring in the extra allocation, e.g. using a second sync.Pool to reuse the interface{}. Although I would not recommend it as it&#x27;s slower, and much less maintainable.<p>&gt; The safe way to ensure you always zero memory is to do so explicitly:<p><pre><code> &#x2F;&#x2F; reset resets all fields of the AuthenticationResponse before pooling it. func (a* AuthenticationResponse) reset() { a.Token = &quot;&quot; a.UserID = &quot;&quot; } </code></pre> I think this is safer, in face of modifications to the AuthenticationResponse structure, and much clearer in its intent:<p><pre><code> &#x2F;&#x2F; reset resets all fields of the AuthenticationResponse before pooling it. func (a* AuthenticationResponse) reset() { *a = AuthenticationResponse{} }</code></pre>
pcwalton将近 6 年前
&gt; During a garbage collection, the runtime scans objects containing pointers, and chases them. If you have a very large map[string]int, the GC has to check every string within the map, every GC, as strings contain pointers.<p>This would, of course, be much less of an issue with a generational GC, which doesn&#x27;t have to scan the entire heap on every collection.
kchr将近 6 年前
&gt; In A&#x2F;B tests, we tried delaying the page in increments of 100 milliseconds and found that even very small delays would result in substantial and costly drops in revenue. - Greg Linden, Amazon.com<p>Just curious, do vendors on Amazon get reimbursed for the drops in revenue during tests like this?
评论 #20216862 未加载
评论 #20217563 未加载
tedunangst将近 6 年前
Replacing strings with ints would be more believable with a real world example.
评论 #20215168 未加载
评论 #20214588 未加载
astockwell将近 6 年前
&gt; Allocate capacity in make to avoid re-allocation<p>Am I the only one who has done this and used append() within a loop, and resulted in a slice that is 2x as long as the original desired length, and the first 1x of items are all empty?<p>I quickly caught it and fixed the approach (instead of append, I used the `i` as the insertion index into my new empty slice which already had capacity allocated), but the ease with which that subtlety could be overlooked turned me off to this approach unless I profile and really find it&#x27;s a hot path.<p>Edit to add: I tried his code, and it resulted in the new slice being as expected.
评论 #20215140 未加载
评论 #20215389 未加载
评论 #20215161 未加载
评论 #20215190 未加载
0815test将近 6 年前
Missing technique: rewrite the performance-critical parts of your Go programs in a different language, and use Cgo to make them accessible to Go code via the standard C ABI. K.I.S.S.
评论 #20215126 未加载
评论 #20215768 未加载
评论 #20217154 未加载
评论 #20214679 未加载
评论 #20214683 未加载
评论 #20217885 未加载
innocentoldguy将近 6 年前
For me, the best way to optimize a Go program is to write it in Rust.
评论 #20218830 未加载
评论 #20222187 未加载