11/11/2014– Tim Ryan
5 + 5 and
Because Lua is extremely extensible, allowing modification of everything from basic add operators to whether primitives act like objects, a year of effort has resulted in a runtime that emulates many real features of Node.js with only a fraction of the resources Node requires on PC.
Just in Time
LuaJIT is a combination of a few components: a parser/compiler converting Lua source into (LuaJIT’s own) bytecode, an interpreter for running this bytecode, and then a JIT (just-in-time compiler) for optimizing this bytecode into machine code at the lowest level. If you’re not familiar with a JIT, imagine a compiler that runs alongside your code, observing your code as it’s running. If it sees any loops that look intensive, are called often, and use similar types of variables on each iteration, it can bundle these assumptions up as compiler “guards” and generate low-level machine code that acts much more like C than a high level language. If, while your code is running, one of those guards fails (the variable
i is no longer a number, because we suddenly assigned it to be an array!) we jump backward into interpreting your code one line at a time. This is one of many optimizations that happen while your code is running inside a VM with just-in-time compilation.
LuaJIT on Thumb
As of today, you can update Tessel to a new build built on LuaJIT’s codebase instead of the classic Lua VM from PUC-Rio by running the usual
tessel update. This doesn’t enable the just-in-time compilation I described; we are working on porting this. For now, LuaJIT’s codebase and interpreter (the first step toward speeding up your code) are enabled and yield a pleasant 2-3x speed improvement. We’re working on porting the JIT component itself to leverage interpreter and speed up code execution by orders of magnitude, so stay tuned.
I’ll be writing more on the technical details of this LuaJIT port in my next few blog posts, so stay tuned.