My love-hate relationship with LuaJIT (2015)
blog.separateconcerns.com
blog.separateconcerns.com
My experience with LuaJIT has similarly been love, not much hate though. My lack of hate stems from the fact that I don't actually use it in production. I've used it as a learning and teaching tool for the past 4 or 5 years.
What I've learned: Co-routines combined with traditional async IO can make for some very fast IO bound programs.
JIT based FFI for interop is the cats meow
The guts of operating systems can be easily explained using such a simple language.
I went as far as creating a collaborative 3D modeling program (ala OpenSCAD) using LuaJIT.
I've created an entire Windows API wrapper (TINN).
I've explored the guts of Linux (using ljsyscall), and done exploration of various bits and pieces (lj2procfs).
I've been able to explore and play with any manner of API from dozens of OpenSource projects and otherwise, simply because LuaJIT (and specifically the FFI) make it possible to quickly wrap them up in such a way that exploration with script is easy.
I never worry about the death of LuaJIT, just like I very rarely look at the guts of my machine that's been running since 2009. The various parts may no longer be supported, but somehow my machines manages to continue to operate. Eventually I'll build another one and move on.
With recent changes to TypeScript, and the emergence of the Chakra engine, I might be tempted to make JavaScript my new thing, but they lack FFI, which I really love. I wish there were a solution for that.
Go has co/goroutines, which I really like, but I don't think their C interop story is particularly compelling, and not quite a scripty thing, which I find to be fairly useful.
I was originally driven to LuaJIT because PUC Lua had a flaw in their OpenGL binding, and it just wasn't robust enough for my needs. I find no value in their current versions, because FFI seems to be the most valuable thing to me I guess.
So, since it's a play thing for me, I find no other language as compelling or useful, for my needs. Some day, I'll use one of the language tool kits to convert my most interesting Lua code to another language, hopefully one with a good FFI. Until then, I'll keep using it.
And, if you've been around the industry long enough, you realize one language won't do it all for all time. I've long since given up on Objective-C (from NeXT days), I no longer write 6502 assembly (although still very popular apparently). I dove into C++ in the past couple of years, and came out with hives! Rust and Go are on the horizon, and who knows what's after that.
The fork between PUC Lua and LuaJIT mentioned in the article is a major point of concern. For good and bad, PUC Lua is not known for backward compatibility. PUC Lua is at 5.3 and LuaJIT targets 5.1. There have been some breaking changes since then.
Without a strong community built around LuaJIT, or a corporate backer investing resources in it, I fear that it will die out eventually. I would be hesitant to implement something that uses LuaJIT with the project looking the way it does to me. I would love to be proven wrong though.
[1]: https://news.ycombinator.com/item?id=9981802 [2]: https://news.ycombinator.com/item?id=10078848
If there is someone reading this who wants to work on LuaJIT full time and thinks they have relevant background then please contact me.
My uninformed outsider's impression is that Mike Pall wasn't really widely successful at getting funding for the work he wanted to do on Luajit (e.g. new GC) – despite being probably the world's foremost JIT for dynamic languages expert[1] and the mileage that many extremely wealthy companies seem to have gotten out of luajit (e.g. I understand it plays a pretty important role in your firewall logic, and probably not one that would be easily replaced with some other technology).
I don't mean to single Cloudflare out for criticism, I'm aware that you have in fact funded Mike's work in the past. And maybe there is absolutely no relationship between Mike moving on and funding for luajit.
But since you replied here, I'd be genuinely interested to hear your perspective on this:
To me it appears that apparently even single-developer open source projects which directly affect the bottomline of large companies can struggle to make enough money to continue their work. Why is this?
[1] Seeing how he essentially single handedly wrote a JIT that seems superior in many respects to what the likes of Google have managed to achieve with Javascript after many lifetimes' worth of top developer effort.
I'll pass him your offer.
I also worked on a JS2Lua transpiler, which is called exactly the same btw, for a university assignment (https://github.com/Etiene/js2lua) , but it's trash and I never really finished it until it incorporated all native JS functions and was actually usable. I have so many ideas that could use this. This is awesome. Thank you so much!
From the outside the message I am receiving is "hurry up and wait for a new maintainer to emerge from closed-door discussions between Mike, CloudFlare, and unspecified other parties." However, this has been the situation for over a year now, and presumably there have already been fruitless discussions with all of the obvious candidates. (Reading between the lines I suspect that the job description was "Mike Pall 2.0" and the qualified candidates were simply too expensive, but that is speculation.)
So what next for LuaJIT users? Keep waiting? Create a fork? Whine on Hacker News?
Personally I don't think we need a "Mike Pall 2.0." What we need is mere-mortal active maintainers who can review, merge, and maintain extensions developed by the community. This work could conceivably be spread between many hands -- if some of those hands would have a commit bit. I think it unfortunate that excellent work like Thomas Fransham's intrinsics implementation are stuck in limbo because there is nobody available to review and commit them.
End rant. I say this as a lover of LuaJIT and somebody who is using it for the long haul and thus very interested in its ongoing development.
Source: https://github.com/LuaJIT/LuaJIT/issues/45#issuecomment-2340...
I am sure that project seemed like a corpse after Rob McLauchlan moved on in the 90s leaving behind a compiler much more complex than LuaJIT. However, dozens of open source heroes have picked up that project and moved it forward. Kudos to them :). Could be that we in the LuaJIT user community need to take a leaf from their book.
LuaJIT source is terribly, terribly, terribly-written C code with minimal (and opaque) documentation that doesn't really facilitate human understanding. In that regard, LuaJIT __completely fails__ one of the maxims from SICP (code should be written primarily for humans to understand).
I therefore propose that this be one of the first issues addressed. The LuaJIT codebase needs to be refactored and extensively documented in such a manner as to facilitate rapid understanding by 3rd parties.
The situation is fantastically luxurious compared with CMCUL where the new maintainers needed to acquire a dusty old DEC Alpha to run a non-reproducible binary image with which to write a cross-compiler to x86...
I am being downvoted because people can't look past personal likes/dislikes and are conditioned to look uncritically at software that they like/depend on. Yet the obvious point I'm making here has nothing to do with the quality of LuaJIT as a tool that can solve problems. There are lots of examples of powerful, high-quality software that sits on an atrocious, impossible-to-understand codebase (e.g. pretty much anything by fabrice bellard, which ironically enough, some even consider as _good_ C) and while LuaJIT may not be at that level, it's not currently _good enough_.
I think we need to move past personality-worship and start striving for quality where it matters. Read this thread and you'll find numerous posts referring to Mike Pall as an alien/god-of-coding and so on. This is not at all helpful.
A software project that is positioned as something with staying power and a base for others to build on should be judged more on its ability to attract and maintain a community around it rather than its creator/profit that it generates/other superficial attributes. It is for this reason that a well-documented codebase that facilitates understanding is of _paramount importance_.
LuaJIT is not a toy compiler from a textbook. There's a lot of inherent complexity in a production compiler that employs advanced optimizations and needs to work on various CPU architectures and operating systems. This reflects in the code.
Please don't go on about downvotes as a rhetorical device. That's a variant of the complaining that the site guidelines ask us all not to do: https://news.ycombinator.com/newsguidelines.html.
The Neovim patch[3] to add built-in lua scripting will support both luajit and PUC lua.
I also updated the FAQ[4] to avoid confusion.
1: https://github.com/neovim/lua-client
2: https://travis-ci.org/neovim/neovim/jobs/162545247
why is that?
For a text editor I don't see how ludicrous speed would help...
Especially when considering that luajit doesn't compile string operations well; which is what I imagine text editors do a lot of.
See the updated FAQ[1]. We are punting on the VimL-to-Lua translator. We think it will be more valuable to instead ship Lua as a first-class alternative to VimL.
> (suffering from code rot and filled with preprocessor declarations for dead platforms) in Lua w/o degrading performance too much
eval.c is being refactored in PR 5119[2].
1-based indexing is just a silly departure from decades of ingrained thinking, dictionaries masquerade as lists but can unintentionally become dictionaries if you mistakenly put a non-numeric key in them, and the only inheritance is prototype-based "classes" that are implemented via gross metatable hacking. Library support is also weak.
I will give it credit though for a robust coroutine implementation.
This on only true for (not too old) programmers.
- Older programmers, taught pre-C, may remember that most languages were typically 1-based (Fortran, Pascal, Algol, Smalltalk...).
- Scientists usually use 1-based notation. I have avoided countless off-by-one by implementing papers in Lua. Matlab is also 1-based, probably because it is a language for mathematicians and scientists.
- In general, non-programmer tend to count from 1 and not 0.
> the only inheritance is prototype-based "classes" that are implemented via gross metatable hacking
If you need to you can implement non-prototypal object models, with metatables or with closures if you don't like them. My recommended object library is https://github.com/kikito/middleclass for people who want to write ojbect-oriented code (I usually don't).
> dictionaries masquerade as lists but can unintentionally become dictionaries if you mistakenly put a non-numeric key in them
I agree with this one, mostly. I would appreciate a purely sequential data structure ("list").
I really wanted to like it for the small and simple implementation, but the language isn't pleasant IMO.
So true. However, you can disable this behavior and then just forget about it:
Still, stronger typing would help, and this is why several people are working on adding gradual typing to Lua. The main initiative is probably https://github.com/andremm/typedlua
It's a shame luajit never created a community beyond its creator. FFI sure was nice.
Not entirely sure how you got to that conclusion. Afaik there are no know bugs. All issues on https://github.com/LuaJIT/LuaJIT/issues are related to adding further optimizations or otherwise improve LuaJIT. In the last year bugs have been fixed almost instantly after they have been posted to the LuaJIT mailing list. So while I'm still a bit sad that Mike is on his way out, I wouldn't call LuaJIT unsupported. It's still a great choice.
That's good to hear. Perhaps LuaJIT isn't "unsupported", but it does seem to be "in maintenance mode".
I would love to see, not just bug fixes to LuaJIT, but ongoing improvements. Unfortunately with Mike having stepped down, and there being no successor, nobody is making these improvements and nobody is driving the project forward.
For example, will the new garbage collector [1] ever be implemented? Will LuaJIT remain stuck at version 5.1 while PUC Lua continues to add new features?
https://github.com/LuaJIT/LuaJIT/issues/203
This is not helpful.
I stand by my assessment that luajit is unsupported software. If it works for you - all the best.
LuaJIT is not magic, it's a software. Very neat and tightly written but it's still just a bunch of C/assembly/Lua code.
Written by one human, and thus other humans can understand it too.
And as interesting as it is to have a small scripting language be able to run code so quickly, there are new languages that deliver the same benefits. Nothing really stands out about LuaJIT to make me feel it is a language I should use.
Lua 5.1 itself is Scheme with regular syntax traded for macros, and one doesn't simply drop another language in for replacement.
LuaJIT comes with warnings like "Callbacks are slow" and "The following operations are currently not compiled and may exhibit suboptimal performance, especially when used in inner loops:" which pushes it further down the list for me.
Given luajit development seems to be stalled, I don't have much confidence this will ever be fixed.
[1] https://github.com/LuaJIT/LuaJIT/commit/a92e73023353e59405eb... [2] https://github.com/LuaJIT/LuaJIT/commit/2868715d80b6ac497a7f...
Well, how often people fix bugs in the compiler they use (and haven't written themselves)? Doesn't seem as much a "practical" consideration as the author says...
>Another issue is that PUC Lua and LuaJIT are diverging. LuaJIT implements Lua 5.1. Lua 5.2 code can be made to work as long as it does not use _ENV, but code that leverages the new features in Lua 5.3 is not supported at all
Well, not as much diverging as in "going to different directions" as that LuaJIT implements an older version. Isn't it supposed to eventually catch up?
Regarding your second point: Mike Pall had no intention to catch up completely. I don't know how much that has changed under CloudFlare leadership.
Yeah, the actual compiler could use a maintainer, but who looks at LuaJIT and thinks "clearly, the weak point is the compiler!"?
Edit: Also what's this about "a machine that does not support LuaJIT" that's apparently x86-based?
However, in that case you are only prevented from using the JIT compiler and should still be able to take advantage of the high-speed interpreter included.
The machine linked in the article can not even run the unmodified interpreter (or at least it could not at the time) because its CPU does not support the CMOV instruction.
Regarding the bugs: I do think most of them are in the JIT and not the interpreter. Even using a lower optimization level that the default fixes a lot of them.
...over here, in this cardboard box, I have an old SheevaPlug: a 1.2GHz Marvell ARMv5 in a plug-sized box with some flash, an SD card interface, ethernet, and some USB ports, which appears to be pretty much the same device. (Of course, it's the software that's the interesting bit.)
http://dl.acm.org/citation.cfm?id=2818305 http://ake.in.th/2016/01/09/til-there-is-a-tool-named-diffst...
Also can't understand all drama about low support. We have a rocket with two modes: turboreactive, which just works, and sub-lightspeed mode where, well, strange things may happen. All those complaints about debugging of relativity shadow the fact that all other dynamic languages are conventional single-mode subsonic aircrafts.
I also used Minix to learn about OSs back in 1991 by installing Minix 1.5 on my Amiga. Sad to say after spending an all-nighter getting it working, I didn't really dive in the way I had intended much after that. Tanenbaum's book was a real eye-opener for me. The source code and a textbook. I haven't seen much like it today. Still, I learned a lot about computers in general doing this, and would recreate the experience in my second dual-boot system, a Mac PowerPC running MkLinux in 1997!
There's something to be said for reading source code, and recompiling small OSs even if you just touch upon it. XV6 was re-written, and the source is < 10K LOC [1]. The course book is about 100 pages and the link also includes the source code in PDF to follow along.
I am also thinking on installing Minix3 on my dedicated linux notebook, but I am afraid I might botch it because my current system is UEFI/SB=off. I think you can only use BIOS/MBR with Minix3, although somebody has posted how to load it from a LiveUSB with Qemu, which you ditch after the install.
https://github.com/jmckaskill/luaffi
It isn't terribly fast though.
The case for doing the glue in Lua is the fact that script is arguably easier to prototype in. So, depends on your particular application space. C can be a lot more error prone, at least when it comes to memory management, but this is a general argument for garbage collected vs not.
It is significantly quicker to write code in a higher level language, since the primitives and libraries do more per line of code. The cost is that it doesn't run as fast. But consider these two scenarios:
1 It takes 2 hours to write the code, and 1 hour for it to run.
2 It takes 2 days to write the code, and 1 minute for it to run
Note that "write the code" includes the actual writing, as well as debugging, testing etc. The first scenario is the most immediately productive. But the more you have to run the code, the more the second scenario applies.
Which is why you combine the approaches. Write the parts that aren't performance sensitive quickly in your high level language, and take longer on the performance parts in C. That works better than exclusively approach 1 or 2.
I wrote the processing in Python first. The core was 10 lines of code and took about 5 minutes to run on a unit of data. It took about an hour to write, and was correct.
It then took two days to rewrite in C and was 400 lines of code, executing in a second or so. Much of the time was finding and fixing minor bugs. But having the "correct" Python code made it very easy to feed the same test data to both implementations and verify the output. Writing test code in C would have taken even longer than just the algorithm!
Though luajit has gotten even more optimisations since that was done.
That said, I rarely find my code's performance is the bottleneck, and don't benefit from luajit. The guys that do are doing scientific computing (e.g. torch) or high performance networking (e.g. snabb)
This went under the radar for a while even though we had tons of traffic. Turns out PUC Lua is pretty fast on its own.
That really depends what you are testing for. For string ops is not the fastest. For recursive Fibonacci (inefficient) LuaJIT is faster than D and Lua is faster than Perl.
LuaJIT is at least 10x faster than all three.
Ruby faster than PUC Lua? That doesn't sound right. You're talking about Ruby MRI?
Not the original MRI, but YARV. For this particular program, the performance of different language interpreters is:
MRI (Ruby 1.8) < CPython 3.5 < CPython 2.7, PUC Lua 5.1/5.3 < YARV (Ruby 1.9/2.3) < LuaJIT -joff
There is a factor of ~5x between the slowest and fastest of these interpreters. LuaJIT with the JIT enabled is ~10x faster than even its own interpreter.
Unfortunately, as a Pythonista (although one whose paid work is mostly in JS these days), I can't imagine that happening in the Python community (not to our credit). Too much ego wrapped up in CPython.
YARV? No.
YARV is the current official Ruby interpreter - it replaced MRI in this role from Ruby 1.9 onwards.
(http://www.atdot.net/yarv/rc2006_sasada_yarv_on_rails.pdf page 15)