@DarthFS:
Table rehashing occurs in power of 2. Appending a new key will increase either the size of the hash or the array part of the table, in powers of 2 respectfully. All dictionary like keys will be appended to the hash part in powers of 2, while array like keys will be appended to the array part in powers of 2. Also, if an array part increases by size, then the hash part will also increase in size despite no slots being allocated to it, though this is nothing to worry about since Lua will set the hash part to a dummy node vector so that extremely little - to no memory will be used by the hash part. Same goes when a hash part increases in size.
-- powers: 0, 1, 2, 4, 8, 16, 32, 64 .....
-- each key needs 1 slot!
-- lua will create 1 slot in the hash part when creating this table:
local t = {
bo = 5
}
t.e = 5 -- rehash, this key needs another slot! hash part size after rehash: 2
t.c = 1 -- another rehash, needs another slot! hash part size after rehash: 4
t.lo = 5 -- no rehash, previous rehash created 2 more slots!
Another example:
-- lua will create total of 8 slots for this table while creating it
local t = {
1, -- 1
2, -- 2
3, -- 4
4, -- 4 (previous rehash allocated 1 more extra slot)
5, -- 8
}
After even 5000 iterations / insertions, the memory address of the array did not reallocate when printing the table. In @RinxfulROBLOX situation all he did was append 4 new keys into his table.
Onedar most likely confused table rehashing with allocation, as seen by his points.
In Lua, dynamic tables having to reallocate is really nothing to worry about, unless you are implementing your own custom implementation of a dynamic array in lower level languages where static arrays are utilized and memory management is less abstracted from the developer.
It is useful to know a little about how Lua has implemented tables, in the context of optimization. This is also stated in the doc @onedar linked.