Skip to content

Calls and Functions

A function call in compiled code goes through a few steps. It checks that the value can be called, checks that there's room on the stack, finds the compiled code behind it, calls it, and moves the results into place. The optimizations on this page skip the steps that can't matter.

Compiled Functions Call Each Other Directly

When compiled code calls a function that was compiled into the same module, it calls its machine code directly, without going through the host's Lua. This is always done, for every call. It also means that compiled code isn't held to the interpreter's limit of 200 nested C calls.

Known Functions

A local function that's never assigned again always holds the same function. Calls through it skip the checks altogether and jump straight to its code.

local function format_size(bytes)
    if bytes < 1024 then return bytes .. " B"; end
    return string.format("%.1f KB", bytes / 1024);
end

for i, file in ipairs(files) do
    ListBox.AddItem("Files", file.name .. " (" .. format_size(file.size) .. ")", i);
end

The same goes for local f = function() end, and for nested functions that call it as an upvalue.

A known function also skips checking the stack when its caller already did. The caller checks for its own room and the callee's at once, so a chain of small helper functions called from a loop costs one check instead of one per call. Recursion keeps a check per call, so it ends with the interpreter's stack overflow just as before.

Self Tail Calls Are Loops

A local function that ends with a call to itself, return f(a, b), becomes a loop. It doesn't grow the stack, just like in the interpreter, and it's as fast as a while loop.

local function gcd(a, b)
    if b == 0 then return a; end
    return gcd(b, a % b);    -- a jump back to the start, not a call
end

Other tail calls stay proper tail calls as well, so a chain of them never runs out of stack.

A tail call of a library function, such as return setmetatable(t, mt) or return tostring(x), calls the function, as the interpreter does too.

Cheap Upvalues

A local that a nested function uses (an upvalue) normally needs a small box, so that every function using it sees the same variable. A local that's never assigned after its declaration can't change, so it doesn't need a box, and each function holds its value.

local prefix = "Log: ";              -- never assigned again: no box
local function log(text)
    Debug.Print(prefix .. text);
end

Upvalues are read once when the function starts, not on every use.

Globals

Each function finds its globals table once when it starts, so a global lookup is a single field lookup. In Lua 5.3, this needs your code to leave _ENV alone, which nearly all code does.

The lookup of the global itself still happens every time, because another script may assign it at any moment. When you know it won't, --#cache removes that lookup too.

Results

The common case, a call that wants one result from a function that returns one, moves that result in a single step. A call used as a condition (if IsValid(x) then) or stored in a local (local n = Count()) takes its result straight from where the call left it.

Technical details
  • A compiled function is a C closure of the host. Calls from Lua (the host, interpreted code, pcall, table.sort's comparator, metamethods) go straight into the compiled code: each function's entry knows which function it is.
  • Before every call, compiled code checks the C stack. When it runs low, the call raises the interpreter's own stack overflow error, so deep recursion never crashes the host. See Limitations.
  • A call to the host's error is recognised, so the message gets the line of the call as the interpreter gives it.
  • A compiled function needs room on the Lua stack. A caller passes on the room it already asked the host for (lua_checkstack), and a function that fits in it doesn't ask again. One that does asks for more than it needs, so the calls below it, recursion included, usually find room. Near the limit of the Lua stack, a function runs again in a fresh call through the host.