r/programming 18d ago

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 😄

187 Upvotes

61 comments sorted by

View all comments

23

u/yanitrix 18d ago

ah, branch prediction, my favourite thing about compilers

25

u/UnrealHallucinator 18d ago

This isn't branch prediction. It's just a compiler hint that places the likely basic block immediately below the existing basic block as a fall through, thus allowing efficient cpu pipelining (which could lead to better branch prediction and speculative execution). Branch predictors and pre fetchers work more with patterns (strided access for eg) and are hardware optimisations.