lua-users home
lua-l archive

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



If my memory serves me right, this scenario can be (could be) solved by the Lua authors by a third kind of "middle" userdata, which would be pointer based (like light userdata is) but still use metatables. This way, pointers could be given equality metamethods.

It's been a while since I dug in this pit, though, and I didn't think proposing a third kind would really be worth it.

-asko


Colin kirjoitti 19.11.2007 kello 20:33:

On Fri, Nov 16, 2007 at 09:05:48AM +0900, Miles Bader wrote:
Colin <share-lua@think42.com> writes:
I would guess that one of the reasons of the missing __hash meta event is that it's not that easy to design -- how do you make sure that the __hash meta event is "compatible" with the Lua hash function and the other table
internals?

Not to mention a non-negligible speed hit in a rather critical area of
Lua!  Making table lookups slower makes _everything_ slower.

(As long as its not slower than the workarounds ... for me, the suggestion of creating caches of all userdata objets to reuse the same when whenever one with the same value is needed does not sound too good because in my application typically much more userdata objects are created than are used
for table lookups.)

It would probably be much more acceptable if user-defined __hash hooks
were only used for tables with some flag turned on (which could be
tested very quickly in the C core), with that flag defaulting to off.

Hm, this, and the suggestion of using the index/newindex metamethods does not work for me either because I want the mechanism to work by default,
however it gave me the following idea:

How about a flag on every userdata object that switches between the current pointer-based hashing for tables, and one that uses the actual userdata (of which the address and length is known) i.e. one could use the same hashing
as for strings, and memcmp() for the equality check)?

Regards, Colin