Re: Yielding across C boundaries

  • From: Mike Pall <mike-1301@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 3 Jan 2013 00:17:25 +0100

Daurnimator wrote:
> Can we expect to eventually see lua_yieldk, lua_callk, and lua_pcallk
> added to luajit?

Not without switching to the Lua 5.2 ABI (i.e. unlikely anytime
soon).

> > You're probably using LuaJIT for its performance benefits. But
> > callbacks from C into the VM are slow -- really slow compared to
> > everything else. If possible, rewrite the intervening C code in
> > Lua (maybe with the help of the FFI) and/or switch to a pull-style
> > API (iterator instead of callbacks).
> 
> Btw, why is this? It came up in an irc channel a couple of days ago:
> I said that iteration would be favourable; but was unable to cite why...

Entering the VM needs a lot of state setup and leaving it isn't
free either. Constantly entering and leaving the VM via a callback
from C *to* Lua has a high overhead. For short callbacks, the
switching overhead between C and Lua may completely dominate the
total CPU time.

Calling an iterator written in C via the FFI *from* a Lua program
is much cheaper -- this compiles down to a simple call instruction.

Staying within Lua code and calling a Lua function from Lua (both
callback-style or iterator-style) is even cheaper: the compiler
can inline the callee, which helps many other optimizations.

--Mike

Other related posts: