[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: array index and modulo
- From: David Kastrup <dak@...>
- Date: Fri, 30 Mar 2007 15:14:42 +0200
roberto@inf.puc-rio.br (Roberto Ierusalimschy) writes:
>> -- 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?
>
> There is a memory penalty. When the start index moves away from 1,
> Lua will stop storing these entries as array entries and start
> storing them as hash entries. That roughly doubles the memory used
> by the table.
Stupid question: given the simplicity of zero-based index
calculations, what was the design criterion behind starting the
vectors at 1? @[6,4,3] is indexed starting from one, so 1 probably
has some natural roots in the language (which need not necessarily
determine the data structures so strictly). Still, it would appear
strange that using 0 would double the memory used by the table:
wouldn't it be sufficient to hash just 0 and use the array method for
all the other consecutive entries? After all, zero-based indexing is
likely to be a natural choice for some applications, and if zero is
the only hashed entry, performance should not suffer.
--
David Kastrup