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
View all comments
Show parent comment
1
u/cdb_11 Aug 25 '26
Not sure if I understand the question. There is no default behavior really. A high-level if-statement can be compiled in the source code order, or with the branch order and condition swapped. Compilers have some heuristics on which one to choose, and it's not always going to match the source code order. The heuristics are just guesses and they can be wrong, so you can adjust code generation manually with
likely/unlikelyannotations.