Remix.run Logo
secondcoming 6 hours ago

If you enforce that the buffer size is a power of 2 you just use a mask to do the

    if (next_head == buffer.size())
        next_head = 0;
part
JonChesterfield 6 hours ago | parent | next [-]

If it's a power of two, you don't need the branch at all. Let the unsigned index wrap.

loeg 6 hours ago | parent | next [-]

You ultimately need a mask to access the correct slot in the ring. But it's true that you can leave unmasked values in your reader/writer indices.

dalvrosa 6 hours ago | parent | prev [-]

Interesting, I've never heard about anybody using this. Maybe a bit unreadable? But yeah, should work :)

mandarax8 4 hours ago | parent [-]

See https://fgiesen.wordpress.com/2012/07/21/the-magic-ring-buff... which takes it even further :)

dalvrosa 3 hours ago | parent [-]

Nice one!

dalvrosa 6 hours ago | parent | prev | next [-]

Indeed that's true. That extra constraint enables further optimization

It's mentioned in the post, but worth reiterating!

loeg 6 hours ago | parent | prev [-]

This was, in fact, mentioned in the article.