r/programming • • 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

61 comments sorted by

View all comments

Show parent comment

1

u/danielcw189 Aug 25 '26

Both are fine in a high-level language.

but didn't the comment babove say, that if(unlikely_condition) would be working against the default behavior of the compiler?

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/unlikely annotations.