lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


The data is some expensive to create userdata.  Its an array of
textures in OpenGL which I'm using as a delay line.  Thus the ring
buffer.  If I'm not mistaken, this is the queue model from PIL which I
have used in other code but doesn't quite work for thids situation.

best,
wes

On 3/29/07, Ralph Hempel <rhempel@hempeldesigngroup.com> wrote:
Wesley Smith wrote:
> This is a very simple problem, but I'm failing to find an elegant
> solution.  I have an array of data that I'm treating as a circular
> buffer.  Typically I use modulo to wrap the index to the range of the
> buffer, but since Lua arrays start from 1 (and I want to follow this
> convention), the modulo operator is not really useful.

Can you turn the problem on its head? You're probably using
the circular buffer because that's one way to deal with
the problem in a language that does not do garbage collection.

What about keeping head and tail "pointers" and creating each new
item as needed and setting the used ones to nil...

-- this needs to be atomic
buffer[head] = "Some new data"
head = head + 1

-- this needs to be atomic
data = buffer[tail]
buffer[tail] = nil
tail = tail + 1

Is there a severe performance penalty for this? What happens
when the index wraps around?

Cheers, Ralph