Solve #182 add table pool for events

This commit is contained in:
Insality
2022-04-05 18:57:40 +03:00
parent 2779f9cf7a
commit dff522fbaa
24 changed files with 244 additions and 6 deletions

View File

@@ -289,6 +289,7 @@ function DruidInstance.remove(self, component)
local all_components = self.components_all
for i = #all_components, 1, -1 do
if all_components[i] == component then
component:on_internal_remove()
if component.on_remove then
component:on_remove()
end

View File

@@ -0,0 +1,75 @@
-- Source: https://github.com/openresty/lua-tablepool/blob/master/lib/tablepool.lua
local setmetatable = setmetatable
local _M = {}
local max_pool_size = 500
local pools = {}
function _M.fetch(tag)
local pool = pools[tag]
if not pool then
pool = {}
pools[tag] = pool
pool.c = 0
pool[0] = 0
else
local len = pool[0]
if len > 0 then
local obj = pool[len]
pool[len] = nil
pool[0] = len - 1
return obj
end
end
return {}
end
function _M.release(tag, obj, noclear)
if not obj then
error("object empty", 2)
end
local pool = pools[tag]
if not pool then
pool = {}
pools[tag] = pool
pool.c = 0
pool[0] = 0
end
if not noclear then
setmetatable(obj, nil)
for k in pairs(obj) do
obj[k] = nil
end
end
do
local cnt = pool.c + 1
if cnt >= 20000 then
pool = {}
pools[tag] = pool
pool.c = 0
pool[0] = 0
return
end
pool.c = cnt
end
local len = pool[0] + 1
if len > max_pool_size then
-- discard it simply
return
end
pool[len] = obj
pool[0] = len
end
return _M