Skip to content

Optimizations

autoplay2c optimizes every build. There are no switches to turn on and no levels to choose, because the optimizations below are always on. None of them changes what your program does.

The One Rule

A compiled program behaves exactly like the interpreter. It prints the same, returns the same, raises the same errors with the same messages and line numbers, and calls the same metamethods in the same order.

That's why every optimization works the same way. The compiler looks for code where it can prove what happens, such as two numbers being added, and makes that case fast. For everything else, and whenever a value at run time turns out to be something unexpected, the code takes the ordinary path that does exactly what the interpreter does.

local function area(w, h)
    return w * h;
end

area(3, 4);       -- numbers: the fast path, a single machine instruction
area("3", 4);     -- a string: the ordinary path, 12 as the interpreter gives it
area(nil, 4);     -- the ordinary path raises the interpreter's own error:
                  -- attempt to perform arithmetic on local 'w' (a nil value)

Optimizations that do change behaviour in some case aren't done automatically. Those are directives, which you ask for yourself, and each one tells you what it assumes.

How Fast Is Compiled Code?

That depends on what your code does.

Kind of code Compared to the interpreter
Arithmetic in loops, number crunching Up to 7 times as fast
Recursive and small functions over numbers Up to 4 times as fast
Arrays local to a function About twice as fast
Strings and patterns About the same, a little slower
Code that mostly works on tables other functions share, or creates many objects 1.5 to 3 times as slow

The reason for the last row is that a compiled module talks to the host's Lua through its public interface, one call per operation, while the interpreter works on its own data directly. The optimizations below remove as many of those calls as they can, and that work continues.

In most AutoPlay and Setup Factory projects, the time goes into the actions themselves (showing pages, reading files, downloading), not into Lua, so you won't notice either way. The main reason to compile is protecting your source, and where your code computes a lot, it runs faster as well.

The Optimizations

Page What it covers
Numbers Locals and expressions that hold numbers are computed natively
Calls and Functions Direct calls, loops for self tail calls, cheap upvalues and globals
Tables Fixed keys, known tables, lengths, pairs, ipairs, fields read again, native arrays and records
More Optimizations Conditions, locals, concatenation, library functions, Lua 5.3 integers, the machine code
Writing Fast Code How to write code that gets the most out of all of this
Technical details
  • The figures above come from a suite of 22 benchmark programs, run against the real Lua DLLs of AutoPlay Media Studio 8 and 9 and Setup Factory 9.7, and a Lua 5.3 64-bit build for Setup Factory 10. Over the whole suite, compiled code currently takes 0.77 to 0.84 times the interpreter's time, depending on the host.
  • Every optimization is tested the same way as the compiler itself: the test programs and 1,000 randomly generated programs per host must give exactly the interpreter's results on every host.
  • The compiler only uses the public Lua API. It never reads the host's internal data structures, which differ between hosts and builds.
  • Numbers become text only through the host (tostring, .., string.format), and text becomes numbers only through the host, so every digit is the host's own.