HI
I found that lua table use 2^n to hash key. normaly we use the prime number to prevent hash collision.
now I use lua-5.3.4, and the code below will cost about 6 second because the hash table degenerates into a linked list
so why lua table don't use prime number to hash?
function alloc_id(index)
return ((index & 0xffffffff) << 32) | 1
end
local src = "">local index = os.time()
for i = 1, 30000 do
local id = alloc_id(index)
index = index + 1
table.insert(src, id)
end
local dst = {}
local begin = os.time()
for k, v in ipairs(src) do
dst[v] = 1
end
print("cost " .. os.time() - begin)