[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: hash metamethod?
- From: "Alex Davies" <alex.mania@...>
- Date: Sun, 1 Feb 2009 22:46:37 +0900
Hey Joshua,
There is one problem with my proposed scheme that bothers me a bit. The
question is: what would a table return as its default __hash and __eql
methods? On one hand I'd want it to treat the table by value, calling
__hash and __eql on all of its individual members, so that tables with the
same contents hash the same.
I just want to say definitely not :). I don't think even a second should be
spent considering that - this is Lua, not Python. We're not afraid to call
two different tables just that - different. (Don't get me wrong, I think
Python's great - but I do think that this as default behaviour of its kind
of weird/pointless, and I cannot see how it could be implemented remotely
efficiently.) Similarly simply adding tables as keys (oftenly done) would
be very slow -potentially many comparisons of equality have to be done on
each key lookup - tables are all we have, lets keep them fast.
Anyway, a patch for a simple __hash would not be hard to make. Only a
couple of places would have to be modified. Again though, you have to
remember that tables are integral to Lua's performance - anything that
bloats or slows them really has to be considered very carefully. And with
that in mind, Lua has no extra space in the nodes in its tables to store the
hash of each key. So whenever the table gets resized, table keys would have
to be checked for a __hash metamethod and recalled (how does Python handle
it?). Along with being slow, this could also cause problems in the future
in the unlikely case that the gc starts resizing tables. (Actually I lie -
if you want to 8 byte align the Node data structure you could fit a hash
value in there).
Personally I do think a __hash metamethod would be a large improvement where
Lua is used largely as a standalone language. As an extension/scripting
language not quite so important.
Anyway, good luck keep us informed. :)
- Alex