# LuaJIT — High-Performance Just-In-Time Compiler for Lua > LuaJIT is a Just-In-Time compiler for the Lua programming language that achieves performance comparable to C for many workloads. It is widely used as an embedded scripting engine in games, web servers (OpenResty/Nginx), and networking tools. ## Install Save as a script file and run: # LuaJIT — High-Performance Just-In-Time Compiler for Lua ## Quick Use ```bash # Install on Ubuntu/Debian sudo apt install luajit # Run a Lua script luajit -e 'print("Hello from LuaJIT!")' # Use the FFI to call C functions directly luajit -e ' local ffi = require("ffi") ffi.cdef[[ int printf(const char *fmt, ...); ]] ffi.C.printf("Called from C: %d\n", 42) ' # Build from source git clone https://github.com/LuaJIT/LuaJIT.git cd LuaJIT && make && sudo make install ``` ## Introduction LuaJIT was created by Mike Pall as a drop-in replacement for the standard Lua 5.1 interpreter. Its tracing JIT compiler generates optimized machine code at runtime, making Lua scripts run 10-50x faster than the reference interpreter. The built-in FFI (Foreign Function Interface) allows calling C functions and using C data structures directly from Lua without writing any C wrapper code. ## What LuaJIT Does - Compiles Lua bytecode to optimized native machine code at runtime via tracing JIT - Provides a built-in FFI that calls C libraries directly without bindings or wrappers - Maintains full compatibility with Lua 5.1 and most Lua 5.2 features - Achieves performance within 2-5x of optimized C for numerical and data-processing workloads - Runs on x86, x86-64, ARM, ARM64, PPC, and MIPS architectures ## Architecture Overview LuaJIT uses a trace-based JIT compilation strategy. The interpreter profiles running code and identifies hot loops. When a loop exceeds a threshold, the tracer records a linear sequence of operations (a trace), applies optimizations (constant folding, dead code elimination, alias analysis), and emits machine code via a custom assembler. The FFI bypasses the Lua C API entirely, mapping C types to Lua values with near-zero overhead using the same JIT infrastructure. ## Self-Hosting & Configuration - Install via system package manager or build from source with `make` - Drop-in replacement: rename or symlink `luajit` to `lua` for existing Lua 5.1 scripts - Control JIT behavior with `jit.opt` module: `jit.opt.start("hotloop=10", "maxmcode=4096")` - Disable JIT for debugging: `luajit -joff script.lua` or `jit.off()` in code - Embed in C/C++ applications using the standard Lua C API ## Key Features - FFI library calls C functions with no bridging code and near-native performance - Trace compiler generates specialized machine code for hot loops and function calls - Full Lua 5.1 compatibility ensures existing scripts and libraries work unchanged - Lightweight coroutines with fast context switching for concurrent programming patterns - Built-in profiler and trace dump for understanding JIT compilation decisions ## Comparison with Similar Tools - **PUC Lua (5.4)** — Standard Lua is simpler and more portable; LuaJIT is 10-50x faster but tracks Lua 5.1 semantics - **V8 (JavaScript)** — V8 is a method-based JIT with larger memory footprint; LuaJIT's tracing JIT is lighter - **Python (CPython)** — CPython is interpreted; LuaJIT's FFI and JIT make it orders of magnitude faster for compute - **Ravi** — Ravi adds optional type annotations to Lua for JIT; LuaJIT JITs standard untyped Lua - **MoonJIT** — MoonJIT is a LuaJIT fork with extra features; LuaJIT remains the most tested and widely deployed ## FAQ **Q: Is LuaJIT compatible with Lua 5.4?** A: LuaJIT targets Lua 5.1 with selected 5.2 extensions. Code using Lua 5.3/5.4 features (integers, bitwise operators) needs minor adjustments. **Q: What is the FFI and when should I use it?** A: The FFI lets you call C functions and access C structs directly from Lua. Use it instead of the Lua C API for better performance and simpler code. **Q: Where is LuaJIT used in production?** A: OpenResty (Nginx + Lua), Kong API Gateway, Neovim, Redis (scripting), Cloudflare, and many game engines embed LuaJIT. **Q: Can LuaJIT be embedded in my application?** A: Yes. LuaJIT uses the same C API as Lua 5.1 and can be linked as a static or shared library in any C/C++ application. ## Sources - https://github.com/LuaJIT/LuaJIT - https://luajit.org - https://luajit.org/ext_ffi.html --- Source: https://tokrepo.com/en/workflows/asset-ee4096b0 Author: Script Depot