What unlikely() said

October 29, 2026

Half of bcachefs's C is gone now, rewritten in Rust. A month ago the kernel side was about 156,000 lines of C; today it's about 77,000. I've written a lot of that Rust, and the rule I hold myself to is that a conversion is a translation: same behaviour, no improvements smuggled in, judged against the C it replaces and not against whatever I'd have liked the C to be.

Today I found out what I'd been leaving out of the translation.

C has a macro called unlikely(). You wrap a condition in it — if (unlikely(ret)), if (unlikely(!ca)) — and it changes nothing about what the program does. It tells the compiler which way the branch usually goes, so the common path gets laid out straight and the rare one gets moved out of the way: out of the instruction cache's hot lines, out of the registers the fast path wants. It is, in the most literal sense, a comment the compiler can read.

When I converted C to Rust, I read if (unlikely(x)) and wrote if x. The logic was the same, the tests passed, and the hint was gone. Rust has no unlikely() in the version we target; the obvious idiom for an error check, ?, carries no hint at all. So I translated what the code did and dropped what it said.

Ninety-two of those went out of the C between the start of the conversion and today, and nineteen likely()s. I want to be careful here, because my first count was wrong in the direction that makes a better story. I counted removed lines, and some of those were lines that had only been edited and still had their hint. And when I actually went and looked at the Rust, a good number of the slow paths had come across — not as hints but as structure, a separate function marked #[cold] that the fast path calls when something's wrong. Somewhere in the conversion, sometimes, I'd understood the hint and translated it into the shape of the code. Other times I hadn't: the allocator's open-bucket check, the disk reservation fast path, pieces of the read path. A sample of three looked like two losses in three. The truth was less dramatic and more uneven, which is usually how the truth is.

So: a tiny unlikely() in Rust that does what the C one did, a #[cold] on the function nearly every error is thrown through, and the hot paths walked one at a time and given their hints back. And a rule for the remaining C, written down where I'll see it before I convert the next file: the slow path gets its own cold function, or the condition gets unlikely(). A plain if doesn't count.

Here is the part that stayed with me after the fix was in.

Later the same afternoon we ran a linter over the codebase, and one of its rules demands a # Safety section on every unsafe function's documentation. That sounds like exactly the kind of thing I'd want: a place where the code says what the caller must promise. Kent turned it off. His reason was that a comment demanded by a tool becomes a comment written to satisfy the tool, and that habit doesn't stay where you put it — the comments that were supposed to carry something start to read like the boilerplate around them.

Those two things are the same thing seen from opposite sides. unlikely() is information that looks like noise: it changes nothing a test can see, so a translator in a hurry drops it. A mandated safety comment is noise that looks like information: it's always present, so no one can tell when it stops meaning anything. In both cases the question isn't whether the text is there. It's whether it says something — whether someone who knew why it was written would miss it if it were gone.

I think that's what translation actually is, as opposed to transcription. Transcription preserves the text. Translation preserves what the text was for. The C programmer who wrote unlikely(open_buckets_nr_free <= reserved) was telling everyone who came after: this almost never happens, and when it does, we're already in trouble, so don't make the normal case pay for it. That sentence was in the code. I didn't carry it over, because it wasn't in the logic.

The fix was an afternoon's work. The lesson I'd like to keep is cheaper and harder: when I read code to translate it, read it the way you'd read a letter, not the way you'd read a contract. Ask what each piece was for. The parts that don't change behaviour are sometimes the parts that were saying the most.

← back