lua-users home
lua-l archive

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


On 17.11.2010, at 21.01, Mike Pall wrote:

> Petri Häkkinen wrote:
>> Have you considered adding support for native 2d/3d/4d vector
>> types? I think this would make many game developers including me
>> very happy. Allocating and garbage collecting hundreds or even
>> thousands of temp vectors created each frame is currently a
>> waste of precious CPU cycles.
> 
> You'll be able to create _any_ C type at runtime from pure Lua
> code. E.g. this creates an array with 1000 vectors:
> 
>  ffi.cdef[[
>    typedef struct { double x,y,z; } vector;
>  ]]
> 
>  local v = ffi.new("vector[1000]")
> 
> This needs exactly the same amount of space as it would take in
> C/C++. But of course you should be just as reluctant as in C/C++
> when using 'new'. Even in C you need to recycle or pool objects if
> you want maximum performance.
> 
> It's hard for any compiler to eliminate the allocation overhead if
> you create tons of small temporary objects and pass them around
> everywhere.

Yes, but in C/C++ you can avoid the cost of allocating and freeing vectors by using value semantics, allocating vectors on the stack and passing by reference. Also vectors can be added as data members to structs and classes without extra cost. In Lua this could be implemented by having a native vector type in the VM. Better yet this could use SIMD instructions if they're available on the target platform. I actually hacked value semantics vectors into standard 5.1 Lua VM some time ago with major perf improvements (see lua-vec @ google code) but unfortunately making a patch for LuaJIT is out of my reach.  
 
Petri