lua-users home
lua-l archive

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




> I have personally replaced the Lua table implementation with an
> single-mechanism array-based hash table to reduce branching and memory
> consumption. Apart from serving as a hash table, when a table is used only
> as an array, the data structure will look like one automatically (a tight
> array of ordered numbers). Naturally, from the outside the language appears
> unchanged.

Doesn't that use more memory, because the hash must store the indices
of the elements too?

-- Roberto

That is true for arrays, and is specifically because of the unification of the array and hash parts.

However, for my (probably very specific) use cases I find lots of small to medium hash type tables to be the more common use case. For larger, array-like structures I tend to store my data in some C structure. I have made other modifications that under my usage scenarios lead to a memory reduction:

* sizeof(Table) a little smaller
* Fewer allocations (one single allocation + the table structure itself)
* Type tags stored using 4 bits each instead of 32 bits (by itself saves 37.5%)

In implementation I felt that unifying the array and hash parts were necessary preconditions to apply these changes. It's possible a tricky programmer could apply some or all of these to the existing implementation to yield similar savings.

/ Tom