From f3dc57f3f4953d2a47f30ef899fb500d96f1e3b3 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Wed, 7 Aug 2019 03:52:18 -0400 Subject: [PATCH] layoutlist: Add more caching. Technically this doesn't solve any memory leak, but AwesomeWM uses in average less memory when changing the selected tab in quick succession. This is because it has less "temporary" tables to track. --- lib/awful/widget/layoutlist.lua | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/awful/widget/layoutlist.lua b/lib/awful/widget/layoutlist.lua index 8458cebb1..1241bf5e7 100644 --- a/lib/awful/widget/layoutlist.lua +++ b/lib/awful/widget/layoutlist.lua @@ -118,8 +118,12 @@ function module.source.default_layouts() return alayout.layouts 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) - self._private.cache = {} + self._private.cache = setmetatable({}, {__mode = "v"}) local show_text = (not self._private.style.disable_name) and (not beautiful.layoutlist_disable_name) @@ -134,18 +138,22 @@ local function reload_cache(self) for _, l in ipairs(ls or {}) do 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) end - table.insert(self._private.cache, { - icon = show_icon and icon or nil, - name = show_text and l.name or nil, + l_cache[l] = l_cache[l] or { layout = l, - screen = s, callback = function() alayout.set(l) end, 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