Garry's Mod Wiki

Revision Difference

optimizationTips#564917

<cat>Dev.Lua</cat> <title>Concepts - Optimization Tips</title> # Knowing When to Optimize While optimization can improve the performance of your code sometimes this can come at the cost of the readability of your code. If you or others is actively working on some code maintaining the readability of that code is important for quicker development time. Knowing what state you want your code to be in should be the first thing you think about when approaching optimization. - Is the code a final completed solution? - will anyone ever wanna look at this code to figure out how it works? - does 3 microseconds of performance really justify the illegible nature of the code? This article may introduce some tips that are "technically faster" but harder to interpret at a glance. Before you implement them at the sake of speed really consider if compromising the legibility of this section of code needed. With this all being said there are places you should focus on optimization, anything that runs regularly, like code inside the infamous three hooks `tick`, `think` and `render`, can impact performance and should be once of the first places you look when starting to optimize. # The Most Important Tips These tips are broadly applicable and more often than not should be the default. Each Subheading will provide the basic version of the tip and then a deeper understand as to why. This additional why section is completely optional, you don't need to understand why these tips work at a low level to use them. ## Use Local Variables! <page text="Local Variables">Beginner_Tutorial_Variables#localvariables</page> are significantly faster than global variables. This tip is so broadly applicable that the standard should be to always use local variables unless there is no way to achieve the desired result without using a local. ```lua myVar = 0 -- Bad! Lua is global by default local myVar = 0 --Good! myVar will only be accessible within the defined scope ``` ### Understanding Why This difference comes from how the Lua Virtual Machine handles them internally. Local variables are compiled into **registers**, these are fixed-size slots allocated on the stack for each function. During runtime, each local variable is assigned an integer index (the offset) into this register array. This means: - Access to local variables now only need a direct index operation - The Lua VM emits a single [opcode](https://en.wikipedia.org/wiki/Opcode) for accessing or modifying local variables - Accessing local varibles bypass any need for name resolution or [hashing](https://en.wikipedia.org/wiki/Hash_function) locals in [closures](https://en.wikipedia.org/wiki/Closure_(computer_programming)) are treated as [upvalues](https://www.lua.org/pil/27.3.3.html). These upvalues point directly to the stack (or heap allocated closure variable) if the function outlives the scope) maintaining its fast access. Globals On the other hand, are not stored in fixed memory slots. Instead, they are entries into a special table. For Garrys Mod (Lua version 5.1) this is the `_G` table. The process to access a global variable from that table involves: 1. Hashing the variable string name 2. Performing a table lookup using that key 3. Returning or modifying the value associated with that key. This where the next broadly applicable tip comes in super handy. ## Caching! Always cache repeated lookups of the same data. If the information doesn't change every time you look at it, there's no need to rebuild the entire table each time. Instead, build the table once and reuse it each time. You can then monitor for changes that might affect the data and then rebuild the table only when necessary. This is crucial in high performance areas of your code _(notable examples including hooks like `tick`, `think`, and `render`)_ ```lua -- -- Bad Example -- hook.add("tick","BadExample",function() --Every time this hook is called a new anonymous function is created for i,v in ipairs( player.GetAll() ) do --Every time this is executed the player.GetAll() function will run hook.Add( "Tick", "BadExample", function() --Every time this hook is called a new anonymous function is created for i, v in ipairs( player.GetAll() ) do --Every time this is executed the player.GetAll() function will run print( v:GetName() ) end end) end ) -- -- Good Example -- local cachedPlayers = {} local function updateCache() cachedPlayers = player.GetAll() end hook.add("PlayerConnect","PlayerJoinedUpdateCache",updateCache) hook.add("PlayerDisconnected","PlayerLeftUpdateCache",updateCache) hook.Add( "PlayerConnect", "PlayerJoinedUpdateCache", updateCache ) hook.Add( "PlayerDisconnected", "PlayerLeftUpdateCache", updateCache ) local function DoOurThing() for i,v in ipairs( cachedPlayers ) do -- Now we we have one table that we can reuse, instead of rebuilding a new one each time for i, v in ipairs( cachedPlayers ) do -- Now we we have one table that we can reuse, instead of rebuilding a new one each time print( v:GetName() ) end end hook.add("tick","GoodExample",DoOurThing) hook.Add( "Tick", "GoodExample", DoOurThing ) -- Notice the two things we've improved here, avoiding creating repeated anonymous functions (closures) and using a cached table ``` ### What to cache? the simple thing to remember if object creation. if calling a function or completing an operation results in a object being created you want to do that as few times as possible. Common Things Not Cached that **SHOULD** be cached - <page>Color</page> Objects - unmoving <page>Vector</page> or <page>Angle</page> types - <page>Material</page> and <page>IMaterial</page> Types - <page>Entity</page> Type - unchanging <page>Player</page> Types - <page>Weapon</page> Type ### Understanding Why This function goes hand in hand with avoiding repeated heap allocations. Creating new objects _(`{...}` or `function()`)_ allocates new memory for that object on each iteration, triggering garbage collection and hurting performance. Even efficient garbage collection carry runtime and memory costs. ## Smart Type Usage ( Color(), Vector(), Angle() ) Garry's Mod types like `Color()`, `Vector()`, and `Angle()` are just tables _(because everything in Lua is a table)_ which means calling these constructors falls into the same issues we covered in the caching section of this article. To summarize that section, avoid repeated object creation wherever possible. However that's not the only way to optimize the handling of these objects. In Garry's Mod you will often encounter the need to manipulate these objects in some way shape or form. While you can use regular operators like `+`, `-`, `*`, and `/` the way these work is by creating a new object of that type, and filling in the resulting operations. Instead of you use functions like: - <page>Vector:Add</page> - <page>Vector:Sub</page> - <page>Vector:Mul</page> - <page>Vector:Div</page> provided by from the vector class for example. These functions instead skip that object creation, and the resulting garbage collection call, to still complete the desired operation. # Niche / Pico Optimizations This section includes code that falls under the umbrella of "yes its faster but at what cost". Some of the tips here only gain you a few microseconds and significancy harm the legibility of your code. You should really only be included if you either know what your doing, or are out of other options. ##Using Inline Expression over Function Calls Using inline expressions over function its technically faster ``` table.insert(tbl, 0) -- The Standard Way tbl[#tbl + 1] = 7 -- Technically Faster ``` ##Optimizing Math Calculations Computers can solve certain math equations quicker than others, restructuring math equations to increase the efficiency at which the computer can solve it leading to more performance, but this gain is menial at best. This will also make your math MUCH harder to follow, if your working on a team, publishing an addon, or building a library chances are anyone who looks at your code will struggle to reverse how you got these numbers originally. ``` -- Multiplication Over Division x / 2 -- Standard x * 0.5 -- Technically Faster ``` ``` -- Squaring over Exponentiation x^2 -- Standard x * x -- Technically Faster ``` ``` -- Factoring Expressions x*y + x*z + y*z --Standard x * (y + z) + y*z --Technically Faster ```