Skip to content

SCX eBPF macro unlikely

v6.12

The unlikely macro hints to the compiler that an expression is unlikely to be true. This can help the compiler optimize the generated code.

Definition

#define unlikely(x) __builtin_expect(!!(x), 0)

Usage

The unlikely macro can be used on boolean expressions, like in if statements, where you expect the expression to be false most of the time. This can help the compiler optimize the generated instructions.

An example of such an optimization would be to place instructions for the likely code path close to the conditional jump to improve instruction cache locality.

Example

if (unlikely(ptr == NULL)) {
    // Unlikely code path
    return -EINVAL;
}
// Likely code path