Reply
Thread Tools Display Modes
Unread 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
fRodzet is offline   Reply With Quote
Unread 03-25-13, 01:48 PM   #2
Haleth
This Space For Rent
 
Haleth's Avatar
WoWInterface Super Mod
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.
Haleth is offline   Reply With Quote
Unread 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?
fRodzet is offline   Reply With Quote
Unread 03-25-13, 02:07 PM   #4
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 4,738
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?
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
Phanx is offline   Reply With Quote
Unread 03-25-13, 04:30 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 9,708
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

Seerah is offline   Reply With Quote
Unread 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
fRodzet is offline   Reply With Quote
Reply

Go BackWoWInterface » 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