From 2d8c21af850696e72176adf2ad07894374840800 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 29 Mar 2014 21:41:47 +0100 Subject: [PATCH] gears.surface: Cache files from disk Instead of loading files from disk every time we need them, add a cache to gears.surface as a weak table that maps strings to cairo surfaces. If this cache should be avoided, there is a new gears.surface.load_uncached() function which works just like gears.surface.load() worked before. Signed-off-by: Uli Schlachter --- lib/gears/surface.lua.in | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/gears/surface.lua.in b/lib/gears/surface.lua.in index 023703fd1..e3ea89415 100644 --- a/lib/gears/surface.lua.in +++ b/lib/gears/surface.lua.in @@ -17,14 +17,17 @@ end -- gears.surface local surface = { mt = {} } +local surface_cache = setmetatable({}, { __mode = 'v' }) --- Try to convert the argument into an lgi cairo surface. -- This is usually needed for loading images by file name. -function surface.load(_surface) +function surface.load_uncached(_surface) -- Nil is not changed if not _surface then return nil end + -- Remove from cache if it was cached + surface_cache[_surface] = nil -- lgi cairo surfaces don't get changed either if cairo.Surface:is_type_of(_surface) then return _surface @@ -37,6 +40,18 @@ function surface.load(_surface) return cairo.Surface(_surface, true) end +function surface.load(_surface) + if type(_surface) == "string" then + local cache = surface_cache[_surface] + if not cache then + cache = surface.load_uncached(_surface) + surface_cache[_surface] = cache + end + return cache + end + return surface.load_uncached(_surface) +end + function surface.mt:__call(...) return surface.load(...) end