likely unlikely()详解

来源:互联网 发布:unity3d摄像机自动缩放 编辑:程序博客网 时间:2024/06/05 03:38

先理清一个概念:从作用上看   if(like(v)) == if(unlikely(v))==if(v)  那么我们为什么要用likely unlikely 命令呢,我们来看下面的详解


本文转自:http://www.cnblogs.com/PyshCliem/archive/2012/09/07/2674714.html


在include/linux/compiler.h中定义如下:
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

 

__builtin_expect是gcc内建的了, 就像bash的test之类的.看来了gcc的doc
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 value of c must be a compile-time constant. The semantics of the built-in are that it is expected that exp == c. For example:
  if (__builtin_expect (x, 0))
    foo ();
would indicate that we do not expect to call foo, since we expect x to be zero. Since you are limited to integral expressions for exp, you should use constructions such as
  if (__builtin_expect (ptr != NULL, 1))
    error ();
when testing pointer or floating-point values.

大致是说, 由于大部分程序员在分支预测方面做得很糟糕, 所以GCC提供了这个内建函数来帮助程序员处理分支预测, 优化程序.其第一个参数exp为一个整型表达式, 这个内建函数的返回值也是这个exp, 而c为一个编译常量, 这个函数的语义是:你期望exp表达式的值等于常量c, 从而GCC为你优化程序, 将符合这个条件的分支放在合适的地方.
因为这个程序只提供了整型表达式, 所以如果你要优化其他类型的表达式, 可以采用指针的形式.
  likely(x) 其实就是期望x的值为1
    if(unlikely(x)){
      foo();
    }
来测试条件的话, 我们就期望foo()函数执行, 所以该宏的名字用likely也就是可能来表示.
  unlikely(x)望表达式x的值为0, 从而如果我们用
    if(unlikely(x)){
      bar();
    }
来测试条件的话, 我们就不期望bar()函数执行, 所以该宏的名字用unlikely也就是不太可能来表示.

上面这两条语句都是x为1的时候执行.其实也就一句话:if()语句你照用, 跟以前一样, 只是如果你觉得if()是1 的可能性非常大的时候, 就在表达式的外面加一个likely(), 如果可能性非常小(比如几率非常小), 就用unlikely()包裹上.你也可以完全不用likely(), unlikely().
注意: macro的定义x有括号. 这应该也是c的基础了, 不过我们一般还是会疏忽的. 这就说明x可以用表达式了, 于是likely也就可以test任意类型的东西了.

总之:
__builtin_expect() 是GCC(version >= 2.96)提供给程序员使用的, 目的是将“分支转移”的信息提供给编译器, 这样编译器可以对代码进行优化, 以减少指令跳转带来的性能下降.
__builtin_expect((x), 1) 表示 x 的值为真的可能性更大;
__builtin_expect((x), 0) 表示 x 的值为假的可能性更大.
使用 likely(), 执行if后面的语句的机会更大,使用unlikely(), 执行else后面的语句的机会更大.