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:
Thanks for any advice etc. |
Just leave it as an upvalue. Especially if the function's getting called often.
|
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.
|
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
|
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.
|
In the initial example, since the function is called over and over many times, I would think
Lua Code:
Lua Code:
|
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