Luvit – Asynchronous I/O for Lua(luvit.io) |
Luvit – Asynchronous I/O for Lua(luvit.io) |
The idea sounds neat, but I'm wary of concepts that aren't seeing real-world use. Node.js has momentum, Luvit has a little bit of momentum, this? In the web development community, I've never heard of the approach, which suggests using it might be more DIY than I'd like. Also the copyright date is 2004-2007. Abandoned?
There is the Kepler project http://www.keplerproject.org/ , which appears to be much like the commonJS project for javascript- with luasocket at its core. Kepler project lists http://slimfastclinic.com/ and http://www.bestjuicer.biz/ as websites that were built with lua. (though they may be out of date since the slimfastclinic.com says it's wordpress)
It might also be worth pointing out, just as a side note, that mod_lua for apache has existed for a while https://httpd.apache.org/docs/trunk/mod/mod_lua.html
For those who don't know, this is basically a clone of the Node.js API in the Lua language (using the LuaJIT compiler). This is interesting because:
- LuaJIT is often faster than V8 and uses less memory
- LuaJIT has a built in C FFI and supports C datatypes for working with low level libraries (or even writing low level code in Lua)
- Lua has coroutines
I think this is already being used in production at Rackspace, mainly because of the lower memory usage.
Luvit.io is specifically being used in our Host Monitoring Agent, which is open source and you can find here:
https://github.com/racker/virgo
This is used to collect all kinds of host metrics (CPU, disk, ram, custom scripts, mysql, etc) for the Cloud Monitoring Product.
- Lua (as a language) is for the most part better designed than JavaScript
Blogging about your experience using it would probably get you some decent traffic, too.
We're working on splitting this out into a reusable platform for writing lightweight host-based agents. Luvit works out fairly well for this - our monitoring agent runs in around 5mb resident memory, works on Windows (although we haven't released that yet) and is relatively easy to code on.
Shameless plug: if this sort of stuff interests you, hit me up, my email is in my profile.
It's still in dev branch, but I'll be merging it soon:
http://webmon.com/blog/2013/02/19/lthread-now-supports-userl...
https://github.com/halayli/lthread/blob/dev/src/lthread_io.c
how does the stack copying technique (as used in lthread) compare to segmented stacks (as used in go)? The swap technique is very simple but I would assume that the stack copy shows up in profiles and is significantly slower than the segmented stack technique... Are there any benchmarks?
Lthread is in C, an environment where you control/manage your memory. Using slab allocators and high performance malloc like jemalloc, you can avoid allocating a lot of your variables on the stack and the stack copy will become minimal. In most of the production code I have running using lthread, stack copying is on the average of 300 bytes.
Luvit is essentially a lua interpreter with bindings to libuv (the same core IO library used in NodeJS). And could mainly be considered an alternative to NodeJS, which it most closely resembles.
Some, who don't like JS syntax may wish to consider Luvit, as it will perform close to, and sometimes better than NodeJS, with some similar gotchas.
I wouldn't say it has more structure than Python or JavaScript. Lua has only these types: number, string, boolean, table, function, nil, userdata and thread. Table acts as a hybrid between list and map, just like Array/Object in JavaScript. And users rarely are exposed directly to userdata/thread, that's more something you'd use when you write a library (use userdata to emulate classes etc). The Lua VM is also much simpler than the other two, it's a simple stack machine. There are no tuples like in python, no first-class support for classes.
Lua has true multiple return value support. The overall usefulness of tuples in light of this feature is not clear.
Lua has first class support for functions and actually supports functional programming unlike Python which cripples many techniques and where Javascript has broken lexical scoping. Lua uses metamethod programming to allow you to build functionality like classes; Javascript has prototype inheritance which is different than Python's classical inheritance. Each is just different; not necessarily more or less structured.
Also, never confuse simple for being primitive. Lua IS incredibly simple and elegant. But all the simple things in the language work together elegantly to allow for amazingly sophisticated things.
EDIT: Also, I just remembered that Lua started as a stack-based VM, but moved to register-based at around version 5. So you could be thinking of the old Lua, too.
So far, I mostly like it. Recently, I used it to build a PoC for an in-house testing tool for embedded devices and decided to slap a web interface onto it for fun (just like Twisted provokes you to add an NNTP and an SSH interface just for fun).
What I'm a bit unhappy with is the decision to be incompatible with the rest of the Lua world. From what I read in the mailing list, it's not just "sorry, that rock uses blocking file IO" but rather "Lua is Lua, and luvit is luvit".
[1] http://tarantool.org/dist/master/tarantool_user_guide.html#s...
Lthread is a cool experiment but coroutines in C is a dubious ideal. If you're writing a program in C/C++, presumably you want ultimate control and the fastest possible execution. You wouldn't introduce extra runtime complexity and a speed hit for easier to use APIs (blocking I/O functions). You'd just use epoll and non-blocking sockets (or libuv). That's not to say that coroutines/green threads/goroutines in high-level languages where you've already decided to take a speed hit for easier use isn't useful. Said another way: if your concern is about easy to read code, C is not the right choice; if your concern is speed: copying a stack on every i/o function is not the right choice.
Segmented stacks aren't possible in C, because you don't have control over the stack's growth and the MMU when a function is running.
Coroutines simplifies your code a lot compared to callbacks(via libuv, epoll) and with lthread, you barely have any performance hit if you don't have sizable variables on the stack. I've written web servers and proxies in lthread that performs as fast as nginx (and sometimes faster). I think that's a pretty good deal.
> You wouldn't introduce extra runtime complexity and a speed hit for easier to use APIs (blocking I/O functions).
Your program is running in a kernel. You already have lost a lot of performance, and the complexity got introduced. Kernel does a gazillion thing while running your program, so if you really want raw performance, make sure to run your code on a bare metal CPU without kernel intervention. But that's not convenient, hence you compromise, pay a penalty, and run your program in a kernel. But does that mean you might as well use a high level language because you compromised on performance? no.
> if your concern is about easy to read code, C is not the right choice
I disagree. They aren't mutually exclusive.
> if your concern is speed: copying a stack on every i/o function is not the right choice.
lthread doesn't copy a stack on every IO function call. The lthread stack is copied only after the socket blocks or it reached its fair share. Sometimes this means an lthread can do 2 to 5 calls before it yields. And when it yields, depending on how deep you are in your call, the stack gets copied from there. If you don't have big variables on your stack, the stack copy can be just few bytes. Note that it's not the whole scheduler stack that's copied (4MB by default), but only what was consumed by lthread (usually from few bytes to 300 bytes, it varies based on the code).
You are over-simplifying your choices to this: if you want performance, use C & callbacks otherwise use a high level language. There's a wide spectrum that you are missing out in this over simplification.