Thread Tools Display Modes
10-15-16, 01:17 AM   #1
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 130
local _G = _G

When peeking into certain addons' code, I notice a lot of globals being set to locals, one commonly being the following:

Lua Code:
  1. local tonumber, pairs, ipairs, error, unpack, select, tostring = tonumber, pairs, ipairs, error, unpack, select, tostring

I was curious, does this save memory space or something?
  Reply With Quote
10-15-16, 01:41 AM   #2
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Sweetsour View Post
When peeking into certain addons' code, I notice a lot of globals being set to locals, one commonly being the following:

Lua Code:
  1. local tonumber, pairs, ipairs, error, unpack, select, tostring = tonumber, pairs, ipairs, error, unpack, select, tostring

I was curious, does this save memory space or something?
Sacrifices some memory usage for a tiny bit of performace gain.
  Reply With Quote
10-15-16, 10:44 AM   #3
VincentSDSH
Non-Canadian Luzer!
 
VincentSDSH's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2006
Posts: 350
Probably needs a bit of conceptual explanation for new authors who'll run across this question.

Lua is a maze of tables. When you call a function (say, 'pairs') it goes through the function list to find it, traversing the tree's branches till it does. Its vars in this usage are pointers to table locations that contain the function (unlike some languages where an = is a value copy) so that makes things both easier or harder depending on what you're trying to achieve.

local pairs = pairs says "store the location of 'pairs' in this local var called 'pairs' (or anything you like) so you don't have to look for it.

You'll see the same with a lot of Ace addons: local sdbc, layout = self.db.char, self.db.global.customization.windows.layout

When authors have a lot of loops or a lot of calls and actions, they'll trade memory for performance b/c those little lookups add up amazingly fast, especially when you run a lot of addons.
__________________
AddonsExecutive Assistant User Configurable To-Do ListLegible Mail Choose the Font for Your Mail
  Reply With Quote
10-16-16, 04:17 PM   #4
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Keep in mind that the performance gains are very minuscule. Only worth it if you are calling the upvalued functions/table lookups many times when optimized performance is best - in combat, an OnUpdate script, etc.
__________________
"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
10-18-16, 04:46 PM   #5
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 130
Thanks for all the info, it all makes a lot more sense
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » local _G = _G


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