lua-users home
lua-l archive

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

I'm not sure if I want to ignore the metatable's index method, though (on the account of certain things masquerading like tables, such as certain kinds of userdata). For getting the length, though, I will probably invest in a `lua_rawgeti(L, -1);` and use that as the index value (raw so that I don't run into someone's `__index` method for getting that special key in an array that symbolizes the size).

On Fri, Aug 19, 2016 at 4:01 AM, Liam Devine <> wrote:

On 19/08/2016 01:48, ThePhD wrote:
Well, I looked into this deeper after everyone gave me the conventional wisdom to NOT depend on `lua_next`'s iteration order and pray for no breaking changes.

It turns out that the correct wisdom -- iterate using an index value -- seems to also be the most performant wisdom from the C++ or C side. [1] compares the performance of calling `lua_next()` in a while loop and assuming it iterates in the right order, pushing a table and calling `lua_geti` with the indices (and knowing the size and indices before hand), and the other measures the doing a lua_pushinteger + lua_gettable. Measured in a computer with an i7 Intel chip, lots of ram, Windows 10, Lua 5.3.3 compiled as a DLL.

Interestingly, the pushinteger + gettable method is about equal to the lua_next implementation. So, for Lua 5.2 and lower, since there's no lua_geti intrinsically built into the API, the correct way is equal in performance to the UB way.

There is however a lua_rawgeti method that you can use.