r/cpp • • Feb 16 '26

Favorite optimizations ??

I'd love to hear stories about people's best feats of optimization, or something small you are able to use often!

135 Upvotes

185 comments sorted by

View all comments

Show parent comment

3

u/Successful_Yam_9023 Feb 16 '26 edited Feb 16 '26

The functions don't have to be simple, but that's roughly the unit I'm talking about. Some function, maybe it calls other functions, maybe some of them are expected to be inlined (that's one of the things to watch for), or the inlined copies of the function if applicable.

Things (issues, if you want to call them that) that can be spotted include: missed autovectorization when it was intended or previously happened before making a change, bad autovectorization (for example, compilers like to eagerly widen integers before doing operations on them which really kills SIMD perf), raw div in the code that was expected to be optimized as a divide-by-constant or divide-by-power-of-two, function call was intended to be inlined but wasn't, loop-invariant value is being constantly recomputed, loop has unintended loop-carried dependency through "memory" (store to load forwarding really) instead of putting the thing in a register during the loop and storing it afterwards, spilling happened in a loop that you carefully designed for the number of available registers, random miscellaneous codegen quirks of the compiler, that sort of thing. A lot of "wow I thought the compiler would do this but I guess it's on me" (not always the compilers fault but rather having the wrong expectation).

1

u/thisismyfavoritename Feb 16 '26

thanks for the detailed answer. By spilling you mean on the memory/stack?