Earlier quoted context omitted.
This is not the problem. You are assuming still using &, which is very obviously incorrect with a very obvious fix (%5) which is still wrong because of behavior at overflow.
I'm answering the specific problem posed by the OP, given his other comments in this thread. [EDIT] Resolved internal concerns about size calculations.
I've been writing ring buffers wrong all these years
71–80 of 175 posts
Re: I've been writing ring buffers wrong all these years
#72This is another interesting ring buffer implementation that uses mmap. https://github.com/willemt/cbuffer
Re: I've been writing ring buffers wrong all these years
#73Earlier quoted context omitted.
Do people's laces wear out? That's not a problem I've ever experienced.
I've had a shoelaces break maybe 3-4 times on shoes I wore regularly for more than 2-3 years. It's annoying out of all proportion to the expense involved. (The plastic bits at the end can also get frayed and fall off, which happens more quickly, but I'm not sure knot style has much to do with that.)
Re: I've been writing ring buffers wrong all these years
#74Earlier quoted context omitted.
Don't use a modulo then. Use subtraction.
Subtraction requires a branch, which could be worse (or not) depending on architecture.
Re: I've been writing ring buffers wrong all these years
#75Earlier quoted context omitted.
From the site: "The finished "Ian Knot" is identical to either the Standard Shoelace Knot or the Two Loop Shoelace Knot. Because it was tied much more quickly and symmetrically, the laces suffer less wear and tear and thus last longer."
Do people's laces wear out? That's not a problem I've ever experienced.
Re: I've been writing ring buffers wrong all these years
#76Earlier quoted context omitted.
It's odd that you were using ring buffers in 1992 for low level code but don't understand the value of avoiding a modulus instruction. Masking is far more efficient and often a ring buffer will be used in code where performance is absolutely critical.
You wouldn't use the modulus operation. You aren't adding some arbitrary number that's going to make you increase either index by more than the buffer length so you know that at worse you are going to need to subtract the length of the buffer. IIRC the way we made this really fast was the write the buffer backwards. That way you can detect wrapping around the buffer because DEC will underflow and set the sign flag. T…
Re: I've been writing ring buffers wrong all these years
#77I have always considered these "double ring" buffers. Along the same lines as how you figure out which race car is in the race is in lead by their position and lap count. You run your indexes in the range 0 .. (2 * SIZE) and then empty is EMPTY -> (read == write) FULL -> (read == (write + SIZE) % (2 * SIZE)) Basically you're full if you're at the same relative index and your on different laps, you are empty if you at…
(The trick is that SIZE has to be a power of two, or else when you increment from 2^32-1 to 0, your pointers will jump to a different position in the array.)
Re: I've been writing ring buffers wrong all these years
#78Earlier quoted context omitted.
No, the size of the array doesn't need to be a power-of-2 if you use modulus to derive indices. But you need to deal with the overflow somehow. For instance: 0xffffffff % 7 = 3, but (0xffffffff + 1) % 7 = 0.
Also as mentioned elsewhere in the comments, modulo is expensive, even more for non-powers of 2
Re: I've been writing ring buffers wrong all these years
#79This is of course not a new invention. The earliest instance I could find with a bit of searching was from 2004, with Andrew Morton mentioning in it a code review so casually that it seems to have been a well established trick. But the vast majority of implementations I looked at do not do this. I was doing this in 1992 so it's at least 12 years older than the 2004 implementation. I suspect it was being done long bef…
Try late 1960s. Generally known then, widely used.
For an interesting proof about tokens in ring buffers, check out https://www.cs.utexas.edu/users/EWD/ewd04xx/EWD426.PDF, which, for 1974, has an interesting bit of multiprocessing.
Re: I've been writing ring buffers wrong all these years
#80Earlier quoted context omitted.
You wouldn't use the modulus operation. You aren't adding some arbitrary number that's going to make you increase either index by more than the buffer length so you know that at worse you are going to need to subtract the length of the buffer. IIRC the way we made this really fast was the write the buffer backwards. That way you can detect wrapping around the buffer because DEC will underflow and set the sign flag. T…
You are still introducing a conditional by detecting the need to subtract, and iterating backward through memory is horrific for cache performance. If you need a specific, non power-of-2 sized buffer, then of course you make that design decision and pay the performance penalty. But I restate it's odd that you weren't even aware of the cost in 1992 as a system level programmer.