Last Updated: February 25, 2016
·
673
· akhilravidas

Hint the likely branch in GCC

If you are using GCC and working with code where one branch is very likely than the other, the builtin __builtin_expect can be very handy.

As is often the case with optimizations, the effects can be counter productive when applied without profiling and confirming the expectation.

A safe version of this is defined as a macro in folly [1]. It is a no-op when the builtin is not supported. This builtin is also often used in the linux kernel source [2]

[1] https://github.com/facebook/folly/blob/master/folly/Likely.h

[2] http://kernelnewbies.org/FAQ/LikelyUnlikely