lua-users home
lua-l archive

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

Francesco Abbate wrote:
> I was wondering if the JIT compiler is also able
> to optimise Lua functions called with 'lua_call' from within a C
> library code (called, in turn, from Lua top level).

Yes, if that Lua function contains a loop.

No, if you repeatedly call a 'flat' Lua function from C. I.e. if
the inner loop is on the C side and the Lua side is just a short
non-looping callback (like the comparison function of table.sort).

The Lua part of the latter variant could be compiled as a
non-looping trace. This is probably going to be added when I add
tracing of recursion, because it needs similar support code.

But you have to realize that context switches between C and Lua
are costly, that nothing can be hoisted out of such a trace and
that the compiler cannot optimize the C side and the Lua side

You are going to get much better performance if you stay on the
Lua side for as long as possible. This also applies to upcalls
to C functions: avoid them in inner loops written in Lua.