[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Faster checking of userdata
- From: Edgar Toernig <froese@...>
- Date: Sat, 28 Aug 2004 03:38:02 +0200
Fabio Mascarenhas wrote:
>
> A safe alternative, that does not use strcmp, [...]
I think one of the fastest methods is to store the metatable
as an upvalue in all Cclosures that make use of the userdata
and then use lua_rawequal for checking:
void *
my_checkudata(lua_State *L, int idx, int metaidx /*not rel*/)
{
if (lua_getmetatable(L, idx))
{
int ok = lua_rawequal(L, -1, metaidx);
lua_pop(L, 1);
if (ok)
return lua_touserdata(L, idx);
}
return NULL;
}
#define META_FOO lua_upvalueindex(1)
struct foo *p = my_checkudata(L, META_FOO);
...
struct foo *p = lua_newuserdata(L, sizeof foo);
lua_pushvalue(L, META_FOO);
lua_setmetatable(L, -2);
...
All operations are cheap - no table lookups or string handling.
Of course this method has a downside: managing which closures
need which upvalues may become a nuisance...
Ciao, ET.