About add-ons optimization
View Single Post
08-23-13, 03:10 PM
Join Date: Mar 2006
In descending order, I'd say these should be your areas of focus:
1. Make sure you are not leaking any globals, and reconsider the necessity of any intentional globals, including the names of frames and other objects. Your main addon object and/or main display frame are good candidates for global names; other objects generally don't need names unless you're using Blizzard templates that require them.
2. Upvalue any globals you're reading in frequently called functions, like OnUpdate, CLEU, UNIT_AURA, etc. Don't bother upvaluing globals you only read infrequently, such as in response to the user changing an option, the player leaving combat, etc.
3. Avoid using OnUpdate scripts for anything other than updating a visual display, such as a statusbar, and move as much of that work as you can to other places, such as event handlers. For example, if you're showing a timer bar for a buff on the player, don't call UnitAura each time your OnUpdate script runs -- store the info you need in variables, and update those variables when UNIT_AURA fires for the player unit instead. Depending on what you're updating, you may be able to offload some/all of the work into C code by using animations.
4. Avoid calling functions when you can, as it's really slow. Some common examples of unnecessary function calls include:
local x = select(2, ...)
local _, x = ...
for i, v in ipairs(tbl) do
for i = 1, #tbl do local v = tbl[ i ]
5. Avoid creating new tables when you can reuse existing tables. In keeping with #4, don't use
unless you actually need to -- for example, if you have a table that's storing info about a buff obtained via UnitAura, you don't need to
it, since the keys don't change, and you just overwrite the values.
6. Keep variables limited to the narrowest scope necessary. For example, if a variable is only used inside a loop, define it locally inside the loop, not outside.
7. Call string functions directly, eg.
format("banana", "%l", strupper, 1)
instead of of
("banana"):format("%l", strupper, 1)
-- both incur the cost of a function call, but the second also incurs the costs of getting a metatable and performing a table lookup. If you upvalue
then the direct-call method is enormously faster, but even as a global lookup it's still slightly faster. The only time I'd recommend using the meta-method is when you're chaining multiple string operations; in that case, it's still the slower way to do it, but the increased readability of your code is usually worth it.
If you want more specifics, post your code.
Troubleshoot an addon
Turn any code into an addon
More addon resources
Need help with your code?
code! Attach or
Please don’t PM me
about addon bugs or code questions. Post a comment or forum thread instead!
Last edited by Phanx : 08-23-13 at
View Public Profile
Send a private message to Phanx
Find More Posts by Phanx