Thread Tools Display Modes
03-25-13, 01:42 PM   #1
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
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
  Reply With Quote
03-25-13, 01:48 PM   #2
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
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.
  Reply With Quote
03-25-13, 01:57 PM   #3
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by Haleth View Post
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?
  Reply With Quote
03-25-13, 02:07 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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?
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
03-25-13, 04:30 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
And if you're looking for an easy, starter addon... it's NOT going to be a threat meter. Just FYI.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
03-25-13, 05:51 PM   #6
fRodzet
A Flamescale Wyrmkin
Join Date: Mar 2013
Posts: 114
Originally Posted by Seerah View Post
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
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Omen local variables confuses me - help ty :)

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off