Writing JIT-Friendly Lua
Avoid JIT bailouts: keep types stable, minimize table resizing, use locals.
Why JIT-Friendliness Matters
The JIT compiler works best on type-stable, predictable code. Unpredictable types and branching cause trace aborts and bailouts, reverting to the interpreter.
Keep Types Stable
A variable should always hold the same type. If a variable alternates between number and string, the JIT cannot specialize and must insert type checks everywhere.
-- BAD: type unstable
local x = 1
if cond then x = "one" end -- JIT cannot specialize x
-- GOOD: separate variables
local xNum = 1
local xStr = "one"All lessons in this course
- LuaJIT Architecture Overview
- Writing JIT-Friendly Lua
- Profiling with jit.p and perf
- Benchmarking Patterns