lua-users home
lua-l archive

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




On Thu, Feb 10, 2011 at 5:14 AM, Mike Pall <mikelu-1102@mike.de> wrote:
Shawn Fox wrote:
> How about adding the ability to get a function pointer back from luajit
> which would call a Lua function?

This is backwards. It's not an FFI for C, it's an FFI to be used
from Lua code. Callbacks will solve that particular problem (after
I implement them) and this is handled from Lua code.

--Mike

Is the callback feature assuming that the application loop is being driven by the Lua code?  What if I have a loop in a C/C++ application where I need to call a function in LuaJIT 100 million times, getting some result back for each call?  It isn't a situation where I can call Lua once and then have it deal with a large block of data, it must be driven externally by the C++ application.

So right now I have some code something like this:

bool processData(data,resultBuffer) {
    push Lua function onto stack;
    push data onto Lua stack;
    call Lua function;
    check for error;
    resultBuffer->value = get data from Lua;
    return true;
}

I don't have control of the loop itself, my code is being called with some data and a place to store the result, I call Lua to process the data, and return a result.  So I need a way to be able to pass data into Lua as quickly as possible.  Right now I use the registry and look up the function using a lightuserdata, but if I could instead get a function pointer back from Lua and call it that way I would think it would be quite a bit faster.

I think my use case may be somewhat non typical, but I've not seen what others are doing with Lua to any great extent.  The amount of work done on each call may be fairly small (the Lua code is supplied by the end user), but the number of times Lua is called can be huge, thus the performance can be dominated by looking up the function and pushing values onto the stack then popping the result back off.