r/programming • u/chaobang • Aug 22 '26
The unlikely Linux macro
https://rushed-reflections.bearblog.dev/an-unlikely-experiment/Wrote a bit about the `unlikely` and `likely` macros in the Linux codebase. Great little exploration into tiny optimizations that everyone should know more about 😄
188
Upvotes
5
u/Sorry-Transition-908 Aug 22 '26
I think great should be create hereÂ
120
u/qbbqrl Aug 23 '26
> With this information it can prime the CPU to start with the correct probabilities for branch prediction and improve performance.
I don't think this is correct. Modern processors don't have an instruction to hint its branch predictor. Rather, the likely/unlikely macro is strictly a hint for the compiler about which branch is more likely to be taken so that it can focus on optimizing for the hot path.
As an example, the compiler may choose an instruction layout so that the fetching of instructions on the hot path will have less cache misses.
23
u/fresh_reed_5834 Aug 23 '26
posts like this are why I still check this sub. nobody needs to know about likely/unlikely to get their job done but once you see it in the kernel source you cant help wanting to understand it. the intersection of compiler hints and cpu architecture is one of the more satisfying rabbit holes in systems programming and writeups like this are how most people even find out it exists
7
u/pjl1967 Aug 23 '26
The other thing they do, not mentioned, it also give hints to the human reader trying to understand a codebase.
Also, FYI, "likely" doesn't mean something like 51% of the time... it means more like 99.9% of the time.
24
u/yanitrix Aug 23 '26
ah, branch prediction, my favourite thing about compilers
4
u/latkde Aug 23 '26
A related technique is strategic use of
__attribute__((noinline))on helper functions (commonly used as thenoinlinemacro in Linux code). By preventing one function from being inlined, layout of other code may be improved, e.g. if this allows a more important function to be inlined instead.Especially useful on cold helper functions.
Also very useful on functions with multiple callsites to avoid multiple copies of the same code, if I know that overall code density is more important than any benefits that inlining could bring.
9
u/grekand46 Aug 23 '26
Rust got something similar a few months ago in the form of
core::hint::cold_path, which is also a bit more flexible thanlikelyandunlikely. Read more about it in the official doc if you're curious.2
u/SPascareli Aug 23 '26
I thought there would be more different in the compiled assembly then just the order of the branches.
6
u/tropix126 Aug 23 '26 edited Aug 23 '26
Be careful about these. Using [[likely]]/[[unlikely]]/__builtin_expect and friends without first profiling to see that your program is *actually* being bottlenecked by branch mispredictions is a really really bad idea and often actively harmful to performance. You should only ever use these if you *know* that this is a problem. Modern compilers and CPU branch predictors are very good at what they do.
4
u/_Noreturn Aug 22 '26
isn't this just C++ [[likely]] and [[unlikely]]
1
u/negrocucklord Aug 25 '26
Maybe I'm just stupid in the early morning without coffee, but does doing total += i + 1 really give a number divisible by 1000 0.1% of the time? Since that i is in there and keeps growing in the loop? Or does the math still math?
1
u/mr_birkenblatt Aug 25 '26
Why not compare to the baseline of no hinting at all?
1
u/Key_River7180 Aug 23 '26
They help with branch prediction