Skip to content

--#cache

With --#cache the compiler looks up a global name once and remembers what it found. This way, every lookup after that is almost free.

In AutoPlay Media Studio, every action you call is a lookup. Input.SetText first finds the global table Input, and then the field SetText in it, which means two table lookups each time the line runs. With --#cache, it's two lookups the first time, and none after that.

--#cache
Input.SetText("Name", value); -- Input and Input.SetText are looked up once

Where It Applies

On a function definition, it applies to the whole function, including the functions defined inside it:

--#cache
function UpdateForm(data)
    Input.SetText("Name", data.name);     -- cached
    Input.SetText("Email", data.email);   -- cached, the same lookup
    Label.SetText("Status", "Saved");     -- cached
end

This works for function f(), function a.b:c(), local function f(), and for a statement whose value is a function, such as local f = function() end.

On any other statement, it applies to that statement, including the blocks of a loop or an if:

--#cache
for i = 1, 100 do
    ListBox.AddItem("List", "Item " .. i, i); -- ListBox.AddItem is looked up once, not 100 times
end

What Is Cached

A chain of names that starts at a global and continues with fixed keys is cached:

Cached Not cached
Input t.x where t is a local
Input.SetText and Input["SetText"] Input[name], a key that isn't fixed
Application.GetLastError
a.b.c, and a and a.b along with it
The method of Input:SetText() style calls

Assignments are never cached, so Input.SetText = MyFunction changes the table, as always.

The Promise

A cached name keeps its value once it has one.

The first lookup is an ordinary one. It finds the value, raises the usual error if Input doesn't exist, and only remembers the value if it isn't nil. A lookup that runs before its global exists keeps looking until it finds one.

From then on, cached lookups use what they remembered. If you assign another value to a cached name (or to a part of it, such as Input) after that, cached lookups keep using the old value, while lookups in code without --#cache see the new one.

function Log(text) Debug.Print(text .. "\r\n"); end

--#cache
function Save()
    Log("saving");      -- remembers the first Log
end

Save();
function Log(text) TextFile.WriteFromString("log.txt", text, true); end
Save();                 -- still calls the first Log

For the actions of AutoPlay Media Studio and Setup Factory the promise holds, because nobody replaces Input.SetText. Be careful with your own global functions and tables that you assign more than once.

When the compiler can see the promise broken, it warns you (A2C:IR:W:0104): a cached name, or a table it is in, is assigned in two places, or in a loop. The second function Log above gives that warning. It can't see everything: an assignment that runs many times from one place, such as in a button's On Click, or a field changed through another name, isn't warned about.

Lua 5.1

In Lua 5.1 the promise also includes that you don't use setfenv on a function with cached lookups.

Technical details
  • Every place in the module that caches the same name shares one slot, so it's looked up once for the whole module, not once per function.
  • The slots belong to the module and the Lua state. They're kept in a table in the registry, made when the module is loaded. A function with cached lookups reads that table once each time it runs.
  • When a cached value is a function compiled into the same module, calls through it go to the compiled code directly, without going through the host's lua_call.
  • A metamethod (__index) that computes a cached value only runs for the first lookup.
  • In Lua 5.3, globals are fields of _ENV. Only globals of the main chunk's _ENV are cached, not those of a local _ENV you declare.