Thread Tools Display Modes
03-20-11, 09:15 AM   #1
daylesan
A Fallenroot Satyr
Join Date: Feb 2011
Posts: 22
Localizing global functions?

I see other addon devs do this and am wondering why. Here's an example from Omen
local GetNumRaidMembers, GetNumPartyMembers = GetNumRaidMembers, GetNumPartyMembers
Is this done to optimize performance?
  Reply With Quote
03-20-11, 09:28 AM   #2
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Yes, local function are used -slightly- faster than global functions. It's a very minimal difference though.
  Reply With Quote
03-20-11, 10:30 AM   #3
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
I agree with Haleth, you get a minimal speed increase

Global variables would litter the global namespace and maybe even override another addon's variables ..
-- WowAce
Globals can cause issues because they may overwrite other people's functions, as well as being general bad practice. By not using globals and instead using a namespace format, one can gain quite a bit anyway

As for the optimization by variable scoping: (bit offtopic)
Originally Posted by Mischback View Post
Keep scope as minimal as possible, just as Xinhuan said.

Xinhuan aswell stated very clear, in which cases a larger scope is needed / can be useful.

But the bottomline is: Small scope = good!

Edit:
Didn't see that the OP was talking about functions instead of variables ><

Last edited by Ketho : 03-20-11 at 10:40 AM.
  Reply With Quote
03-20-11, 01:21 PM   #4
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Well, the name of a function *is* a variable.

Code:
local function foo() return end
is the same as

Code:
local foo = function() return end
and then you can even do stuff like this

Code:
local myvar = foo
myvar()

The optimizations you gain by using your own local scope of a global variable are so negligible that it's only really worth it for functions you call very often, as in an OnUpdate.
__________________
"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-20-11, 01:30 PM   #5
hankthetank
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Jul 2009
Posts: 64
You save the time to lookup the global. It's a good tweak for performance critical parts of your code, that is OnUpdate or Combatlog handlers mostly. Some authors take this way too far though and local every global they use. I guess it's their justification for using buzzwords like lightweight.

Last edited by hankthetank : 03-20-11 at 01:35 PM.
  Reply With Quote
03-20-11, 02:16 PM   #6
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Code:
local tstarttime = GetTime()
for x = 1, 1000000 do
    UnitName("target")
end
print(GetTime()-tstarttime)
That one needs 0.162 seconds.

Code:
local UnitName = UnitName
local tstarttime = GetTime()
for x = 1, 1000000 do
     UnitName("target")
end
print(GetTime()-tstarttime)
That one needs 0.147 seconds.

A difference of 15 microseconds for 1 million (!) function calls.
I would say in terms of performance gains it's save to simply ignore the local function stuff.

Last edited by Duugu : 03-20-11 at 02:18 PM.
  Reply With Quote
03-20-11, 07:07 PM   #7
Xinhuan
A Chromatic Dragonspawn
 
Xinhuan's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 174
There is a second side effect to localizing global functions. And that is, if some other addon were to hook the function AFTER your addon has grabbed the local reference, when you call the local reference of the function, the hooked version doesn't get called.

[This does not apply to hooksecurefunc().]

Even if the gains are minuscule, its still a good programming practice to optimize when possible if it does not get in the way of code readability and maintenance.
__________________
Author of Postal, Omen3, GemHelper, BankItems, WoWEquip, GatherMate, GatherMate2, Routes and Cartographer_Routes
  Reply With Quote
03-20-11, 09:46 PM   #8
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
Originally Posted by Xinhuan View Post
There is a second side effect to localizing global functions. And that is, if some other addon were to hook the function AFTER your addon has grabbed the local reference, when you call the local reference of the function, the hooked version doesn't get called.

[This does not apply to hooksecurefunc().]
From my tests, hooksecurefunc() actually does change the pointer for the function passed. It works exactly like a post-hook from the old days, only it's made specifically to bypass the taint system.
__________________
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)

Last edited by SDPhantom : 03-20-11 at 09:48 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Localizing global functions?


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