Quantcast
local _G = _G - WoWInterface
Thread Tools Display Modes
12-10-10, 01:53 PM   #1
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
local _G = _G

I see some people do this, and others don't. I've read that it speeds up access to _G slightly. Is that true, and if so, does it really make any difference?
  Reply With Quote
12-10-10, 03:21 PM   #2
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Code:
local _G = _G 
function test()
    local start = time()
    for x = 1, 200000000 do
        local a = _G["TargetFrame"]
        
    end
    print(time() - start)
end
Result: 8

Code:
function test()
    local start = time()
    for x = 1, 200000000 do
        local a = _G["TargetFrame"]
        
    end
    print(time() - start)
end
Result: 10

But can't say why.

[edit]
Keep in mind that are 200,000,000 loops. In words: two hundred million. The difference is ~2 secs. 0.00000001 secs per access.
Guess it's save to ignore that in most cases.

Last edited by Duugu : 12-10-10 at 03:26 PM.
  Reply With Quote
12-10-10, 03:29 PM   #3
henrik_s
Enhancement Shaman
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 19
Local variables are very fast as they reside in virtual machine registers, and are accessed directly by index. Global variables on the other hand, reside in a lua table and as such are accessed by a hash lookup. -- Thomas Johansson
  Reply With Quote
12-10-10, 05:43 PM   #4
Ketho
A Molten Giant
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 977
ehm whats the difference between _G and getfenv?
I've also seen the latter one being used sometimes ..
Code:
local _G = _G
Code:
local _G = getfenv(0)
  Reply With Quote
12-10-10, 09:12 PM   #5
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 806
http://www.lua.org/manual/5.1/manual.html#pdf-getfenv
http://www.wowpedia.org/API_getglobal

No difference I'd think after reading the two links above
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
12-11-10, 05:04 AM   #6
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
The more entries in _G, the more time it takes to find a specific entry. Hence the more AddOns you have installed which don't use upvalues (insert their variables directly into the global namespace), the longer the lookup time.
__________________
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
12-11-10, 05:09 AM   #7
haste
Featured Artist
 
haste's Avatar
Premium Member
Featured
Join Date: Dec 2005
Posts: 1,027
Originally Posted by Ketho View Post
ehm whats the difference between _G and getfenv?
I've also seen the latter one being used sometimes ..
Code:
local _G = _G
Code:
local _G = getfenv(0)
"Back in the day", when WoW used Lua 5.0, _G wasn't exposed to us. We had to use getlobal() to fetch dynamically named globals. Unless we fetched _G through getfenv() that is.

The was really the main reason for fetching _G. Today it isn't really worth it.
__________________
「貴方は1人じゃないよ」
  Reply With Quote
12-11-10, 05:15 AM   #8
Foxlit
A Warpwood Thunder Caller
 
Foxlit's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 91
Originally Posted by Torhal View Post
The more entries in _G, the more time it takes to find a specific entry. Hence the more AddOns you have installed which don't use upvalues (insert their variables directly into the global namespace), the longer the lookup time.
Lua tables are an array/hash table mix. Array accesses are simple arithmetic operations, and do not depend on the size of the array; and hash tables are engineered to ensure an O(1) lookup time -- regardless of the number of stored elements.

Now, the picture does depend on how saturated the hash table is; but that might also swing the other way around -- add a bunch of entries to _G, and Lua will resize it making access times shorter. On a lower level, memory caching does come into play, but its effects are unavoidable anyway if you're using large quantities of data.

In short, what you're describing is not how hash tables work.
__________________
... and you do get used to it, after a while.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » local _G = _G

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