Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase memory to handle super large Lua tables

I have a Lua function that, given n, generates all permutations of the series from 1 to n and stores each unique series in table form within a container table.

The size of this generated table gets very large very quickly (and necessarily so). About the time I try n = 11, the script will run for several seconds before failing out to "lua: not enough memory." I have 16gb of physical RAM, but watching the performance monitor in the Windows task manager allows me to watch the ram be consumed during run time, and it only gets up to about 20% before the script ends with the memory error.

I found this post that looks like the direction I need to head: memory of a process in Lua

Since I'm running my script with Lua.exe, I'm assuming that I'm limited to how much memory Windows allocates for Lua.exe. Can I increase this amount? Can I use a C# wrapper program to simply run the Lua script (the idea being that it will have a higher/less restricted memory allocation)? Or am I looking in the wrong direction?


like image 252
Conor Avatar asked Dec 12 '22 22:12

Conor


1 Answers

Do you need to store all the permutations in advance? You could generate them on-the-fly instead.

Example:

local function genPerm(self, i)
  local result = {}
  local f = 1
  for j = 1, self.n do
    f = f * j
    table.insert(result, j)
  end
  for j = 1, self.n-1 do
    f = f / (self.n + 1 - j)
    local k = math.floor((i - 1) / f)
    table.insert(result, j, table.remove(result, j+k))
    i = i - k * f
  end
  return result
end

local function perms(n)
  return setmetatable({n=n}, {__index=genPerm})
end

local generator = perms(11)
for _, i in ipairs {1, 42, 1000000, 39916800} do
  print(table.concat(generator[i], ','))
end
like image 192
finnw Avatar answered Dec 27 '22 01:12

finnw