0Pricing
Lua Academy · Lesson

LuaJIT Architecture Overview

How LuaJIT's tracing JIT works vs the standard Lua VM interpreter.

LuaJIT Architecture Overview is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is LuaJIT?

LuaJIT is a Just-In-Time compiler for Lua 5.1. It is significantly faster than the standard Lua VM for numeric and loop-heavy code, while remaining 100% compatible.

Two Execution Modes

LuaJIT has two modes: the interpreter (a fast bytecode interpreter, faster than PUC Lua's) and the JIT compiler (traces hot loops to native machine code).

Tracing JIT

LuaJIT uses a tracing JIT: it records (traces) the execution of hot loops, then compiles the trace to optimized native code. It does not compile entire functions.

Hot Loop Detection

LuaJIT counts loop iterations. When a loop becomes hot (exceeds a threshold), the JIT records a trace and compiles it. Subsequent iterations run as native code.

JIT Compilation Pipeline

Trace → IR (intermediate representation) → optimizations (LICM, CSE, DCE) → register allocation → machine code (x86/x64, ARM, MIPS).

LuaJIT-Specific Features

LuaJIT adds the FFI library (call C functions directly), jit.* control API, bit library for bitwise ops (before Lua 5.3), and extended PCRE-ish patterns.

Performance Characteristics

LuaJIT is typically 2-10x faster than PUC Lua for JIT-compiled code, and 30-100x faster than Python for numeric loops. FFI calls have near-zero overhead.

JIT Bailout (Side Exit)

If a JIT assumption is violated (type changes, rare branches), the JIT bails out to the interpreter. Frequent bailouts reduce performance.

jit.on() and jit.off()

Enable/disable JIT globally or per-function. Useful to isolate whether JIT is helping or hurting a specific code path.

jit.off()        -- disable JIT globally
jit.on()         -- enable JIT globally
jit.off(myFunc)  -- disable JIT for one function

Compatibility Notes

LuaJIT is compatible with Lua 5.1. Some Lua 5.2/5.3 features (integers, goto, __gc on tables) are not supported. Check compatibility when migrating.

When to Use LuaJIT

Use LuaJIT when: performance is critical, you need FFI for C library access, or you are embedding Lua in a game engine (LÖVE, OpenResty, nginx) that ships LuaJIT.

LuaJIT Question

What is LuaJIT's tracing JIT compiling?

Recap: LuaJIT Architecture

LuaJIT is a tracing JIT that compiles hot loops to native code. It includes the FFI library for zero-overhead C calls. Understand bailouts and type stability to get the most from JIT compilation.

Frequently asked questions

Is the “LuaJIT Architecture Overview” lesson free?

Yes — the full text of “LuaJIT Architecture Overview” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “LuaJIT Architecture Overview”?

How LuaJIT's tracing JIT works vs the standard Lua VM interpreter. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “LuaJIT Architecture Overview” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. LuaJIT Architecture Overview
  2. Writing JIT-Friendly Lua
  3. Profiling with jit.p and perf
  4. Benchmarking Patterns
← Back to Lua Academy