vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1  
2 --[[
3 Name: Compost-2.0
4 Revision: $Rev: 6641 $
5 Author: Tekkub Stoutwrithe (tekkub@gmail.com)
6 Website: http://wiki.wowace.com/index.php/CompostLib
7 Documentation: http://wiki.wowace.com/index.php/Compost-2.0_API_Documentation
8 SVN: svn://svn.wowace.com/root/trunk/CompostLib/Compost-2.0
9 Description: Recycle tables to reduce garbage generation
10 Dependencies: AceLibrary
11 ]]
12  
13 local vmajor, vminor = "Compost-2.0", "$Revision: 6641 $"
14  
15 if not AceLibrary then error(vmajor .. " requires AceLibrary.") end
16 if not AceLibrary:IsNewVersion(vmajor, vminor) then return end
17  
18 local lib = {}
19  
20  
21 -- Activate a new instance of this library
22 local function activate(self, oldLib, oldDeactivate)
23 if oldLib then -- if upgrading
24 self.var, self.k = oldLib.var, oldLib.k
25 else
26 self.k = { -- Constants go here
27 maxcache = 10, -- I think this is a good number, I'll change it later if necessary
28 }
29 self.var = { -- "Local" variables go here
30 cache = {},
31 secondarycache = {},
32 }
33  
34 -- This makes the secondary cache table a weak table, any values in it will be reclaimed
35 -- during a GC if there are no other references to them
36 setmetatable(self.var.secondarycache, {__mode = "v"})
37 end
38 if not self.var.tablechecks then
39 self.var.tablechecks = {}
40 setmetatable(self.var.tablechecks, {__mode = "kv"})
41 for i,v in ipairs(self.var.cache) do self.var.tablechecks[v] = true end
42 for i,v in ipairs(self.var.secondarycache) do self.var.tablechecks[v] = true end
43 end
44 if oldDeactivate then oldDeactivate(oldLib) end
45 end
46  
47  
48 -- Removes an empty table from the cache and returns it
49 -- or generates a new table if none available
50 function lib:GetTable()
51 if self.var.disabled then return {} end
52  
53 if table.getn(self.var.cache) > 0 then
54 for i in pairs(self.var.cache) do
55 local t = table.remove(self.var.cache, i)
56 self.var.tablechecks[t] = nil
57 if next(t) then -- Table has been modified, someone holds a ref still, discard it
58 error("Someone is modifying tables reclaimed by Compost!")
59 self:IncDec("numdiscarded", 1)
60 else -- It's clean, we think... return it.
61 self:IncDec("totn", -1)
62 self:IncDec("numrecycled", 1)
63 return t
64 end
65 end
66 end
67  
68 if next(self.var.secondarycache) then
69 for i in pairs(self.var.secondarycache) do
70 local t = table.remove(self.var.secondarycache, i)
71 self.var.tablechecks[t] = nil
72 if next(t) then -- Table has been modified, someone holds a ref still, discard it
73 error("Someone is modifying tables reclaimed by Compost!")
74 self:IncDec("numdiscarded", 1)
75 else -- It's clean, we think... return it.
76 self:IncDec("totn", -1)
77 self:IncDec("numrecycled", 1)
78 return t
79 end
80 end
81 end
82  
83 self:IncDec("numnew", 1)
84 return {}
85 end
86  
87  
88 -- Returns a table, populated with any variables passed
89 -- basically: return {a1, a2, ... a20}
90 function lib:Acquire(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20)
91 local t = self:GetTable()
92 return self:Populate(t,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20)
93 end
94  
95  
96 -- Acquires a table and fills it with values, hash style
97 -- basically: return {k1 = v1, k2 = v2, ... k10 = v10}
98 function lib:AcquireHash(k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8,v8,k9,v9,k10,v10)
99 local t = self:GetTable()
100 return self:PopulateHash(t,k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8,v8,k9,v9,k10,v10)
101 end
102  
103  
104 -- Erases the table passed, fills it with the args passed, and returns it
105 -- Essentially the same as doing Reclaim then Acquire, except the same table is reused
106 function lib:Recycle(t,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20)
107 t = self:Erase(t)
108 return self:Populate(t,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20)
109 end
110  
111  
112 -- Erases the table passed, fills it with the args passed, and returns it
113 -- Essentially the same as doing Reclaim then AcquireHash, except the same table is reused
114 function lib:RecycleHash(t,k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8,v8,k9,v9,k10,v10)
115 t = self:Erase(t)
116 return self:PopulateHash(t,k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8,v8,k9,v9,k10,v10)
117 end
118  
119  
120 -- Returns a table to the cache
121 -- All tables referenced inside the passed table will be reclaimed also
122 -- If a depth is passed, Reclaim will call itsself recursivly
123 -- to reclaim all tables contained in t to the depth specified
124 function lib:Reclaim(t, depth)
125 if type(t) ~= "table" or self.var.disabled then return end
126 self:assert(not self.var.tablechecks[t], "Cannot reclaim a table twice")
127  
128 if not self:ItemsInSecondaryCache() then self.var.totn = table.getn(self.var.cache) end
129  
130 if depth and depth > 0 then
131 for i in pairs(t) do
132 if type(t[i]) == "table" then self:Reclaim(t[i], depth - 1) end
133 end
134 end
135 self:Erase(t)
136 if self.k.maxcache and table.getn(self.var.cache) >= self.k.maxcache then
137 table.insert(self.var.secondarycache, t)
138 else
139 table.insert(self.var.cache, t)
140 end
141 self:IncDec("numreclaim", 1)
142 self:IncDec("totn", 1)
143 self.var.maxn = math.max(self.var.maxn or 0, self.var.totn)
144 self.var.tablechecks[t] = true
145 end
146  
147  
148 -- Reclaims multiple tables, can take 10 recursive sets or 20 non-recursives,
149 -- or any combination of the two. Pass args in the following manner:
150 -- table1, depth1, tabl2, depth2, table3, table4, table5, depth5, ...
151 function lib:ReclaimMulti(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20)
152 if not a1 then return end
153 if type(a2) == "number" then
154 self:Reclaim(a1, a2)
155 self:ReclaimMulti(a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20)
156 else
157 self:Reclaim(a1)
158 self:ReclaimMulti(a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20)
159 end
160 end
161  
162  
163 -- Erases the table passed, nothing more nothing less :)
164 -- Tables referenced inside the passed table are NOT erased
165 function lib:Erase(t)
166 if type(t) ~= "table" then return end
167 if self.var.disabled then return {} end
168 local mem = gcinfo()
169 setmetatable(t, nil)
170 for i in pairs(t) do
171 t[i] = nil
172 end
173 t.reset = 1
174 t.reset = nil
175 table.setn(t, 0)
176 self:IncDec("memfreed", math.abs(gcinfo() - mem))
177 self:IncDec("numerased", 1)
178 return t
179 end
180  
181  
182 -- Fills the table passed with the args passed
183 function lib:Populate(t,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20)
184 if not t then return end
185 if a1 ~= nil then table.insert(t, a1) end
186 if a2 ~= nil then table.insert(t, a2) end
187 if a3 ~= nil then table.insert(t, a3) end
188 if a4 ~= nil then table.insert(t, a4) end
189 if a5 ~= nil then table.insert(t, a5) end
190 if a6 ~= nil then table.insert(t, a6) end
191 if a7 ~= nil then table.insert(t, a7) end
192 if a8 ~= nil then table.insert(t, a8) end
193 if a9 ~= nil then table.insert(t, a9) end
194 if a10 ~= nil then table.insert(t, a10) end
195 if a11 ~= nil then table.insert(t, a11) end
196 if a12 ~= nil then table.insert(t, a12) end
197 if a13 ~= nil then table.insert(t, a13) end
198 if a14 ~= nil then table.insert(t, a14) end
199 if a15 ~= nil then table.insert(t, a15) end
200 if a16 ~= nil then table.insert(t, a16) end
201 if a17 ~= nil then table.insert(t, a17) end
202 if a18 ~= nil then table.insert(t, a18) end
203 if a19 ~= nil then table.insert(t, a19) end
204 if a20 ~= nil then table.insert(t, a20) end
205 return t
206 end
207  
208  
209 -- Same as Populate, but takes 10 key-value pairs instead
210 function lib:PopulateHash(t,k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8,v8,k9,v9,k10,v10)
211 if not t then return end
212 if k1 ~= nil then t[k1] = v1 end
213 if k2 ~= nil then t[k2] = v2 end
214 if k3 ~= nil then t[k3] = v3 end
215 if k4 ~= nil then t[k4] = v4 end
216 if k5 ~= nil then t[k5] = v5 end
217 if k6 ~= nil then t[k6] = v6 end
218 if k7 ~= nil then t[k7] = v7 end
219 if k8 ~= nil then t[k8] = v8 end
220 if k9 ~= nil then t[k9] = v9 end
221 if k10 ~= nil then t[k10] = v10 end
222 return t
223 end
224  
225  
226 function lib:IncDec(variable, diff)
227 self.var[variable] = (self.var[variable] or 0) + diff
228 end
229  
230  
231 function lib:ItemsInSecondaryCache()
232 for i in pairs(self.var.secondarycache) do return true end
233 end
234  
235  
236 function lib:GetSecondaryCacheSize()
237 local n = 0
238 for i in pairs(self.var.secondarycache) do n = n + 1 end
239 return n
240 end
241  
242  
243 -- Prints out statistics on table recycling
244 -- /script CompostLib:GetInstance("compost-1"):Stats()
245 function lib:Stats()
246 if self.var.disabled then ChatFrame1:AddMessage("CompostLib is disabled!")
247 else ChatFrame1:AddMessage(
248 string.format(
249 "|cff00ff00New: %d|r | |cffffff00Recycled: %d|r | |cff00ffffMain: %d|r | |cffff0000Secondary: %d|r | |cffff8800Max %d|r | |cff888888Erases: %d|r | |cffff00ffMem Saved: %d KiB|r | |cffff0088Lost to GC: %d",
250 self.var.numnew or 0,
251 self.var.numrecycled or 0,
252 table.getn(self.var.cache),
253 self:GetSecondaryCacheSize(),
254 self.var.maxn or 0,
255 (self.var.numerased or 0) - (self.var.numreclaim or 0),
256 (self.var.memfreed or 0) + 32/1024*(self.var.numrecycled or 0),
257 (self.var.numreclaim or 0) - (self.var.numrecycled or 0) - table.getn(self.var.cache)))
258 end
259 end
260  
261 setmetatable(lib, { __call = lib.Acquire })
262  
263 --------------------------------
264 -- Load this bitch! --
265 --------------------------------
266 AceLibrary:Register(lib, vmajor, vminor, activate)
267 lib = nil