lua-users home
lua-l archive

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


On Mon, 18 May 2020 at 10:47, Andrea <andrea.l.vitali@gmail.com> wrote:
> this is indeed unique

i don't think it's unique.  it's called "object semantics", where the
identity of an object persists regardless of its content.  remember:

a={}; b={}  ===> a ~= b

a1={}; a2=a; a1[x] = y ===> a1 == a2


to keep the same behaviour as table keys as on direct comparison,
hashing the pointer address is appropriate.

that's why strings are more complex, even if they're implemented as
objects (allocated and moved by reference), they have "value
semantics" at the Lua level, so it's the content (not the pointer)
what is hashed.  also the deduplication, which makes it possible to
compare by pointers after they're interned (and further complications
for longer strings that are lazily internet and might not be
deduplicated)

some languages have other complex objects with value semantics (python
tuples are a nice example), and each one has special handling in the
implementation (immutability, specific hash and comparison functions,
etc.)

as for too much collisions because of alignment vs. 2^n table sizes,
looking at https://github.com/lua/lua/blob/master/ltable.c#L78-L85,
there are some "-1)|1)" to avoid the "too many zeroes" case.  at first
sight it doesn't look as effective as some bit-shuffling, but I
haven't done any tests at all.


-- 
Javier
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org