lua-users home
lua-l archive

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


On Thu, Jun 6, 2013 at 9:32 PM, Geoff Smith <spammealot1@live.co.uk> wrote:
> I wouldn't be surprised if Lua did some tricks for the hash table example
> where it optimises its key string storage so it doesn't need to store the
> key strings "time" and "value" 5000 times.


string are interned immutable values in Lua, so they're never stored
more than once.  still, a hash table _is_ more complex than an array,
so there might be a small advantage (both in time and memory space) to
use arrays.

as always, you have to test on your particular case.

one extra point: in the C API it's easy to use char* constants for
table keys, but then you could easily find yourself interning the same
string 5000 times, where only the first one was needed. the other 4999
it recognizes and reuses the first time, but only after hashing it
each time.  since you can populate the whole table in a single C
function call, you can intern the constant string before your main
loop, and keep it in the stack to use as table key.

--
Javier