Re: gc bug in 2.0.4?

  • From: Mike Pall <mikelj-1611@xxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 15 Nov 2016 15:58:33 +0100

Aleksandar Kordic wrote:

So the solution is:

local ffi = require"ffi"
local cType = ffi.typeof("float[?][2]")
math.randomseed(1)
for i = 1, 10 do
    local t = {}
    local counter = math.random(1000)
    for l = 1, counter do
        t[l] = {l, 0}
    end
    local z = cType(counter, t)
    collectgarbage()
    local sum = 0
    for l = 0, counter - 1 do
        sum = z[l][1] + z[l][0] + sum
    end
end

Nope. Don't create an intermediate table. That's expensive and
unnecessary. Directly initialize cdata fields. Also, you can rely
on zero-initialization and avoid setting fields to zero again.

  local z = cType(counter)
  for l=0,counter-1 do z[l][0] = l end

Also, don't allocate a new array every time, unless you want to
benchmark allocation overhead.

--Mike

Other related posts: