lua-users home
lua-l archive

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


On Friday 25 June 2004 17:47, Marius Gheorghe wrote:
> > local function vector3_add(self, o)
> >     local rtn = self();
> >     for i=1,3 do
> >        rtn[i] = self[i] + o[i];
> >     end
> >     return rtn;
> > end
> >
> > The question is, can anyone think of a way to either:
> > * Alter my object usage and design patterns to create fewer
> > short-lived objects while not overly killing convenience
>
> Here is a possible idea to eliminate the temporary objects, however I am
> not sure if it would work in more complex expression evaluations:

As it is, it wouldn't work if you used the same function more than once in an 
expression. It does have a possible extension though: keep a pool of vectors 
in an array, and increment a next_free index every time you return one. Then, 
after each bout of vector manipulation, and having been sure to clone any 
vectors that you actually want to keep, reset next_free to 1 (or to the value 
at function entry, depending on your code shape) so that the pooled vectors 
can be reused.

-- Jamie Webb