Skip to content

Performance guidelines

(Jip) Willem Wijnia edited this page Nov 19, 2023 · 8 revisions

Supreme Commander: Forged Alliance comes equipped with a Lua interpreter. Unlike compiled languages, where compilers can optimize and eliminate common inefficiencies, an interpreter executes code exactly as it is written. This makes 'code hygiene' particularly crucial. Your code navigates through its execution without the luxury of compiler optimizations. This article introduces various considerations to help write high performing code.

For references and additional reading:

Specifically (2) and (3) are a good read that can help your understanding of this article.

Concepts

Scope of a value

Understanding the scope of a value is critical in Lua, covering global, table, upvalue, and local scopes. Retrieving values involves specific opcodes: GETGLOBAL, GETUPVALUE, GETTABLE, and GETLOCAL. Notably, GETGLOBAL consumes the most resources, while GETLOCAL is essentially a cost-free move operation.

A relatively simple way to 'upgrade' the scope of a value to the upvalue scope is by writing local X = X, where X is a value in the global scope. An excellent example can be found in the chapter 'Basic facts' of reference (3).

This technique is to be applied consistently throughout the repository, as demonstrated in the FactoryUnit class. External references are localized at the top of the file, ensuring they are captured in the closure when the class functions are created. As mentioned before, the upvalue scope enhances access speed compared to the global or table scope and therefore the code executes faster.

Clone this wiki locally