0Pricing
Lua Academy · Lesson

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

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