r/cpp • u/Little-Reflection986 • 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
View all comments
Show parent comment
20
u/matthieum Feb 16 '26
Modern versions of GCC have gained the ability to split a single function into hot/regular and cold part, and moving the cold part into a different function.
This is, really, the best possible outcome, as then you don't even have a call overhead in the "hot" part -- and by that I don't mean
call, I mean all the kerfuffle of moving the arguments of the called function in the right register/spot on the stack -- you just have a jump.Unfortunately, it's a fairly "magical" optimization: the developer doesn't get to choose where the boundary is, and if the compiler is too conservative, this means leaving part of the error path -- like preparing the error message -- in the hot/regular part of the function :/