From 0ccf3647a2c8213be6d53fa343f89fd656af9565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 12 Oct 2022 19:16:05 +0800 Subject: resolve performance issue by cache require-path fix #1595 --- script/workspace/require-path.lua | 40 ++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'script/workspace/require-path.lua') diff --git a/script/workspace/require-path.lua b/script/workspace/require-path.lua index 02b1996d..968511f2 100644 --- a/script/workspace/require-path.lua +++ b/script/workspace/require-path.lua @@ -13,6 +13,7 @@ local m = {} ---@field scp scope ---@field nameMap table ---@field visibleCache table +---@field requireCache table local mt = {} mt.__index = mt @@ -25,6 +26,7 @@ local function createRequireManager(scp) scp = scp, nameMap = {}, visibleCache = {}, + requireCache = {}, }, mt) end @@ -153,14 +155,10 @@ function mt:getVisiblePath(path) end --- 查找符合指定require name的所有uri ----@param suri uri ---@param name string ---@return uri[] ----@return table? -function mt:findUrisByRequireName(suri, name) - if type(name) ~= 'string' then - return {} - end +---@return table +function mt:searchUrisByRequireName(name) local searchers = config.get(self.scp.uri, 'Lua.runtime.path') local strict = config.get(self.scp.uri, 'Lua.runtime.pathStrict') local separator = config.get(self.scp.uri, 'Lua.completion.requireSeparator') @@ -174,7 +172,6 @@ function mt:findUrisByRequireName(suri, name) local tail = '/' .. furi.encode(fspath):gsub('^file:[/]*', '') for uri in files.eachFile(self.scp.uri) do if not searcherMap[uri] - and suri ~= uri and util.stringEndWith(uri, tail) then local parentUri = files.getLibraryUri(self.scp.uri, uri) or self.scp.uri if parentUri == nil or parentUri == '' then @@ -203,6 +200,35 @@ function mt:findUrisByRequireName(suri, name) return results, searcherMap end +--- 查找符合指定require name的所有uri,并排除当前文件 +---@param suri uri +---@param name string +---@return uri[] +---@return table? +function mt:findUrisByRequireName(suri, name) + if type(name) ~= 'string' then + return {} + end + local cache = self.requireCache[name] + if not cache then + local results, searcherMap = self:searchUrisByRequireName(name) + cache = { + results = results, + searcherMap = searcherMap, + } + self.requireCache[name] = cache + end + local results = {} + local searcherMap = {} + for _, uri in ipairs(cache.results) do + if uri ~= suri then + results[#results+1] = uri + searcherMap[uri] = cache.searcherMap[uri] + end + end + return results, searcherMap +end + ---@param uri uri ---@param path string ---@return require-manager.visibleResult[] -- cgit v1.2.3