[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: reusing objects for tight iteration loops
- From: William Ahern <william@...>
- Date: Thu, 30 Jan 2014 18:47:08 -0800
On Thu, Jan 30, 2014 at 04:54:25PM -0800, Josh Haberman wrote:
> Let's suppose I'm iterating over a large data set, and for every "row"
> of the set I want to execute some Lua code.
>
> I could implement an iterator, then my users could use for/in:
>
> for row in fast_iterator(data_stream) do
> do_something(row.foo, row.bar)
> end
>
> 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.
The idiomatic way to do this, based on several different libraries I've
seen, is to add a mode where the iterator returns a list of values on the
stack, rather than stuffing everything into a table. Have the iterator
function take a possibly empty list of key arguments. If the list is empty,
just return a row. This is the "convenience" mode. If the list is not empty,
then it's a list of values to return.
This can even be faster than reusing a table.