View Single Post
08-18-12, 03:00 PM   #13
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
What version of Lua are you talking about? As of now, WoW runs 5.1, the allocation for tables may be different in newer versions. Also, my explanation the subject wasn't memory size, but the number of elements allocated in the 2 arrays of a table.



http://www.wowpedia.org/Memory_usage.
Memory accumulation isn't linear however, instead there is an exponential algorithm, so that as the table gets larger, incrementally larger numbers of elements are pre-allocated.


http://www.wowpedia.org/Lua_object_memory_sizes
A new, empty table will consume 40 bytes.

As indexes are added, the table will allocate space for new indexes at an exponential rate. For example:

Code:
local t = {}  -- 40 bytes
t[1] = true   -- alloc 1 index
t[2] = true   -- alloc 1 index
t[3] = true   -- alloc 2 indexes
t[4] = true
t[5] = true   -- alloc 4 indexes
...
If a table is indexed by sequential integers, each index will take 16 bytes (not including any memory allocated for the value). If the becomes a hash, the index size will jump to 40 bytes each. Lua is "smart" and will allocate at the 16 byte rate as much as it can. If the int sequence is broken, the new values will allocate at the 40 byte rate. For example:

Code:
local t = {}  -- 40 bytes
t[1] = true   -- 16 bytes
t[2] = true   -- 16 bytes
t[3] = true   -- 32 bytes
t[4] = true
t[8] = true   -- 40 bytes
t[9] = true   -- 40 bytes
t[10] = true  -- 80 bytes
t["x"] = true
t["y"] = true -- 160 bytes
...
__________________
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 : 08-18-12 at 03:10 PM.
  Reply With Quote