lua-users home
lua-l archive

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


On Thu, Jan 30, 2014 at 7:54 PM, Josh Haberman <jhaberman@gmail.com> wrote:
> If you optimize this pattern enough, you start getting to the point
> where the malloc/GC/cache effects of allocating a new "row" every time
> start to become significant.


sometimes, when i have an "accesor" function that returns a table, i
add an optional 'base' table parameter to let the user choose if they
want to reuse a table allocate a new one:

function get_element(stream, e)
    e = e or {}
    -- fill e fields
end

so, if you omit the base table, you get a new one:

elm = get_element(stream)

in a loop, you can either get different elements each time:

local tbl={}
while not_empty(stream) do
    tbl[#tbl+1] = get_element(stream)
end

or reuse the same table:

local e=nil
while not_empty(stream) do
    e=get_element(stream,e)
    -- use e
end

-- 
Javier