View Single Post
08-26-13, 11:11 PM   #24
kurapica.igas
A Chromatic Dragonspawn
Join Date: Aug 2011
Posts: 152
Oh, one more thing, about the memory usage, the up-values cost much more if you use the local vars in many functions in your add-on, Here is a little test :

Lua Code:
  1. do
  2.     clear = function() collectgarbage() return collectgarbage("count") end
  3.  
  4.     for i = 1, 10 do
  5.         _G["a"..i] = function() end
  6.     end
  7.  
  8.     memStep0 = clear()
  9.  
  10.     do
  11.         local a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 = a1, a2, a3, a4, a5, a6, a7, a8, a9, a10
  12.  
  13.         for i = 1, 100 do
  14.             _G["A"..i] = function()
  15.                 a1() a2() a3() a4() a5() a6() a7() a8() a9() a10()
  16.             end
  17.  
  18.             _G["A"..i]()
  19.         end
  20.     end
  21.  
  22.     memStep1 = clear()
  23.  
  24.     do
  25.         local addon = {}
  26.         addon._Addon = addon  -- the addon itself
  27.  
  28.         if not getmetatable(addon) then
  29.             setmetatable(addon, {
  30.             __index = function(self,  key)
  31.                 -- keep vars in the _G to the addon to reduce the access cost
  32.                 local v = _G[key]
  33.                 if v ~= nil then
  34.                     rawset(self, key, v)
  35.                     return rawget(self, key)
  36.                 end
  37.             end,
  38.  
  39.             __metatable = true,
  40.             })
  41.         end
  42.  
  43.         setfenv(1, addon)
  44.  
  45.         for i = 1, 100 do
  46.             _Addon["A"..i] = function()
  47.                 a1() a2() a3() a4() a5() a6() a7() a8() a9() a10()
  48.             end
  49.  
  50.             _Addon["A"..i]()
  51.         end
  52.     end
  53.  
  54.     memStep2 = clear()
  55.  
  56.     print("Cost for up-values", memStep1 - memStep0)
  57.     print("Cost for standalone environment", memStep2 - memStep1)
  58. end

So, here is the result:

Cost for up-values 22.275390625
Cost for standalone environment 9.9453125
  Reply With Quote