lua-users home
lua-l archive

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


Diego Nehab wrote:
> Hi,
> 
>> local midentity = matrixh()
>> local valtitude = vector()
>> function model:update_fast()
>>    local global_matrix = self.global_matrix
>>    geometry.matrixh.copy(global_matrix, midentity)
>>    global_matrix:rotate(self.orbital_orientation)
>>    valtitude.y = self.altitude
>>    global_matrix:translate(valtitude)
>>    global_matrix:rotate(self.orientation)
>>    global_matrix:translate(self.position)
>> end
>> 
>> And even if that optimized form is acceptable for entity update, for
>> skeletal animation of a couple hundred of entities with 64 bones each
>> it just takes too much time to run at interactive framerate, so
>> instead I rewrite these critical bottlenecks in C.
> 
> If you are willing to use this syntax, you could perhaps go the next
> step and use light user data for matrices? You could then completely
> avoid Lua's garbage collection (and production).  

With the above syntax I avoid most object creation, and at that point unfortunately the cost of Lua->C function calls become the bottleneck.

However the initial problem is not really the garbage collection, but the amount of objects created on the fly. Arithmetic operator metamethods have no access to the place where the result will be assigned, so they have to create (allocate) objects on the fly. Whether the object is a table, a full userdata or a buffer allocated by the host app has basically the same cost.

The reason this cost is significative is because the allocation has to be on the C heap (while for example C++ operator overrides can manipulate objects on the C stack). Perhaps the ability to create full-userdata *in* the Lua stack directly, à la C/C++, rather than just pushing a reference to a heap-allocated object would help. That would be an initial position for the userdata, which would be moved to the heap as soon as another reference (than its stack slot) to it is created. Otherwise it would simply be deleted at the same time it is poped from the Lua stack.