Skip to content

--#inline

With --#inline the compiler copies a small function's body into every place that calls it. The call itself, with all the work that comes with it, disappears.

--#inline
local function clamp(x, lo, hi)
    if x < lo then
        return lo;
    elseif x > hi then
        return hi;
    end
    return x;
end

local volume = clamp(tonumber(Input.GetText("Volume")) or 0, 0, 100); -- clamp's body runs right here

Where It Applies

--#inline applies to a function definition: local function f, local f = function, function f, function a.b() or function a.b:c(). On any other statement it gives a warning and does nothing.

Which Calls Are Inlined

The calls the compiler can recognise by name are inlined:

Inlined Not inlined
clamp(x, 0, 1), through the local, also from nested functions (clamp)(x, 0, 1)
Math2.Clamp(x, 0, 1), through the global name it was defined under local c = Math2.Clamp; c(x, 0, 1)
Vector:Length(), for a method Calls inside another inlined body

The arguments are evaluated exactly as they would be for a call, all of them, and then cut or padded to the parameters.

Functions of One Expression

A function whose body is one return of one expression, with no locals of its own, is inlined as part of the expression around the call, when its arguments are numbers or locals used in arithmetic. Its parameters are then the arguments' native values, and no Lua values are passed in or out at all:

--#inline
local function ror(x, n)
    return ((x >> n) | (x << (32 - n))) & 0xFFFFFFFF;
end

local s1 = ror(e, 6) ~ ror(e, 11) ~ ror(e, 25);   -- three rotations, computed natively

When a value isn't a number, such as a table with metamethods, the whole expression runs the ordinary code, which runs each call once and in order. The log names such calls as A2C:IR:O:3008.

When the Compiler Says No

--#inline is a request. The compiler declines functions it can't inline safely, and tells you why in a warning, such as:

tools.lua:4:1: warning A2C:IR:W:3002: `f` is not inlined: it takes `...`

A function isn't inlined when it:

  • takes ...,
  • defines functions of its own,
  • calls itself,
  • uses local variables of a function around it,
  • has more than 40 statements,
  • is defined under a name that doesn't start at a global, or is defined more than once with --#inline.

Also keep in mind that return f(x) with f inlined is no longer a tail call. In Lua 5.3, a call isn't inlined where the function's globals can't be reached the same way.

The Promise

The name a function was defined under keeps that function. This means you never assign another value to it, and the function is defined before the first inlined call runs.

When the promise is broken, inlined calls keep running the original body, even after you assign another function to the name, and even before the definition has run. Calls that weren't inlined call whatever the name holds at that moment.

--#inline
function Twice(x) return x * 2; end

function Twice(x) return x * 3; end -- breaks the promise

Debug.Print(Twice(5));               -- inlined: still 10
Debug.Print((Twice)(5));             -- a real call: 15

When the compiler can see the promise broken, it warns you at the directive (A2C:IR:W:0103): a local function is assigned, or a global one (or a table it is in) is assigned in a second place or in a loop. The second Twice above gives that warning.

Tip

--#inline is most useful for small helpers that are called in loops. autoplay2c already calls local functions that are never reassigned directly, without a lookup (see Calls and Functions), so for larger functions the gain is small.