TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Outsmarting the compiler: Short story about optimization

67 pointsby dpzmickalmost 8 years ago

8 comments

cpercivaalmost 8 years ago
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.
Veedracalmost 8 years ago
&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 未加载
BenjiWiebealmost 8 years ago
Quick upvote for a well designed mobile friendly site. The last few HN posts I read were awful on mobile.
评论 #14695792 未加载
JoachimSchipperalmost 8 years ago
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 未加载
Dreamialmost 8 years ago
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 未加载
bloafalmost 8 years ago
So why wouldn&#x27;t you just put c before the arr[padding#] chars?
评论 #14695807 未加载
dmh2000almost 8 years ago
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 未加载
taneqalmost 8 years ago
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 未加载