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
3
u/cdb_11 Aug 23 '26
Nope, it's generally the opposite. Hot loops are less of a concern here. These affect static prediction and maybe icache, which is when the predictor and cache didn't see this particular code yet. Unless you have a perf counter that specifically tracks mispredictions on branches that haven't been seen yet, the profiler will just tell you that you have some branch mispredictions. Which isn't useful, because it always says that, because you always have them. Branch predictor and cache is shared, so generally you can only measure the real impact once you've "prematurely" annotated enough branches for this to matter at all. Maybe don't try to guess on which every single branch will go, but for example some conditions are very obviously never going to be taken.