WoW:Lua object memory sizes: Difference between revisions

→‎Function closures: Better explanation of changing vs unchanging values
m (minor spelling and grammatical corrections)
(→‎Function closures: Better explanation of changing vs unchanging values)
Line 49: Line 49:


== Function closures ==
== Function closures ==
''Note that the actual '''code''' also uses RAM, of course. But that only happens '''once'''. Closures however are created each time the word "function" is executed.''


Each function closure takes 20 bytes of RAM. The below code will use 20000 bytes:
Each function closure takes 20 bytes of RAM. The below code will use 20000 bytes:
Line 55: Line 57:
  end
  end


Each upvalue uses an additional 36 bytes of RAM for integers/booleans/refs. The below code will use 20000 + 36000 = 56000 bytes:
Upvalues that do not change only uses 4 bytes per closure. The below code will use 20000 + 4000 = 24000 bytes:
local semistatic = "hi!"
for t=1,1000
  x = function() print(semistatic) end
end
 
However, upvalues that actually ''change'' use an additional 32 bytes. The below code will use 20000 + 4000 + 32000 = 56000 bytes:
  for t=1,1000
  for t=1,1000
   x = function() print(t) end
   x = function() print(t) end
  end
  end
And finally, a mix: The below code will use 20*2*500 + 4*2*500 + 32*500 = 40000 bytes
for t=1,'''500'''
  for i=1,2
    x = function() print(t) end
  end
end
== Quiz! ==
To motivate programmers, here follows a number of statements. Only one is correct. If you choose the right one, you will be shown boobies.
* [http://www.disney.com/ It is better to have lots of named keys in a table rather than create a sub-table with integer indices]
* [http://www.ratemyboobies.com/ It usually uses less CPU power to let the incremental GC handle tables compared to recycling them]
* [http://www.disney.com/ It is always better to use lots of upvalues to a function closure because locals don't use any memory]


[[Category:UI Technical Details]]
[[Category:UI Technical Details]]
Anonymous user