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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Outsmarting the compiler: Short story about optimization

67 点作者 dpzmick将近 8 年前

8 条评论

cperciva将近 8 年前
When looking at code performance, it&#x27;s important to remember that conditional branches are almost always cheap <i>inside microbenchmarks</i>, because the CPU can figure out when the same branches get taken on every loop... but far more expensive in the real world. A similar issue applies to cache: Your code might fit inside the L1 cache in your benchmarks, but when it&#x27;s used in the real world you get cache misses since the rest of the program accesses data too.
Veedrac将近 8 年前
&gt; Then, a bit later, we need to very quickly finish populating the structs.<p>I am finding it extremely hard to envision a circumstance where this is a bottleneck for anything. Care to clarify the context?<p>I also find the struct layout really odd; why not just move c?<p>Your benchmarks are also probably broken; branch predictors use global state so will almost certainly predict fine the way you&#x27;ve used things. You need to <i>repopulate</i> a significantly-sized <i>array</i> each time with randomly chosen values. You can&#x27;t use the same array because it&#x27;ll be learnt, and you can&#x27;t use a short array because it&#x27;ll be predicted globally.
评论 #14695781 未加载
BenjiWiebe将近 8 年前
Quick upvote for a well designed mobile friendly site. The last few HN posts I read were awful on mobile.
评论 #14695792 未加载
JoachimSchipper将近 8 年前
This is not so much &quot;outsmarting the compiler&quot; as &quot;working with the compiler&quot; - tweaking code to trigger certain optimizations&#x2F;generate particular output. The missing part, of course, is showing that these optimizations actually help...
评论 #14695830 未加载
Dreami将近 8 年前
How does this wrapper and each variant connect together? I supposed he would include a pointer to the actual struct in the wrapper, but I just see payload (that I&#x27;m gonna assume needs to be written to that array with padding in the actual struct).<p>For me, a wrapper includes the original thing and just wraps stuff around it. How does this work here? May also be a question to the author, I suppose now...
评论 #14694226 未加载
bloaf将近 8 年前
So why wouldn&#x27;t you just put c before the arr[padding#] chars?
评论 #14695807 未加载
dmh2000将近 8 年前
gcc warning: ISO C++ forbids zero-size array ‘payload’ [-Wpedantic]<p>clang warning: flexible array members are a C99 feature [-Wc99-extensions]<p>is this still the case?
评论 #14696037 未加载
评论 #14696038 未加载
taneq将近 8 年前
Outsmarting the compiler: A short story about optimisation:<p>&quot;Don&#x27;t.&quot;<p>The end.<p>(Basically, as the story shows, with this kind of micro-optimization you may or may not beat the compiler but you&#x27;re almost certainly wasting your time compared with more effective optimization methods, like rethinking the problem.)
评论 #14693652 未加载
评论 #14695884 未加载