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 😄

187 Upvotes

61 comments sorted by

View all comments

Show parent comment

1

u/_Noreturn Aug 23 '26

On a large project like the Linux kernel, it helps a lot when the language does that work for you. Otherwise maintainers have to constantly be vigilant to tell people to stick to the 1 convention within that codebase.

I mean I could nitpick and say C allows like a million ways to implement a thing unlike C++ e.g inheritance you could do it with the first object trick and function pointers or allocate dynamicly vis the last member trick and use a global vtable. In C++ it would just be inheritance which is universal.

Or how to name functions e.g object_create or object_new? in C++ it is always called a constructor.

As always set a style guide

This isn't what the C++ standard says. They specifically say the moved-from object is a valid object, which means it should be safe to call any API on it. Yes, in practice, most code simply ignores what the C++ standard says and does the sensible thing, treat the moved-from object as not existing anymore.

Lets be honest that the C++ standard is irrelevant, you are the kernel you define the spec basically and you could rely on gcc extensions.

Also the C++ standard buzzwords practically translats to "your moved from object should be destructible" as that is what only realistically matters.