lua-users home
lua-l archive

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


Shannon Stewman wrote:
>
> 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.
> 
> 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;
> }

"Wer misst, misst Mist!" ;-)

If this is faster you're having a really strange system.

You replace a strcmp (in luaL_checkudata) with a lua_push-
string (in luaL_getmetatable).  Since lua_pushstring performs
a strlen + calc-hash + hash-lookup + memcmp I can't see how
that could be faster.  I even guess that luaL_checkudata was
implemented as it is to avoid using lua_pushstring...

If luaL_checkudata is really such a bottleneck in your system
how about switching to different checking method - i.e. one
that doesn't require table lookups or string handling?

> 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?

There's no need to compare the pointers.  Use lua_rawequal
to compare the two objects.

Ciao, ET.