WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   UpValues vs function scope local vars (https://www.wowinterface.com/forums/showthread.php?t=46455)

Aanson 05-12-13 02:50 PM

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.

Seerah 05-12-13 03:54 PM

Just leave it as an upvalue. Especially if the function's getting called often.

Torhal 05-12-13 05:57 PM

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.

Vlad 05-13-13 05:36 AM

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

SDPhantom 05-14-13 08:13 PM

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.

myrroddin 05-15-13 07:32 AM

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.

Phanx 05-15-13 03:07 PM

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.


All times are GMT -6. The time now is 10:13 AM.

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