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
5
u/tropix126 Aug 23 '26 edited Aug 23 '26
My main point is that when people see code like this being used in performance-critical kernel modules along with a tip that "they can use this too!", they are inclined to slap it everywhere in their programs any time a branch has a hot path. This is almost always a huge premature optimization and something you should only reach for if you have a very likely branch in a tight loop that's actually bottlenecking things as reported by your profiler of choice. It's also fairly easy to get wrong and guessing wrong is worse than not guessing at all. Whether or not they affect branch prediction at runtime is I think technically arch dependent, but I doubt LLVM is emitting x86 branch hints because those are weird, so yea it's mostly icache optimizations; what I meant in my original comment was that branch predictors are good enough such that wasting mental capacity on optimizing your if-statements is usually a waste of time.