From 338d14edb78c94bc528baf0bf20c5af1493a9f43 Mon Sep 17 00:00:00 2001 From: poire-z Date: Thu, 16 Nov 2017 14:04:59 +0100 Subject: [PATCH] Speedup dictionaries init (#3489) When looking for .ifo to populate Dictionary settings menu, avoid walking sub-directories once we found a .ifo, as these sub-directories may contain a lot of images and other subdirs. --- .../apps/reader/modules/readerdictionary.lua | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/frontend/apps/reader/modules/readerdictionary.lua b/frontend/apps/reader/modules/readerdictionary.lua index 027095a4d..2bea3c360 100644 --- a/frontend/apps/reader/modules/readerdictionary.lua +++ b/frontend/apps/reader/modules/readerdictionary.lua @@ -27,25 +27,35 @@ local function getIfosInDir(path) -- - No sorting, entries are processed in the order the dir_read_name() call -- returns them (inodes linked list) -- - If entry is a directory, Walk in it first and recurse + -- With a small optimisation to avoid walking dict subdirectories that + -- may contain many images: if .ifo found, don't recurse subdirectories. local ifos = {} local ok, iter, dir_obj = pcall(lfs.dir, path) if ok then + local ifo_found = false + local subdirs = {} for name in iter, dir_obj do if name ~= "." and name ~= ".." then local fullpath = path.."/"..name - local attributes = lfs.attributes(fullpath) - if attributes ~= nil then - if attributes.mode == "directory" then - local dirifos = getIfosInDir(fullpath) -- recurse - for _, ifo in pairs(dirifos) do - table.insert(ifos, ifo) - end - elseif fullpath:match("%.ifo$") then - table.insert(ifos, fullpath) + if fullpath:match("%.ifo$") then + table.insert(ifos, fullpath) + ifo_found = true + elseif not ifo_found then -- don't check for dirs anymore if we found one .ifo + local attributes = lfs.attributes(fullpath) + if attributes ~= nil and attributes.mode == "directory" then + table.insert(subdirs, fullpath) end end end end + if not ifo_found and #subdirs > 0 then + for _, subdir in pairs(subdirs) do + local dirifos = getIfosInDir(subdir) + for _, ifo in pairs(dirifos) do + table.insert(ifos, ifo) + end + end + end end return ifos end