lua-users home
lua-l archive

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



On 9-Jan-06, at 1:59 PM, Mike Pall wrote:

This is not a problem because you can only create number objects
for numbers which are representable with lua_Number. So even if
the array grows larger (with a sparse tail), you just can't set
or get these elements because you are lacking the proper key.

That was my first thought, too. However, it's not quite true;
you can use lua_rawseti to set any key which fits in an int,
provided the array part is large enough to hold it (which it
might be if the table were created with lua_createtable().)

However, if the array part is not big enough, the key gets
bounced into the hash part and is then truncated into a
lua_Number. This can have odd consequences; in particular,
if the array part has 2^24 elements and an attempt is made
to add element 2^24+1 (which cannot be represented as a float),
Lua will resize the array to the same size before overwriting
the value at 2^24, which is a fairly time-consuming operation.
So, for example, if you do a lot of table.inserts, to append
to an empty table, you'll find the the first 2^24 of them
execute in a few seconds, but the next 100 take as long as
the first 16 million. (Of course, none of these succeed in
actually adding to the length of the table.)