likely、unlikely

likely()unlikely()是内核中定义的两个函数宏,具体定义如下所示:

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

其中的__builtin_expect是gcc中提供的一个预处理命令(这个名词也是网上写的,我想叫函数更好些),==有利于代码优化==,它在gcc(version 4.4.0)具体定义如下:

long __builtin_expect (long exp, long c) [Built-in Function]

注解为:

You may use __builtin_expect to provide the compiler with branch prediction information. In general, you should prefer to use actual profile feedback for this (‘-fprofile-arcs’), as programmers are notoriously bad at predicting how their programs actually perform. However, there are applications in which this data is hard to collect.The return value is the value of exp, which should be an integral expression. The semantics of the built-in are that it is expected that exp == c.

大体意思为: 我们可以使用这个函数人为告诉编绎器一些分支预测信息“exp == c” 是“很可能发生的”,也就意味着告诉编译器可先进行判断“exp == c”是否真的发生了,==这样先判断大概率事件,便可省去一些步骤去判断一些小概率事件的发生。==

同时对于

#define likely(x) __builtin_expect(!!(x), 1)

也就是说明x == 1这种情况是“经常发生的”或是“很可能发生的”。

例:(内核版本2.6.22.6):/kernel/shed.c中有一段:

使用likely后 ,执行if后面语句的可能性大些,==编译器将if{}里的内容编译到前面。==

使用unlikely后,执行else后面语句的可能性大些,==编译器将else{}里的内容编译到前面。==

这样便有利于cpu预取,提高预取指令的正确率,因而可提高效率。

if (likely(!active_balance)) 
{
    /* We were unbalanced, so reset the balancing interval */
    sd->balance_interval = sd->min_interval;
} 
else 
{
    /*
    * If we've begun active balancing, start to back off. This
    * case may not be covered by the all_pinned logic if there
    * is only 1 task on the busy runqueue (because we don't call
    * move_tasks).
    */
    if (sd->balance_interval max_interval)
        sd->balance_interval *= 2;
}

编译过程中,会将if后面{}里的内容编译到前面,else 后面{}里的内容编译到后面。若将likely换成unlikely则正好相反。

==总之,likely与unlikely互换或不用都不会影响程序的正确性。但可能会影响程序的效率。==

注:同时对于__builtin_expect(!!(x), 1)中的!!(x),有说法是说为转换为BOOL型,但C中对BOOL型的支持貌似不太严格,所以此处好像意义不大。

==这两个函数宏的功能就是通过先进行一些大概率事件的判断与处理,并将该部分处理内容编译到前面,同时将对小概率事件的判断与处理放至后面,并将该部分处理内容编译到后面。==

==这样相应的在CPU中便可以省去一些的操作指令,以此来提升CPU的效率,属于一种效率优化手段。==

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值