I traced the problem to the table.orderedPairs() function in utils.lua. In addition to not correctly cleaning up after itself, it clobbers the global variable key and uses O(n^2) comparisons against the sorted index table (the previously mentioned "__orderedIndex" field) over the course of the full table iteration.
Here's a version that is shorter, easier to read (I think), and may run faster without touching any globals or leaving remnants that could cause problems. This one function replaces __genOrderedIndex(), orderedNext(), and table.orderedPairs() in utils.lua (lines 291-330).
Code: Select all
function table.orderedPairs(t)
local sorted_keys = {}
local i = 0 -- iterator index
local n = 0 -- size of key table
for key, _ in pairs(t) do
n = n + 1
sorted_keys[n] = key
end
table.sort(sorted_keys, cmp_multitype)
return function ()
i = i + 1
if i <= n then
local k = sorted_keys[i]
return k, t[k]
end
end
end