2 ms·
A common use case for these is to prevent the compiler from inlining the unlikely case to avoid thrashing the instruction cache. if (unlikely_condition) {
by cmovq 3y ago
A common use case for these is to prevent the compiler from inlining the unlikely case to avoid thrashing the instruction cache.
if (unlikely_condition) {
// Don’t inline this
expensive_operation();
}
It’s a good idea to check the generated assembly when using these as they can lead to weird reordering of the code.
- tux3 3y agoIt 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