How to improve bottleneck functions?
I am trying to improve the performance of my bottleneck functions that (ab)use the OnUpdate event. Since this event is called so frequently, I would like to optimize the functions.
My idea is to move my temporary variables outside of the function. Here's a silly example: Code:
myFrame:SetScript("OnUpdate",function(self,elapsedTime) The fix would be: Code:
do Does this extended variable scope have any benefit, or should I be letting the garbage collector pick up every local variable? Do for-loop variables override preexisting variables and build new localized ones? Thanks to any Lua-gurus out there! |
Variables created within the for loop are done so on the stack and are freed immediately after the end of execution without even being on the garbage collection facility's radar. You can create your variable at the top of the function so it's declared exactly once per run or leave it in the do block as you're doing now, but either way isn't going to significantly improve your CPU usage.
The only real optimization you can do in your OnUpdate script is to throttle how often it occurs - at the moment, if your FPS is 120 your function is executed 120 times in one second. Here, I'm limiting the runs to once every quarter of a second. Code:
do |
Quote:
|
I would make a local reference to UnitName as well.
|
yeah, stuff the unitname into the array you're using (or have another array that contains that info). if you absolutely can't do that, at least prebuild your unitid instead of running that pointless concat function. ie, unitid[1] = "raid1target"
|
Thanks for the replies!
Normally I would be imposing time restrictions as Torhal has suggested. This particular situation I am referencing is an exception, as i am dealing with time critical status changes that lack any triggered event. Pre-concatenated strings is an excellent idea, since I am dealing with a small finite set. The return value on the unit is actually the focus of the bottleneck. I'm not exactly sure how localizing UnitName would work. Does that mean doing something like Code:
local UnitNameShortcut = UnitName |
local UnitName = UnitName
is fine. |
Quote:
There's no serious flaw in localizing globals that you're going to use a lot, but there's really no tangible benefit unless you're trying to prevent the function from being tampered with outside of your addon. |
Having a quick glance of the code the best optimizations your gonna get is by either keeping tab of the size of array so you don't have to iterate over the entire thing with #array or by keeping a copy of the concatenation "raid"..i.."target".
i.e. lua Code:
These are probably the best optimizations your gonna get if your nutty about that stuff. |
All times are GMT -6. The time now is 06:15 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI