[Dev-luatex] Memory leak in string.explode()?

Hans Hagen pragma at wxs.nl
Wed Nov 7 13:08:33 CET 2012


Hi Reinhard,

At my end, this works best:

function io.readall(f)
     local size = f:seek("end")
     if size == 0 then
         return ""
     elseif size < 1024*1024 then
         f:seek("set",0)
         return f:read('*all')
     else
         local done = f:seek("set",0)
         if size < 1024*1024 then
             step = 1024 * 1024
         elseif size > 16*1024*1024 then
             step = 16*1024*1024
         else
             step = math.floor(size/(1024*1024)) * 1024 * 1024 / 8
         end
         local data = { }
         while true do
             local r = f:read(step)
             if not r then
                 return table.concat(data)
             else
                 data[#data+1] = r
             end
         end
     end
end

usage:

local f = io.open(name)
if f then
   data = io.readall(f)
   f:close()
end

upto 50% faster and often less mem usage

Hans


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the dev-luatex mailing list