[sdiy] help understanding circular buffers for delay line

Ove Ridé nitro2k01 at gmail.com
Sat Mar 3 21:00:20 CET 2012


On 3 March 2012 20:15, Martin Klang <mars at pingdynasty.com> wrote:
>
> why not use modulus?
> inpos = (inpos + 1)  % length ;
>
> no power-of-two limitation with that.

What Matthew mentioned was predictability. IF the buffer is a power of
two, the compiler might decide to optimize it to an and. If it's not,
obviously it can't. In other words, simple changes may create big,
seemingly illogical, changes in performance.
Besides, power-of-two sizes are generally not a problem since the
buffer size only decides the *maximum* array size possible. (The
actual delay time can, depending on design, be decided by the offset
between the pointers.) This might be a problem if you have exactly a
power of two of memory and also need to store other things, but not a
problem if you can allow yourself to use that full memory size, or if
you don't have any practical memory constraints like on a modern PC.

> or how about something which you can look at and hope to understand, e.g.
> if(++inpos == length)
>   inpos = 0;

I would feel "safer" using code that doesn't rely on a single value.
What if inpos for whatever weird reason got bigger than length?

if(++inpos >= length)
  inpos -= length;
or maybe here
  inpos %= length;

(The modulus is then executed less often. Again this may or may not do
a difference depending on what tricks the compiler actually pulls
off.)

> As for performance, don't optimise your code first, optimise your design.

To me, the choice of power-of-two buffer or not IS a design choice.
It's something you decide first and then design the rest of the code
around that.

> Then if need be, find the bottle necks, compile and compare the assembly output. For the target platform.
>
> For something like this, I would start with a library implementation such as this [1], and work from there.
>
> just my £0.02,
>
> /m
>
> [1] http://www.boost.org/doc/libs/1_49_0/libs/circular_buffer/doc/circular_buffer.html

Disagree. I would try to avoid using generic code like that for
something very specific like this, where performance may matter a lot.
Especially if the final target is a microcontroller.

-- 
/Ove

Blog: <http://blog.gg8.se/>

"Here is Evergreen City. Evergreen is the color of green forever."



More information about the Synth-diy mailing list