lua-users home
lua-l archive

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


> Another possibility is to use the lua registry table to store your
> tables.  You can readily use luaL_reg(L, LUA_REGISTRYINDEX);  to
> safely get integer references to the index of your given tables.
> Another option is to immediately populate the LUA_REGISTRYINDEX w/
> 1000 entries and be guaranteed that luaL_reg used by any other code
> won't take up any spots inside your expected space.


Right, I could use the registry table, I went down the route of using
the luaL_ref function first, but they appeared to be down hash key
lookups instead of array index lookups. For my 1000's of tables, this
meant luaH_getstr showing up at the top of my profile runs, because it
was iterating through keys to find the values after converting my
carefully chosen numeric values into TValue objects.

A couple of people replied that the table should re-size itself to have
an internal array even though the keys were inserted into the hash
portion. In my debugging this was not the case. Even after 1000's of
keys were inserted, using lua_rawseti and lua_rawgeti were still doing
hash key lookups.

It looks like the public C function lua_rawseti calls the internal
function luaH_setnum that allocates new keys, but that internal function
only allocates new keys for the hash table. Integer indices are
converted to key objects before being inserted into the hash table -
there is no facility in that function to grow the array portion of the
lua table. I guess it is relying on resizing the table to move those
keys into the array? This seems in-efficient and in my case doesn't seem
to be working.