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

6

u/max123246 Aug 23 '26

You know you can choose one style and is that a problem in practice nope.

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.

also don't pretend like C has everything be good

I agree, I don't reach for C these days at all unless there's a codebase I need to work in that's already in C.

you should trwat moved from objects as unusable, and you can make non nullable types work.

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. But you're now back in the world where your moved-from object just does arbitrary out of spec behavior and you kinda have to hack around it.

C has its faults but I appreciate that it doesn't claim to protect me against bugs when in fact, C++ just makes those bugs when they happen far more confusing to understand.

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.