lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> My last mail where I compared the memory of different concatenation methods is 
> bugging me. table.concat in Lua 5.2 is now as almost bad as working in a loop. 
> I know it can't be as good as OP_CONCAT because it has to handle lists larger 
> than the stack. But to be pathological is a regression. Of course it only 
> matters when the source string is long.
> 
> Also, string.format in Lua 5.2 makes a lot more garbage than in 5.1. But I 
> don't think it's used for concatenating many strings that often.

I do not think your comparison makes a typical use of concat. It
concatenated a very few number of very large strings, while (I guess)
the typical use is to concatenate a large number of small strings (e.g.,
lines in a text).  Moreover, there may be complete GC cycles *while*
you generate the concatenation, so your test may not be showing correctly
the total garbage generated.

Despite these problems, the concat implementation in 5.2 can use more
memory than in 5.1, but it should be also more efficient. See the
next example. (This is just one more example.)

-- Roberto

cal mt

local function tracememory ()
  if newproxy then
    mt = getmetatable(newproxy(true))
    mt.__gc = function (u)
      print(collectgarbage'count' * 1024)
      newproxy(u)
    end
  else
    mt = {}
    mt.__gc = function (u)
      print(collectgarbage'count' * 1024)
      setmetatable({}, mt)
    end
    setmetatable({}, mt)
  end
end

local s1 = string.rep('x', 80)

local a = {}
for i = 1, 1000000 do a[i] = s1 end

collectgarbage(); tracememory()

local t = os.clock()
local s3 = table.concat(a)
print("time", os.clock() - t)