0Pricing
Lua Academy · Lesson

Profiling with jit.p and perf

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

Profiling with jit.p and perf is a free Lua Academy lesson on CoddyKit — lesson 3 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.

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.

Frequently asked questions

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

Yes — the full text of “Profiling with jit.p and perf” 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 “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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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