[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: reusing objects for tight iteration loops
- From: Javier Guerra Giraldez <javier@...>
- Date: Thu, 30 Jan 2014 20:52:24 -0500
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