lua-users home
lua-l archive

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


On Mon, Nov 2, 2009 at 6:31 PM, Leo Razoumov <slonik.az@gmail.com> wrote:
> On 2009-11-02, Mike Pall <mikelu-0911@mike.de> wrote:
>>  To see the metamethod dispatch hoisting, try this program:
>>
>>   local t = {}
>>   for i=1,100 do t[i] = tostring(i) end
>>   local x = 0
>>   for i=1,100 do x = x + t[i]:len() end
>>   print(x)
>>
>>  The dispatch in the second loop first involves a lookup of the
>>  "__index" table in the string metatable. Then "len" is looked up
>>  in this table and the resulting function (string.len) is called.
>>
>>  Ok, so run it with:
>>
>>   luajit -jdump=im test.lua
>>
>>  Here's the loop part of the second trace:
>>
>>  ->LOOP:
>> [..snip..]
>>  Pretty short, eh? As you can see, all dispatch has been hoisted.
>>
>>  --Mike
>>
>
> Well, the next step in functions like __add(z1,z2) or __mul(z1,z2)
> will be to check that the second argument is indeed of the type
> "complex". Typically it is done with luaL_checkudata which (1) pulls
> object's metatable onto the stack (2) looks up the required metatable
> in the REGISTRY with  lua_getfield(L, LUA_REGISTRYINDEX, tname) by its
> name string and (3) compares two metatables. It is a lot of overhead
> for +-/* of complex numbers. I do not see how LuaJIT can help here,
> for this overhead happens on the C-side of things outside of LuaJIT
> control. Is a good solution possible?
>
> --Leo--
>

Have you tried getting the metatable of both arguments and simply
checking that they are identical?

Alternatively, you could store the metatable in an upvalue of the
metamethods, and compare to that instead of using the registry-based
luaL_checkudata().

-Duncan