lua-users home
lua-l archive

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


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,

-- 
Shannon Stewman         | Let us walk through the waning night,
Caught in a whirlpool,  | As dawn-rays tickle our toes, the dew soothes
A quartering act:       | Our blistered soles, and damp bones stir
Solitude or society?    | As crimson cracks under the blue-grey sky.