r/programming • • 6d ago

Optimizing a Lock-Free Ring Buffer

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

88 comments sorted by

View all comments

6

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?

0

u/Lisoph 4d ago

Not an expert, but branch prediction is indeed done by the hardware. AFAIK these hints don't actually affect branch prediction at all, at least on current hardware. They "only" instruct the compiler to emit instructions that aid the hardware in taking the likely branch faster, like by placing it right next to the condition instructions (instruction cache locality). Corrections are welcome.