Thread Tools Display Modes
05-12-13, 02:50 PM   #1
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
UpValues vs function scope local vars

I have several local UpValues which are used by several different functions.

Is there any significant difference between the speed in which the API looks up vars of different scopes?

The reason I ask is because, in one particular func (which can potentially be called often), I can't avoid referring to an UpValue 6 or 7 times, and I've been wondering whether or not I should just assign it to a function scope var at the start of the func and refer to that instead 6 or 7 times.

ie:

Lua Code:
  1. function MyFunction()
  2.     local newVar = UPVALUE_VAR;
  3. -- do stuff with newVar instead
  4. end


Thanks for any advice etc.
__________________
__________________
  Reply With Quote
05-12-13, 03:54 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Just leave it as an upvalue. Especially if the function's getting called often.
__________________
"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
05-12-13, 05:57 PM   #3
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
To clarify: Upvalues are assigned to once, using a single lookup. Within a function, variables are assigned to via a lookup that occurs every time the function is called.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
05-13-13, 05:36 AM   #4
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
I'd rather pull out as many constants as I can in the core of the file itself, as Torhal said, each function call you make a new reference, so not sure how much "performance" it actually saves you, say it's a OnUpdate script running each frame. ;P
__________________
Profile: Curse | Wowhead
  Reply With Quote
05-14-13, 08:13 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Upvalues and locals practically share the same registry space. If any performance impact at all, I'd think it would take more CPU power initializing a local copy of an upvalue.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
05-15-13, 07:32 AM   #6
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
In the initial example, since the function is called over and over many times, I would think
Lua Code:
  1. local newVar = UPVALUE_VAR
  2. local function MyFunction()
  3.     -- do something with newVar
  4. end
Would be much better than
Lua Code:
  1. local function MyFunction()
  2.     local newVar = UPVALUE_VAR
  3.     -- do something with newVar
  4. end
Simply because you are assigning newVar once rather than repeatedly, especially since its value never changes. That said, is assigning upvalues a performance gain? That's a different story.
  Reply With Quote
05-15-13, 03:07 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
If you're calling the function in an OnUpdate script, or an event handler that responds to CLEU or UNIT_HEALTH or some other frequently-fired event, go ahead and upvalue it. If you're calling it in response to events like PLAYER_TARGET_CHANGED, PLAYER_REGEN_ENABLED, or UNIT_NAME, don't bother. It's just code clutter for no real benefit.
__________________
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

WoWInterface » Developer Discussions » Lua/XML Help » UpValues vs function scope local vars

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