[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A proposal for faster userdata type checking
- From: "Wesley Smith" <wesley.hoke@...>
- Date: Fri, 29 Feb 2008 00:55:44 -0800
Here's my check_udata function:
template <class T>
void * Udata<T> :: check_udata(lua_State *L, int ud)
{
void *p = lua_touserdata(L, ud);
if (p != NULL && lua_getmetatable(L, ud)) {
lua_pushlightuserdata(L, (void *)T::name);
lua_rawget(L, -2);
if(!lua_isnil(L, -1)) {
lua_pop(L, 2);
return p;
}
lua_pop(L, 2);
}
return NULL;
}
T::name is a static const char * for the udata class. I use it as a
unique ID for the userdata class.
wes
On Fri, Feb 29, 2008 at 12:49 AM, Diego Nehab <diego@tecgraf.puc-rio.br> wrote:
> Hi,
>
> As the Mike's example shows, if you are willing to be a
> little less generic, you can store the metatable as an
> upvalue or as the enviromnent for each function that must do
> type checking for that class. That way, you save one rawget
> during the checkudata call, which is by far the most common
> case. I might do that, and offer two versions of checkudata.
> The generic one taking the lightuserdata, and obtains the
> metatable from it. The optimized one takes no extra
> argument, and assumes instead that the expected metatable is
> the environment.
>
> Kind regards,
> Diego
>