lua-users home
lua-l archive

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

Diego Nehab wrote:
>>> Safety: Not suggesting that either. Just saying that you can use a
>>> single table lookup for type safety, instead of getting a metatable
>>> from the object, another from the registry (keyed by a string!), and
>>> comparing them. You could use a weakly keyed table of valid light
>>> userdata, keyed by their values, and store it as an upvalue to the
>>> functions that need to do error checking. Error checking would
>>> reduce to one table lookup (keyed by something easier to hash) and
>>> a isnil check.
>> Correct me if I'm wrong, but the trade is one getmetatable (which
>> afaik is not a table lookup), one table lookup (the type metatable
>> from the registry) and a rawequal for my current method, versus two
>> table lookups (the weak table from the registry and the object inside
>> the weektable) and a isnil. I can believe there's a gain but it's not
>> obvious. Or maybe I missed something.
> There is only one table lookup. The weak table is not in the
> registry, it is an upvalue to rotate/translate/copy. 
> Upvalues should be pretty fast. Plus, the table lookup that actually
> happens uses a lightuserdata as the key. The luaL_checkudata function
> uses a full string, not even a literal string.  I think the string
> has to be traversed for its length, hashed, and compared, perhaps
> even allocated if not found. The lightuserdata is just a pointer, so
> I expect the lookup to be much faster.  Then again, I haven't
> measured the gains yet (but at some point I will).      

I missed the upvalue part, this makes much more sense to me now.
Actually all my object creation and type checking are already in a few C
functions (new_foo, push_foo, check_foo, to_foo for each type foo), so
it should be easy to replace my current type checking system by yours.
I'll give it a try when I get some time.