logoalt Hacker News

secondcomingyesterday at 2:42 PM3 repliesview on HN

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

Replies

JonChesterfieldyesterday at 2:46 PM

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

show 2 replies
dalvrosayesterday at 2:52 PM

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

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

loegyesterday at 3:07 PM

This was, in fact, mentioned in the article.