Merge pull request #2847 from Elv13/layoutlist_memleak

layoutlist: Use weak tables to store the cache.
This commit is contained in:
Emmanuel Lepage Vallée 2019-08-08 23:54:37 -07:00 committed by GitHub
commit 794da2abec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 9 deletions

View File

@ -118,8 +118,12 @@ function module.source.default_layouts()
return alayout.layouts return alayout.layouts
end end
-- Keep the object for each layout. This avoid creating little new tables in
-- each update, which is painful for the GC.
local l_cache = setmetatable({}, {__mode = "k"})
local function reload_cache(self) local function reload_cache(self)
self._private.cache = {} self._private.cache = setmetatable({}, {__mode = "v"})
local show_text = (not self._private.style.disable_name) and local show_text = (not self._private.style.disable_name) and
(not beautiful.layoutlist_disable_name) (not beautiful.layoutlist_disable_name)
@ -134,18 +138,22 @@ local function reload_cache(self)
for _, l in ipairs(ls or {}) do for _, l in ipairs(ls or {}) do
local icn_path, icon = beautiful["layout_" .. (l.name or "")] local icn_path, icon = beautiful["layout_" .. (l.name or "")]
if icn_path then if icn_path and show_icon then
icon = surface.load(icn_path) icon = surface.load(icn_path)
end end
table.insert(self._private.cache, { l_cache[l] = l_cache[l] or {
icon = show_icon and icon or nil,
name = show_text and l.name or nil,
layout = l, layout = l,
screen = s,
callback = function() alayout.set(l) end, callback = function() alayout.set(l) end,
style = self._private.style, style = self._private.style,
}) }
-- Update the entry.
l_cache[l].icon = show_icon and icon or nil
l_cache[l].name = show_text and l.name or nil
l_cache[l].screen = s
table.insert(self._private.cache, l_cache[l])
end end
end end
@ -327,7 +335,7 @@ function layoutlist:set_widget_template(widget_template)
self._private.widget_template = widget_template self._private.widget_template = widget_template
-- Remove the existing instances -- Remove the existing instances
self._private.data = {} self._private.data = setmetatable({}, { __mode = 'k' })
-- Prevent a race condition when the constructor loop to initialize the -- Prevent a race condition when the constructor loop to initialize the
-- arguments. -- arguments.
@ -403,7 +411,7 @@ local function new(_, args)
ret._private.style = args.style or {} ret._private.style = args.style or {}
ret._private.buttons = args.buttons ret._private.buttons = args.buttons
ret._private.source = args.source ret._private.source = args.source
ret._private.data = {} ret._private.data = setmetatable({}, { __mode = 'k' })
reload_cache(ret) reload_cache(ret)