Re:C++ Attribute: Likely, Unlikely
by cmovq on Sunday October 22, 2023 @12:30PM
(#37976797)
A common use case for these is to prevent the compiler from inlining the unlikely case to avoid thrashing the instruction cache.
It’s a good idea to check the generated assembly when using these as they can lead to weird reordering of the code.
[ Parent
| on HN ]
It may also help a bit with a cold branch predictor and with icache hit rate
The compiler can make sure that the body for the likely condition is inline with the rest of the code, while the unlikely condition (e.g. the else block of a likely if) can be outlined behind a forward branch
Keeping the unlikely code further aside and behind a branch helps the happy path stay hot and well-predicted