luaJIT

{{Short description|Just-in-time compiler for the Lua programming language}}

{{Infobox software

| name = LuaJIT

| author = Mike Pall

| latest release version = v2.1.ROLLING[https://github.com/LuaJIT/LuaJIT/tags LuaJIT tags]

| latest release date = {{start date and age|2023|08|21}}

| programming language = C, Lua

| operating system = Unix-like, MacOS, Windows, iOS, Android, PlayStation

| platform = x86, X86-64, PowerPC, ARM, MIPS{{cite web|title=LuaJIT |url=https://luajit.org/luajit.html |website=LuaJIT |access-date=25 February 2022}}

| genre = Just-in-time compiler

| license = MIT License{{cite web|url=https://github.com/LuaJIT/LuaJIT/blob/v2.1/COPYRIGHT|title = LuaJIT/COPYRIGHT at v2.1 · LuaJIT/LuaJIT|website = GitHub|date = 7 January 2022}}

| website = {{URL|https://luajit.org}}

| repo = {{URL|https://github.com/LuaJIT/LuaJIT}}

| screenshot = LuaJIT REPL on Windows.png

| screenshot alt = "LuaJIT REPL running on Windows 10

| logo = LuaJIT Orange on Blue Logo.png

| logo alt = The logo used for the LuaJIT compiler project

| logo caption = The logo featured on the LuaJIT website.

}}

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

History

The LuaJIT project was started in 2005 by developer Mike Pall, released under the MIT open source license.{{Cite web |title=The LuaJIT Project |url=https://luajit.org/ |access-date=2023-06-17 |website=luajit.org}}

The second major release of the compiler, 2.0.0, featured major performance increases.{{cite web |last1=Pall |first1=Mike |title=Re: [ANN] llvm-lua 1.0 |url=http://lua-users.org/lists/lua-l/2009-06/msg00071.html |website=lua-users.org |access-date=25 February 2022}}

LuaJIT uses rolling releases. Mike Pall, the creator and maintainer recommends using the tip of the v2.1 branch, and does not believe in releases.{{cite web |title=Project status - Issue #665 - LuaJIT/LuaJIT |website=GitHub |url=https://github.com/LuaJIT/LuaJIT/issues/665 |access-date=3 February 2023}}

Mike Pall resigned in 2015 making only occasional patching to the future 2.1 version since then.{{Cite web |title=[ANN] Looking for new LuaJIT maintainers - luajit - FreeLists |url=https://www.freelists.org/post/luajit/Looking-for-new-LuaJIT-maintainers |access-date=2023-03-29 |website=www.freelists.org |language=en}}

Notable users

  • CERN, for their Methodical Accelerator Design 'next-generation' software for describing and simulating particle accelerators{{cite web |last1=Deniau |first1=Laurent |title=Lua(Jit) for computing accelerator beam physics |url=https://cds.cern.ch/record/2157242 |website=CERN Document Server |publisher=CERN |access-date=25 February 2022}}
  • OpenResty, a fork of nginx with Lua scripting{{cite web |title=OpenResty® - Official Site |url=https://openresty.org/en/ |website=openresty.org}}
  • Neovim, a text editor based on vim that allows the use of Lua for plugins and configuration{{Cite web |title=Lua - Neovim docs |url=https://neovim.io/doc/user/lua.html#lua-luajit |access-date=2024-05-07 |website=neovim.io}}
  • Kong, a web API gateway{{cite web |title=Kong/kong |url=https://github.com/Kong/kong |website=GitHub |publisher=Kong |access-date=25 February 2022 |date=25 February 2022}}
  • Cloudflare, who use LuaJIT in their web application firewall service{{cite web |title=Helping to make Luajit faster |url=https://blog.cloudflare.com/helping-to-make-luajit-faster/ |website=blog.cloudflare.com |date=19 October 2017 |access-date=25 February 2022}}

Performance

LuaJIT is often the fastest Lua runtime.{{cite web|url=https://staff.fnwi.uva.nl/h.vandermeer/docs/lua/luajit/luajit_performance.html|title=LuaJIT Performance}} LuaJIT has also been named the fastest implementation of a dynamic programming language.{{cite web |title=Laurence Tratt: The Impact of Meta-Tracing on VM Design and Implementation |url=https://tratt.net/laurie/research/pubs/html/bolz_tratt__the_impact_of_metatracing_on_vm_design_and_implementation/ |website=tratt.net |access-date=2 March 2022}}{{cite thesis |last= d'Andrea |first= Laurent |date= 2019 |title= Behavioural Analysis of Tracing JIT Compiler Embedded in the Methodical Accelerator Design Software |publisher= CERN |url= https://cds.cern.ch/record/2692915 |access-date= 31 July 2022}}

LuaJIT includes a Foreign Function Interface compatible with C data structures. Its use is encouraged for numerical computation.{{cite web |last1=Pall |first1=Mike |title=Tuning numerical computations for LuaJIT (was Re: [ANN] Sci-1.0-beta1) - luajit - FreeLists |url=https://www.freelists.org/post/luajit/Tuning-numerical-computations-for-LuaJIT-was-Re-ANN-Sci10beta1 |website=www.freelists.org |language=en}}

= 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.Rottenkolber, Max. "Later Binding: Just-in-Time Compilation of a Younger Dynamic Programming Language." ELS. 2020

= Internal representation =

LuaJIT uses two types of internal representation. A stack-based bytecode is used for the interpreter, 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.

-- 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

Extensions

LuaJIT adds several extensions to its base implementation, Lua 5.1, most of which do not break compatibility.{{cite web |title=Extensions |url=https://luajit.org/extensions.html |website=LuaJIT |access-date=25 February 2022}}

  • "BitOp" for binary operations on unsigned 32-bit integers (these operations are also compiled by the just-in-time compiler){{cite web |title=BitOp Semantics |url=https://bitop.luajit.org/semantics.html |website=LuaJIT |access-date=25 February 2022}}
  • "CoCo", which allows the VM to be fully resumable across all contexts{{cite web |title=Coco - True C Coroutines |url=https://coco.luajit.org/ |website=LuaJIT |access-date=25 February 2022}}
  • A foreign function interface{{cite web |title=FFI Library |url=https://luajit.org/ext_ffi.html |website=LuaJIT |access-date=25 February 2022}}
  • Portable bytecode (regardless of architecture, word size, or endianness, not version){{Cite web |title=Extensions |url=https://luajit.org/extensions.html |access-date=2022-08-25 |website=luajit.org}}

DynASM

{{Infobox software

| name = DynASM

| developer = Mike Pall

| programming language = Lua, C{{cite web |title=DynASM Features |url=https://luajit.org/dynasm_features.html |website=DynASM |access-date=25 February 2022}}

| platform = x86, X86-64, PowerPC, ARM, MIPS

| genre = Preprocessor, Linker

| license = MIT License

| website = {{URL|https://luajit.org/dynasm.html}}

}}

DynASM is a lightweight preprocessor for C that provides its own flavor of inline assembler, independent of the C compiler. 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. It was created for LuaJIT 1.0.0 to make developing the just-in-time compiler easier.{{citation needed|date=February 2022}}

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.

References