LuaJIT: History
Please note this is an old version of this entry, which may differ significantly from the current revision.
Contributor:

LuaJIT is a tracing just in time compiler for the Lua programming language.

  • luajit
  • lua
  • just in time

1. History

The LuaJIT project was started in 2005 by developer Mike Pall, released under the MIT open source license.[1]

The second major release of the compiler, 2.0.0, bolstered major performance increases[2]

The latest release, 2.0.5 is released in 2017. Since then, the project is not currently maintained by developers other than contributors.[3]

2. Notable Users

  • CERN, for their Methodical Accelerator Design 'next-generation' software for describing and simulating particle accelerators[4]
  • OpenResty, a fork of nginx with Lua scripting[5]
  • Kong, a web API gateway[6]
  • Cloudflare, who use LuaJIT in their web application firewall service[7]

3. Performance

LuaJIT is often the fastest Lua runtime.[8] LuaJIT has also been named the fastest implementation of a dynamic programming language.[9][10] LuaJIT is sometimes hailed as competitive to the performance of C++.[11][12]

LuaJIT includes a Foreign Function Interface compatible with C data structures. It's use is encouraged for numerical computation.[13]

3.1. Tracing

LuaJIT is a tracing just-in-time compiler. LuaJIT chooses loops and function calls as trace anchors to begin recording possible hot paths. Function calls will require twice as many invocations to begin recording as a loop. Once LuaJIT begins recording, all control flow, including jumps and calls, are inlined to form a linear trace. All executed bytecode instructions are stored and incrementally converted into LuaJIT's Static single-assignment Intermediate representation. LuaJIT's trace compiler is often capable of inlining and removing dispatches from object orientation, operators, and type modifications.[14]

3.2. Internal Representation

LuaJIT uses two types of internal representation. A stack-based bytecode is used for the Interpreter (computing), and a static-single assignment form is used for the just-in-time compiler. The interpreter bytecode is frequently patched by the JIT compiler, often to begin executing a compiled trace or to mark a segment of bytecode for causing too many trace aborts.[10]

-- Loop with if-statement local x = 0 for i=1,1e4 do x = x + 11 if i%10 == 0 then -- if-statement x = x + 22 end x = x + 33 end
---- TRACE 1 start Ex.lua:5 ---- TRACE 1 IR 0001 int SLOAD #2 CI 0002 > num SLOAD #1 T 0003 num ADD 0002 +11 0004 int MOD 0001 +10 0005 > int NE 0004 +0 0006 + num ADD 0003 +33 0007 + int ADD 0001 +1 0008 > int LE 0007 +10000 0009 ------ LOOP ------------ 0010 num ADD 0006 +11 0011 int MOD 0007 +10 0012 > int NE 0011 +0 0013 + num ADD 0010 +33 0014 + int ADD 0007 +1 0015 > int LE 0014 +10000 0016 int PHI 0007 0014 0017 num PHI 0006 0013 ---- TRACE 1 stop -> loop ---- TRACE 2 start 1/4 Ex.lua:8 ---- TRACE 2 IR 0001 num SLOAD #1 PI 0002 int SLOAD #2 PI 0003 num ADD 0001 +22 0004 num ADD 0003 +33 0005 int ADD 0002 +1 0006 > int LE 0005 +10000 0007 num CONV 0005 num.int ---- TRACE 2 stop -> 1

4. Extensions

LuaJIT adds several extensions to its base implementation, Lua 5.1, most of which do not break compatibility.[15]

  • "BitOp" for binary operations on unsigned 32-bit integers (these operations are also compiled by the just-in-time compiler)[16]
  • "CoCo", which allows the VM to be fully resumable across all contexts[17]
  • A foreign function interface[18]
  • Portable bytecode (regardless of architecture, word size, or endianess, not version)[19]

5. DynASM

DynASM is a lightweight preprocessor for C which was created for LuaJIT 1.0.0 to make developing the just-in-time compiler easier. DynASM replaces assembly code in C files with runtime writes to a 'code buffer', such that a developer may generate and then evoke code at runtime from a C program.

DynASM was phased out in LuaJIT 2.0.0 after a complete rewrite of the assembler, but remains in use by the LuaJIT contributors as a better assembly syntax for the LuaJIT interpreter.

