lua-users home
lua-l archive

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


Shawn Fox wrote:
> Is the callback feature assuming that the application loop is being driven
> by the Lua code?

No, you can run a callback wherever you need it -- it's just a
C function pointer.

> 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?

This is what the callback feature would enable.

But please note this is a misdesign. Every C <-> Lua transition
has an overhead and neither your C compiler, not LuaJIT can
produce effective code for that. Basically everything is
'volatile' across such a call and all data has to go through
memory. No state can be cached, no code can be hoisted, nothing
can be inlined.

Performance will suffer, compared to a pure Lua solution. You'll
be disappointed with it, no matter what I come up with.

> 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.

Yes.

> 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.

Bad design. Who missed the CS course where they explain why
push-style APIs are bad? :-)

So what magical things does that C++ code do and why can this not
be written in Lua? No, you don't have to answer that, better work
on fixing it.

--Mike