__builtin_expect是gcc(版本>=2.96,網上寫的,我沒驗證過)中提供的一個預處理命令(這個名詞也是網上寫的,我想叫函數更好些),有利於代碼優化。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.
舉個例子(內核版本2.6.22.6):/kernel/shed.c中有一段: 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; }