[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: LuaJIT FFI callbacks for functions using only built-in operators and locals
- From: Mike Pall <mikelu-1112@...>
- Date: Thu, 15 Dec 2011 22:14:02 +0100
Adam Strzelecki wrote:
> Just asking whether it would be technically possible, to
> generate code for simple functions without all marshaling (VM
> barriers) that happens on caller code and Lua callback code?
> I.e. "function (x) x+1 end" that is mapped into "int
> (*addone)(int v)", this function is using only built addition in
> Having that such function use only built-in operators, locals
> and single types across whole body, could it be optimized
> straight ahead into machine code?
It would be possible to reduce the marshaling overhead with a lot
of effort. But there's no easy way to avoid the cost for entering
and leaving the VM in general. And that makes up the majority of
> This could be some benefit to some math packages that can take
> callback function as argument, calling it a lot.
That's a dead end, as I've described in the FFI docs. Don't use
callbacks if you care about performance.
Francesco Abbate has shown with GSL Shell that rewriting the
C parts in Lua leads to better performance than any callback-based
C solution ever could. Since everything is in Lua, the JIT compiler
can inline these trivial functions and optimize them together with
the surrounding code (e.g. an integration algorithm).