When looking at code performance, it'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's used in the real world you get cache misses since the rest of the program accesses data too.
> 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've used things. You need to <i>repopulate</i> a significantly-sized <i>array</i> each time with randomly chosen values. You can't use the same array because it'll be learnt, and you can't use a short array because it'll be predicted globally.
This is not so much "outsmarting the compiler" as "working with the compiler" - tweaking code to trigger certain optimizations/generate particular output. The missing part, of course, is showing that these optimizations actually help...
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'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...
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?
Outsmarting the compiler: A short story about optimisation:<p>"Don't."<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're almost certainly wasting your time compared with more effective optimization methods, like rethinking the problem.)