December 18, 2025
Buffer beef: devs in a loop
I've been writing ring buffers wrong all these years (2016)
Coder admits ring buffer goof; commenters spin into a lock-free cage match
TLDR: A programmer found a better way to build a circular data holder that doesn’t waste space, and the comments erupted over how to do it right for speed. Some insist the “final trick” is the only lock‑free path, while others question one‑slot designs and size rules — performance matters.
A developer confessed he’s been writing “ring buffers” — think of a tiny circular tray that holds data — the wrong way, wasting a slot, and the comments turned into a full‑on spin cycle. His fix: a cleaner approach that uses the whole tray without tripping over the empty/full confusion. Cue the lock‑free crowd (no waiting, no locks, just careful reads/writes) storming in: codeworse declared the final trick is the “only way” to make it fast, while RossBencina pushed back, saying you don’t need power‑of‑two sizes and you can do it with simple conditionals. Meanwhile, kybernetikos dropped a mic: “Even the LMAX Disruptor (a famous speed‑obsessed pattern) uses this trick.” Someone took the philosophical route, arguing a “one‑element ring buffer” shouldn’t just be a normal buffer set to size one — like you don’t build a whole array just to store true/false; you’d use a bitset or something leaner. The memes wrote themselves: “100% overhead, 0% payload,” “programmers arguing about circles,” and lots of winks at masking vs. modulus math. If you love performance drama, this thread delivers. Dive into the original post and the flame‑lit comments: blog and HN thread.
Key Points
- •Two common ring buffer implementations are described: array with two masked indices and array with one index plus a length field.
- •The two-index approach wastes one slot to distinguish empty from full states, limiting usable capacity to N−1.
- •The index+length approach uses full array capacity and maintains simple operations for push and shift.
- •For concurrent reader/writer scenarios, index+length causes cache contention and requires atomic updates to shared state.
- •The article introduces a third idea—array with two unmasked indices—aiming to combine benefits, though details are not provided in the excerpt.