lua-users home
lua-l archive

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


Am 24.10.2013 09:48 schröbte Coda Highland:
On Thu, Oct 24, 2013 at 12:16 AM, Luther <lutheroto@gmail.com> wrote:

The loop shows two identical sequences of keys. However, in different
runs of the program, the order of keys changes, as if the order is
completely random. I know Lua doesn't give any guarantees about the
order of keys, but I can't think of any reason why the hash table would
be constructed differently on each run. Can anyone tell me what's going
on here?

(I know C, so you can point me to the relevant source code if you think
it will help me understand.)

Luther



Because Lua's tables are hash tables. They're an unordered collection
-- they're organized according to an internal hash code for
performance, and that hashing function has some randomization involved
to help avoid pathological worst cases or maliciously-constructed
attacks.

See here[1] and here[2].

  [1]: http://lua-users.org/lists/lua-l/2011-12/msg00813.html
  [2]: http://lua-users.org/lists/lua-l/2012-01/msg00497.html


I don't know the Lua internals well enough to point you at the code,
but hash tables are fairly common so if you want to know more about
the general concept you should have no trouble looking it up.

Look for `makeseed` in `lstate.c`.


/s/ Adam


Philipp