How do the likely/unlikely macros in the Linux kernel work and what is their benefit?

后端 未结 10 729
攒了一身酷
攒了一身酷 2020-11-22 12:58

I\'ve been digging through some parts of the Linux kernel, and found calls like this:

if (unlikely(fd < 0))
{
    /* Do something */
}

o

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 13:24

    These are GCC functions for the programmer to give a hint to the compiler about what the most likely branch condition will be in a given expression. This allows the compiler to build the branch instructions so that the most common case takes the fewest number of instructions to execute.

    How the branch instructions are built are dependent upon the processor architecture.

提交回复
热议问题