Lua Academy · Lesson

Profiling with jit.p and perf

Profile Lua code with jit.p, annotate hot paths, and reduce allocations.

Lesson 3 of 413 steps

Profiling with jit.p and perf is a free Lua Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

Why Profile?

Profiling identifies which functions consume the most CPU time. Without profiling, optimization efforts are often misdirected to code that is not the bottleneck.

jit.p: LuaJIT Built-in Profiler

LuaJIT includes jit.p, a sampling profiler. Enable it from the command line or inside the script.

-- Command line:
luajit -jp=s script.lua
-- Or inside script:
require("jit.p").start("s")  -- sample at function level
-- ... code ...
require("jit.p").stop()

jit.p Modes

Modes: "s" sample by function, "l" sample by line, "i" show interpreter vs JIT. Output to file: "-jp=s,output.txt".

Reading jit.p Output

Output shows a percentage breakdown of where time was spent. Focus on the top few hotspots.

-- Example output:
-- 40%  mymodule.hotLoop
-- 25%  table.sort
-- 15%  string.format
-- ...

jit.dump for Trace Details

require("jit.dump").on() outputs detailed IR and machine code for each compiled trace. Use to understand what the JIT actually compiled.

Using Linux perf

On Linux, perf record -g luajit script.lua captures call stacks at the OS level. Combine with perf report for a flame graph.

LuaJIT + perf Map Files

LuaJIT can generate perf symbol map files so that perf reports show Lua function names instead of raw addresses: luajit -jp=p script.lua.

os.clock for Micro-Benchmarks

For quick benchmarks measure wall time with os.clock(). Repeat the operation many times to average out measurement noise.

local function bench(fn, iters)
  local t0 = os.clock()
  for _ = 1, iters do fn() end
  return (os.clock() - t0) / iters
end

Isolate Hotspots

After profiling, extract the hot function into a minimal standalone benchmark. This makes it easy to iterate on optimizations and verify improvements.

GC Impact on Profiling

GC pauses show up as spikes in profiling. Measure with GC disabled (collectgarbage("stop")) to separate algorithm time from GC time.

Profiling Best Practices

  • Profile with realistic data, not micro-benchmarks.
  • Profile release builds, not debug builds.
  • Measure before and after every change.
  • Don't optimize code that takes <5% of total time.

Profiling Question

What does jit.p mode "s" measure?

Recap: Profiling with jit.p and perf

Use jit.p for Lua-level function/line profiling, jit.dump for JIT trace inspection, and Linux perf for OS-level flame graphs. Measure, optimize, measure again.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Profiling with jit.p and perf” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “Profiling with jit.p and perf”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

What will I learn in “Profiling with jit.p and perf”?

Profile Lua code with jit.p, annotate hot paths, and reduce allocations. 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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Profiling with jit.p and perf” 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