lua-users home
lua-l archive

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

On Wed, 19 Aug 2020 at 06:50, Sergey Zakharchenko
<> wrote:
> This is a continuation of my luaL_checkudata/luaL_testudata
> optimization ideas. How about a new API, lua_metatabletopointer (or
> lua_getmetatablepointer?), acting as if lua_getmetatable followed by
> lua_topointer, but without touching the Lua stack? This would enable
> faster "are we really called with 'our' userdata?" checks and reduce
> userdata metamethod call overhead (checks would trivially extend to,
> say, a list of metatables known to a function).

In the early days of Ravi I checked performance of matrix
multiplication with Lua tables versus C arrays with metamethods.
Even without metatable checking the C arrays were slower than Lua
tables - metamethods are costly to invoke!
Adding metatable checking made it even worse of course (I was using
the optimized version suggested by Luiz in the mailing list a long
while ago).

I think it is best to benchmark the perceived advantage first. You may
be surprised.

In Ravi today metatable based type assertion is done by the VM when
you have something like:

function tryme(a: MyType)

Having a named typed therefore turned out to be important. And for
backward compatibility it is important to continue using existing
registry of metatables.