lua-users home
lua-l archive

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

Encouraged by Lua's metamethods, I find myself cheerfully
doing things like:

result = ((vector1 + vector2) / 2):cross(vector3):normalize();

While this is wonderfully convenient, a woeful downside of
this is the number of extremely short-lived objects that get
created along the way.  One of my object-heavy apps (a mesh
simplifier) thus profiles as spending 20-40% of its time
solely in garbage collection (lua 5.0/5.1).

To support the natural syntax above, my Vector3 object's
metamethods return a newly-instantiated object of the same
type, i.e.:

local function vector3_add(self, o)
   local rtn = self();
   for i=1,3 do
      rtn[i] = self[i] + o[i];
   return rtn;

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
* Fairly easily 'tweak' Lua to optimize for the very-short-lived
object case

(Would the latter be one of the things traditionally covered
efficiently by a 'generational' garbage collector?)