Skip to content

Writing Fast Code

You don't have to change your code for autoplay2c, since it compiles any Lua and keeps it working. But if a part of your project computes a lot, these habits let the optimizations do the most.

Most of them are good Lua anyway, and they make the interpreter faster as well.

Use Locals

Locals can be native numbers, flags and arrays, but globals can't. A global is a table lookup on every use, because any script may change it at any time.

total = 0;
for i = 1, 1000 do
    total = total + i;
end
local total = 0;
for i = 1, 1000 do
    total = total + i;
end

Use Local Functions, and Don't Reassign Them

A local function that you never assign again is called directly, can get native numbers as its parameters, and can be turned into fully native code. A global function is looked up on every call.

function Square(x) return x * x; end
local function square(x) return x * x; end

If other scripts have to call the function, keep it global, and use --#cache where you call it in a loop.

Keep One Kind of Value in a Local

A local that only ever holds numbers is native and doesn't need any checks. Once it also holds a string or nil somewhere, it's checked on every use inside loops, and it's an ordinary Lua value outside them. Use separate locals for separate things, and avoid nil as a "nothing yet" marker for numbers.

local best = nil;                       -- sometimes nil, sometimes a number
for i = 1, 1000 do
    local d = (i * 7) % 13;
    if best == nil or d < best then best = d; end
end
local best = 1 / 0;                     -- infinity: always a number
for i = 1, 1000 do
    local d = (i * 7) % 13;
    if d < best then best = d; end
end

In Lua 5.3, also keep integers and floats apart where you can, so write 0.0 for a float you start at zero. A local that switches between the two still works natively, with a small cost per operation.

Keep Work Arrays Private

A table that's used as an array inside one function, and never passed anywhere, becomes a native array. Do the work in it, and then copy the results to a table you return.

The same goes for tables with named fields that your script keeps to itself: they become native records. Read and write their fields by name, and copy what another function needs into its arguments instead of passing the table.

Cache Library Functions in Locals

local floor, sqrt = math.floor, math.sqrt;

This saves the lookup of math and its field on every call, in the interpreter as well as in compiled code.

Use the Host's Iterators

pairs and ipairs on plain tables run as a direct table walk. A numeric for over 1, #t is just as fast.

Know What Costs

Some things go through the host every time, whatever the compiler does, because their behaviour is the host's:

  • calls to actions such as Input.SetText and other host functions,
  • converting numbers to text (.., tostring, string.format) and back (tonumber),
  • string functions and patterns,
  • tables that other functions see, and metamethods.

None of these are slow, but in a hot loop they add up. Move what you can out of the loop.