This looks very interesting, as I was looking for a solution to this exact problem.
One request I have that would make this even better for my application would be to add compiler-level support to limit vectors to 2D. There seems to be partial support via the LUA_VEC_SIZE constant, but I think it should be extended to every place where x, y, z, w are specified. I see two advantages to doing this: 1) in 2D mode, the width of Lua data structs wouldn't need to be increased, so the memory footprint would be largely unchanged; and 2) dot product would return the 2D result rather than the 3D, and perhaps other operations, such as the length calculation, could be slightly optimized.
I could make these modifications myself, but it would be nice to have them supported moving forward.
Thanks for this great idea!
Chris
On 4/1/2010 10:19 AM, Petri Häkkinen wrote:
(reposting, the first one apparently didn't go through)
Hi there,
We've recently been tinkering with Lua core to add support for a new primitive vector data type similar to float4 datatype in shading languages like HLSL and GLSL. The result is lua-vec, or Lua 5.1.4 with vectors. The motivation was to create an efficient vector implementation for 3d applications such as games, 3d visualizations and prototypes of various rendering algorithms. After trying out different less intrusive options like vectors implemented in standard Lua, userdata vectors written in C and even hacked non-persistent light userdata vectors, it soon became apparent that none of the existing approaches work well for vector heavy applications. The main source of inefficiency was determined to be garbage collection of temporary vectors created by arithmetic operations. Thus the idea of a more intrusive approach was born: could we hack the very core of Lua to add support for vectors? Well, the answer is yes.
For a longer answer please visit the homepage of our highly experimental project at:
http://code.google.com/p/lua-vec/
So far the results are promising: speed up by a factor of 4.5 in a vector heavy benchmark (a global illumination renderer). Interestingly, lua-vec's speed in this benchmark is quite close to LuaJIT. However, we're still far from the speed of a full C++ implementation. The question is how much faster could LuaJIT be if it was augmented with native vectors? We're currently wondering how much work it would be to similarly hack vectors into LuaJIT.....
Here are the bechmark results (more detail in project wiki, values are rendering times in seconds):
1217s std lua
770s userdata vectors
274s lua-vec (our implementation)
234s luajit
??s luajit& lua-vec
10s cpp
Comments, bug reports and criticism of our unorthodox way are always welcome!
On behalf of lua-vec team,
Petri Häkkinen
p.s. the other team member is my brother Henri Häkkinen.