DynASM includes a bare-bones C header file which is used at compile time for logic the preprocessor generates. The actual preprocessor is written in Lua.

5.1. Example

|.type L, lua_State, esi // L. |.type BASE, TValue, ebx // L->base. |.type TOP, TValue, edi // L->top. |.type CI, CallInfo, ecx // L->ci. |.type LCL, LClosure, eax // L->ci->func->value. |.type UPVAL, UpVal |.macro copyslot, D, S, R1, R2, R3 | mov R1, S.value; mov R2, S.value.na[1]; mov R3, S.tt | mov D.value, R1; mov D.value.na[1], R2; mov D.tt, R3 |.endmacro |.macro copyslot, D, S; copyslot D, S, ecx, edx, eax; .endmacro |.macro getLCL, reg ||if (!J->pt->is_vararg) { | mov LCL:reg, BASE[-1].value ||} else { | mov CI, L->ci | mov TOP, CI->func | mov LCL:reg, TOP->value ||} |.endmacro |.macro getLCL; getLCL eax; .endmacro [...] static void jit_op_getupval(jit_State *J, int dest, int uvidx) { | getLCL | mov UPVAL:ecx, LCL->upvals[uvidx] | mov TOP, UPVAL:ecx->v | copyslot BASE[dest], TOP[0] }

The content is sourced from: https://handwiki.org/wiki/Software:LuaJIT

References

  1. https://luajit.org
  2. Pall, Mike. "Re: [ANN llvm-lua 1.0"]. http://lua-users.org/lists/lua-l/2009-06/msg00071.html. 
  3. "Download". https://luajit.org/download.html. 
  4. Deniau, Laurent. "Lua(Jit) for computing accelerator beam physics". CERN. https://cds.cern.ch/record/2157242. 
  5. "OpenResty® - Official Site". https://openresty.org/en/. 
  6. "Kong/kong". Kong. 25 February 2022. https://github.com/Kong/kong. 
  7. "Helping to make Luajit faster". 19 October 2017. https://blog.cloudflare.com/helping-to-make-luajit-faster/. 
  8. "LuaJIT Performance". https://staff.fnwi.uva.nl/h.vandermeer/docs/lua/luajit/luajit_performance.html. 
  9. "Laurence Tratt: The Impact of Meta-Tracing on VM Design and Implementation". https://tratt.net/laurie/research/pubs/html/bolz_tratt__the_impact_of_metatracing_on_vm_design_and_implementation/. 
  10. d'Andrea, Laurent (2019). Behavioural Analysis of Tracing JIT Compiler Embedded in the Methodical Accelerator Design Software (Thesis). CERN. Retrieved 31 July 2022. https://cds.cern.ch/record/2692915
  11. Real, Lucas C. Villa; de Bayser, Maximilien (23 September 2021). User-Defined Functions for HDF5. https://arxiv.org/abs/2109.11709. 
  12. Hakim, Ammar; Juno, James (November 2020). "Alias-Free, Matrix-Free, and Quadrature-Free Discontinuous Galerkin Algorithms for (Plasma) Kinetic Equations". SC20: International Conference for High Performance Computing, Networking, Storage and Analysis: 1–15. doi:10.1109/SC41405.2020.00077. ISBN 978-1-7281-9998-6.  https://dx.doi.org/10.1109%2FSC41405.2020.00077
  13. Pall, Mike. "Tuning numerical computations for LuaJIT (was Re: [ANN Sci-1.0-beta1) - luajit - FreeLists"] (in en). https://www.freelists.org/post/luajit/Tuning-numerical-computations-for-LuaJIT-was-Re-ANN-Sci10beta1. 
  14. Rottenkolber, Max. "Later Binding: Just-in-Time Compilation of a Younger Dynamic Programming Language." ELS. 2020
  15. "Extensions". https://luajit.org/extensions.html. 
  16. "BitOp Semantics". https://bitop.luajit.org/semantics.html. 
  17. "Coco - True C Coroutines". https://coco.luajit.org/. 
  18. "FFI Library". https://luajit.org/ext_ffi.html. 
  19. "Extensions". https://luajit.org/extensions.html. 
More
This entry is offline, you can click here to edit this entry!
Video Production Service