lua-users home
lua-l archive

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


Shannon Stewman wrote:

Hmm, did you benchmark this? I would be surprised if your function is faster than luaL_checkudata, because luaL_getmetatable uses lua_pushstring. This function is pretty costly, compared to lua_rawget and strcmp (it does a strlen and computes a hash of the string).

Your best bet is to use a light userdata to identify your metatables, like the other poster suggested, instead of a string.

--
Fabio Mascarenhas

Hello all,

After profiling some C extensions I had written, I noticed that many
of my fast routines spent an enormous percentage of time (60% or more)
calling luaL_checkudata, and most of that was spent in its strcmp call.

I wrote a faster routine that works on pointers, and should work for
both userdata and tables.

Is there are problem using this technique, assuming the metatables I'm
comparing are allocated in a library's luaopen_XXX function and only
freed on a lua_close() call?

void* fastcheckudata( lua_State* L, int index, const char* id)
{
  lua_getmetatable(L,index);
  const void* p1 = lua_topointer(L,-1);
  luaL_getmetatable(L,id);
  const void* p2 = lua_topointer(L,-1);

  lua_pop(L,2);

  return p1 == p2 ? lua_touserdata(L, index) : NULL;
}

Best,