lua-users home
lua-l archive

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


on 6/4/04 8:41 AM, Virgil Smith at Virgil@Nomadics.com wrote:

> In fact the only real "complexity"/"gotcha" here is that the following
> version of "Returning a closure" is VERY WRONG
> 
> -- Returning a closure (INCORRECTLY)
> -- This will force tbl itself to be persisted
> -- in the stream
> mt.__persist = function(tbl) do
>  return function() do
>     return {x=tbl.x, y=tbl.y, z=tbl.z}
>  end
> end

That's also potentially an easy mistake to make. Also, relying on generating
lots of closures or auxiliary tables creates just to strip stuff out creates
a lot of extra memory allocation traffic which hurts efficiency. Now, if one
went with tables rather than closures or in addition to closures as the
results of the __persist metamethod and passed the method an empty table
that the persistence mechanism would clear and reuse later, you might be
able to work around that memory overhead issue.

Mark

P.S. These sort of cases make a table.clear function that resets a table
back to the empty state attractive.