lua-users home
lua-l archive

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

Diego Nehab wrote:
> Readability: Not suggesting that at all. I am just following your
> premises. The syntax you said you tried is heavier than the one I
> suggested. If these functions return the l-value, you can even nest
> them.   

The premises in question were just a temporary solution before porting
most of the 3D code to C. I'd like to avoid that functionnal/OO syntax
as much as possible for that kind of code. I'd rather go with a kind of
expression compiler, something like:

local update_matrix = compile(function(position, orientation, scale)
    return translation(position) .. rotation(orientation) ..
end, 'matrixh', 'vectorh', 'quaternion', 'number') -- first type is the

update_matrix(self.global_matrix, self.position, self.orientation,

Here 'compile' would call the function with specialy crafted userdata
that would record the operations rather than apply them, and build an
optimized function. Of course this excludes conditionnals, but for the
kind of math I'm doing that would be handy already.

> I thought there were a few optimization strategies that you had not
> considered. I am trying to help by pointing them out. Locals do make
> a difference in tight loops. I has been pointed out in this list
> before.   

I initialy posted to signal I met the same problems as the original
poster, but actually I gave up Lua level optimizations some time ago.
However I'm still interested in the discussion and your help is welcome

> Safety: Not suggesting that either. Just saying that you can use a
> single table lookup for type safety, instead of getting a metatable
> from the object, another from the registry (keyed by a string!), and
> comparing them. You could use a weakly keyed table of valid light
> userdata, keyed by their values, and store it as an upvalue to the
> functions that need to do error checking. Error checking would reduce
> to one table lookup (keyed by something easier to hash) and a isnil
> check.       

Correct me if I'm wrong, but the trade is one getmetatable (which afaik
is not a table lookup), one table lookup (the type metatable from the
registry) and a rawequal for my current method, versus two table lookups
(the weak table from the registry and the object inside the weektable)
and a isnil. I can believe there's a gain but it's not obvious. Or maybe
I missed something.
> I don't know how much faster that would be, but I am using this
> strategy in the new version of LuaSocket. Very clean, very simple,
> totally safe.  

Is that in the last released version of in one about to be released ?

> Finally, have you tried using LuaJIT? I know nothing about it, but
> maybe the function call overhead magically disappears? 

I'm developping on Windows x64, which neither LuaJIT nor luatcc support.
However afaict LuaJIT is designed to optimize plain Lua code and data in
tables. I have to use userdata to pass them to OpenGL API, and thus I
have to use many lua_CFunction to manipulate them, so I doubt LuaJIT
will help out of the box (I tried it quickly on a 32bits build without
configuring anything and the overall performance gain was barely
noticeable). I could retry interning the functions I use most and
tweaking JIT compilation, but that requires some work and my TODO list
is quite long already.