Hacker News new | ask | show | jobs
by cmovq 971 days 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.
1 comments

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