lua-users home
lua-l archive

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


Answer in the text

-----Original Message----- 
> From: "Leo Romanoff" <romixlev@yahoo.com> 
> To: "Thomas Jericke" <tjericke@indel.ch>, "Lua mailing list" <lua-l@lists.lua.org> 
> Date: 31-03-2013 15:20 
> Subject: Re: Lua and preemptive scheduling for coroutines 
> 
> > This  project now runs for over a year and we actually have a lot of machines  
> > in the field. So don't listen to those who say that this won't work. It  
> > is just a question whether you like to have per-emptive threads.
> 
> Yes, I haven't listened and just implemented it ;-)

That's the spirit!

> 
> I'm not sure you really need to call your hook at any instruction. Simple arithmetic opcodes, access to variables and the like is not so interesting for interception. They never consume a lot of time.
> I could be enough to intercept only CALLs (i.e. control flow opcodes and something that may result in invocations of (long lasting) functions) and in addition every N instructions probably (for a very long linear code or for tight loops without any calls).

Well I didn't wanted to mess with this stuff. I call my instruction hook at the very same point in lvm.c, where the debug hook is called. The call is very slim and and the function called is a C function which is very short. I might get a little better performance if I look at the op code first, but on the other hand, this check would take some time as well. And there are cases where simple opcodes (loadk) can take a lot of time, not a single one, but think about a huge table (thousands of entries) being loaded. For us this is a realistic use case as we use Lua for scripting and for passing structured data.

I might want to add one thing, another place where yielding is not possible is during a require. So I patched the require function to support yielding. I actually could release that patch. We might should have some kind of patch archive with patches that make the Lua standard library functions yieldable. I currently have patches for require() and tostring(). Unyieldable functions are the arch enemy of scheduling based on yielding, that's why you should use Lua 5.2 for sure. If you haven't yet, check this part of the manual: http://www.lua.org/manual/5.2/manual.html#4.7

-- Thomas