WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Omen local variables confuses me - help ty :) (https://www.wowinterface.com/forums/showthread.php?t=46108)

fRodzet 03-25-13 01:42 PM

Omen local variables confuses me - help ty :)
 
So i was reading through Omens Threat Meter AddOn. But i'm abit confused to why he make such locals:

Lua Code:
  1. local floor, format, random, pairs, type = floor, format, random, pairs, type
  2. local tinsert, tremove, next, sort, wipe = tinsert, tremove, next, sort, wipe
  3. local RAID_CLASS_COLORS = RAID_CLASS_COLORS
  4. local UnitDetailedThreatSituation = UnitDetailedThreatSituation
  5. local UnitExists, UnitGUID, UnitName, UnitClass, UnitHealth = UnitExists, UnitGUID, UnitName, UnitClass, UnitHealth
  6. local UnitIsPlayer, UnitPlayerControlled, UnitCanAttack = UnitIsPlayer, UnitPlayerControlled, UnitCanAttack

note that they are equal on each side of the equation - why is that?

Ty :)

Haleth 03-25-13 01:48 PM

The author creates local copies of references to functions they use often for a small performance improvement. Calling a function using a global reference is slightly slower than using a local reference.

The difference is very small, though. If you call a function a million times, you might see a millisecond difference or so. It can be worth considering for addons which do a lot of calculations in combat, like Omen.

fRodzet 03-25-13 01:57 PM

Quote:

Originally Posted by Haleth (Post 275175)
The author creates local copies of references to functions they use often for a small performance improvement. Calling a function using a global reference is slightly slower than using a local reference.

The difference is very small, though. If you call a function a million times, you might see a millisecond difference or so. It can be worth considering for addons which do a lot of calculations in combat, like Omen.

Ty for the answer - so in fact if i was to rewrite his addon i could actually leave these kind of locals behind?

Phanx 03-25-13 02:07 PM

You could, yes, but as Haleth pointed out, the difference in speed is meaningful for an addon like Omen that's doing a lot of work in combat. Think about how many threat updates happen in a 25-player raid vs. a boss. Every HoT and DoT tick changes threat, as does every spell cast, every ability used, etc. Now think about a typical player running Omen, Recount, DBM, and Healbot -- how many functions are being called every second? Every bit of speed counts in keeping the whole UI running smoothly in raid combat.

There's absolutely no downside to upvaluing global functions you're going to be calling a lot (to "upvalue" something means to make a local pointer to something in a higher scope -- in this case, taking a function in the global scope, and making a pointer local to the scope of your file) and there is a tangible upside, so why not do it?

Seerah 03-25-13 04:30 PM

And if you're looking for an easy, starter addon... it's NOT going to be a threat meter. Just FYI. ;)

fRodzet 03-25-13 05:51 PM

Quote:

Originally Posted by Seerah (Post 275186)
And if you're looking for an easy, starter addon... it's NOT going to be a threat meter. Just FYI. ;)

haha. i'll keep that in mind :) ty


All times are GMT -6. The time now is 03:15 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI