linux核心likely() 與 unlikely()

來源:互聯網
上載者:User

核心中的 likely() 與
unlikely()
在 2.6 核心中,隨處可以見到 likely() 和 unlikely() 的身影,那麼為什麼要用它們?它們之間有什麼區別?

首先要明確:

            if(likely(value)) 等價於 if(value)

            if(unlikely(value)) 也等價於 if(value)

也就是說 likely() 和 unlikely() 從閱讀和理解代碼的角度來看,是一樣的!!!

這兩個宏在核心中定義如下:

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


__builtin_expect() 是 GCC (version >=
2.96)提供給程式員使用的,目的是將“分支轉移”的資訊提供給編譯器,這樣編譯器可以對代碼進行最佳化,以減少指令跳轉帶來的效能下降。

__builtin_expect((x),1) 表示 x 的值為真的可能性更大;
__builtin_expect((x),0) 表示 x 的值為假的可能性更大。

也就是說,使用 likely() ,執行 if 後面的語句
的機會更大,使用unlikely(),執行else 後面的語句的機會更大。


例如下面這段代碼,作者就認為 prev 不等於 next 的可能性更大,

if (likely(prev != next)) {
       next->timestamp = now;
        ...
} else {
        ...;
}
通過這種方式,編譯器在編譯過程中,會將可能性更大的代碼緊跟著起面的代碼,從而減少指令跳轉帶來的效能上的下降。

下面以兩個例子來加深這種理解:

第一個例子: example1.c

int testfun(int x)
{
        if(__builtin_expect(x, 0)) {
                              ^^^--- We instruct the compiler, "else"
block is more probable
                x = 5;
                x = x * x;
        } else {
                x = 6;
        }
        return x;
}
在這個例子中,我們認為 x 為0的可能性更大

編譯以後,通過 objdump 來觀察彙編指令,在我的 2.4 核心機器上,結果如下:

# gcc -O2 -c example1.c
# objdump -d example1.o

Disassembly of section .text:

00000000 <testfun>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   8b 45 08                mov    0x8(%ebp),%eax
   6:   85 c0                   test   %eax,%eax
   8:   75 07                   jne    11 <testfun+0x11>
   a:   b8 06 00 00 00          mov    $0x6,%eax
   f:   c9                      leave
10:   c3                      ret
11:   b8 19 00 00 00          mov    $0x19,%eax
16:   eb f7                   jmp    f <testfun+0xf>

可以看到,編譯器使用的是 jne (不相等跳轉)指令,並且 else block 中的代碼緊跟在後面。

8:   75 07                   jne    11
<testfun+0x11>
a:   b8 06 00 00 00          mov    $0x6,%eax

第二個例子: example2.c

int testfun(int x)
{
        if(__builtin_expect(x, 1)) {
                              ^^^ --- We instruct the compiler, "if"
block is more probable
                x = 5;
                x = x * x;
        } else {
                x = 6;
        }
        return x;
}
在這個例子中,我們認為 x 不為 0 的可能性更大

編譯以後,通過 objdump 來觀察彙編指令,在我的 2.4 核心機器上,結果如下:

# gcc -O2 -c example2.c
# objdump -d example2.o

Disassembly of section .text:

00000000 <testfun>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   8b 45 08                mov    0x8(%ebp),%eax
   6:   85 c0                   test   %eax,%eax
   8:   74 07                   je     11 <testfun+0x11>
   a:   b8 19 00 00 00          mov    $0x19,%eax
   f:   c9                      leave
10:   c3                      ret
11:   b8 06 00 00 00          mov    $0x6,%eax
16:   eb f7                   jmp    f <testfun+0xf>

這次編譯器使用的是 je (相等跳轉)指令,並且 if block 中的代碼緊跟在後面。

   8:   74 07                   je     11
<testfun+0x11>
   a:   b8 19 00 00 00          mov    $0x19,%eax

文章出處:
http://www.diybl.com/course/6_system/linux/Linuxjs/2007930/75397.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.