lua-users home
lua-l archive

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



On Wed, Nov 17, 2010 at 9:20 PM, Petri Häkkinen <petrih3@gmail.com> wrote:

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

Sorry to be stubborn here, but I'd like to start a new thread on this if you, Mike, have time and interest to talk about this.

Just to be sure that we're on the same page; I think vectors should be like other atomic data types, e.g. like numbers, rather than being objects. This would eliminate the allocation cost altogether.

Modern games are full of vector manipulation, so this is basically the only thing stopping us from creating big commercial quality games with LuaJIT. I'm dreaming of developing games with LuaJIT with only minimal low level subsystems (like rendering) written in C/C++. Traditionally games have been written almost completely in C++ but the iteration loop is horribly slow with compile-run-reload game state-test cycle taking forever on big projects. With LuaJIT virtually every aspect of the game could be changed at runtime (aka live updates).

I'm sure there are other uses for vectors outside the game industry. Image and audio processing come to mind instantly. Ok these are maybe a little contrived examples I have to admit because usually image/audio filters are tiny pieces of code where live updating is not that crucial.

Basically what we did with lua-vec was to double the size of Value struct so that it could hold a 4D float vector. Doubling the size of every value is quite nasty yes, but perhaps this could be implemented better in LuaJIT... What do you think?

Is there any technical reasons why native first class vectors wouldn't work with LuaJIT? How difficult would this be to implement?

Anybody else sharing my dream?

Petri