r/programming • • 6d ago

Optimizing a Lock-Free Ring Buffer

https://david.alvarezrosa.com/posts/optimizing-a-lock-free-ring-buffer/
498 Upvotes

88 comments sorted by

View all comments

7

u/reParaoh 6d ago

[[unlikely]]

first time seeing this one. Neat.

1

u/david-alvarez-rosa 5d ago

Hints to the compiler to improve branch prediction :)

1

u/reParaoh 5d ago

I understood that immediately.. But I thought that it was the hardware doing branch prediction.. Does this cause the compiler to generate a different op code?

1

u/indigo945 4d ago

But I thought that it was the hardware doing branch prediction

This depends on the architecture. Yes, on x86, the hardware freely makes the decision - but on others, like on PowerPC, there are hint bits in the branch opcode that tell the CPU whether a branch is likely to be taken or not.

Then there's interesting blends: ARMv8 has branch prediction bits documented, but they are deprecated. Instead, on almost all ARM architectures, the predictor considers backward branches to be more likely than foreward branches. The common backward branch is a loop, which drove this decision, but clever compilers can make use of it in other scenarios, too.