summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-01-21 16:59:17 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-01-21 16:59:17 +0800
commit3778e5eeb3ffe35e5a5f70700635a90970a2ee40 (patch)
tree5c9923a155edd332778c7e2318f73c42b9d5fb65
parent021ceef2e05c781e849e1bac86eaae6e36775cf9 (diff)
downloadlua-language-server-3778e5eeb3ffe35e5a5f70700635a90970a2ee40.zip
Avoid O(n^2) `require` computations
-rw-r--r--changelog.md1
-rw-r--r--script/core/collector.lua3
-rw-r--r--script/core/completion/completion.lua2
-rw-r--r--script/workspace/require-path.lua50
4 files changed, 39 insertions, 17 deletions
diff --git a/changelog.md b/changelog.md
index 88ab37f4..42fdaacb 100644
--- a/changelog.md
+++ b/changelog.md
@@ -5,6 +5,7 @@
+ `Lua.diagnostics.workspaceDelay`: `0` sec -> `3` sec
+ `Lua.workspace.maxPreload`: `1000` -> `5000`
+ `Lua.workspace.preloadFileSize`: `100` KB -> `500` KB
+* `CHG` improve performance
* `FIX` modify luarc failed
* `FIX` library files not recognized correctly
* `FIX` [#906](https://github.com/sumneko/lua-language-server/issues/906)
diff --git a/script/core/collector.lua b/script/core/collector.lua
index 62ca5a7d..1abfc51b 100644
--- a/script/core/collector.lua
+++ b/script/core/collector.lua
@@ -42,6 +42,9 @@ function mt:dropUri(uri)
self.subscribed[uri] = nil
for name in pairs(uriSubscribed) do
self.collect[name][uri] = nil
+ if not next(self.collect[name]) then
+ self.collect[name] = nil
+ end
end
end
diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua
index 124ebee1..9eb33c2a 100644
--- a/script/core/completion/completion.lua
+++ b/script/core/completion/completion.lua
@@ -884,7 +884,7 @@ local function collectRequireNames(mode, myUri, literal, source, smark, position
if myUri == uri then
goto CONTINUE
end
- local path = workspace.getRelativePath(uri)
+ local path = furi.decode(uri)
local infos = rpath.getVisiblePath(uri, path)
for _, info in ipairs(infos) do
if matchKey(literal, info.expect) then
diff --git a/script/workspace/require-path.lua b/script/workspace/require-path.lua
index 577da85e..874c6743 100644
--- a/script/workspace/require-path.lua
+++ b/script/workspace/require-path.lua
@@ -3,7 +3,7 @@ local files = require 'files'
local furi = require 'file-uri'
local workspace = require "workspace"
local config = require 'config'
-local collector = require 'core.collector' 'require-path'
+local collector = require 'core.collector'
---@class require-path
local m = {}
@@ -11,7 +11,10 @@ local m = {}
local function addRequireName(suri, uri, name)
local separator = config.get(uri, 'Lua.completion.requireSeparator')
local fsname = name:gsub('%' .. separator, '/')
- collector:subscribe(suri, fsname, uri)
+ local scp = workspace.getScope(suri)
+ ---@type collector
+ local clt = scp:get('requireName') or scp:set('requireName', collector())
+ clt:subscribe(uri, fsname, name)
end
--- `aaa/bbb/ccc.lua` 与 `?.lua` 将返回 `aaa.bbb.cccc`
@@ -110,13 +113,17 @@ function m.findUrisByRequirePath(suri, path)
end
end
- for uri in collector:each(suri, fspath) do
- local infos = m.getVisiblePath(suri, furi.decode(uri))
- for _, info in ipairs(infos) do
- local fsexpect = info.expect:gsub('%' .. separator, '/')
- if fsexpect == fspath then
- results[#results+1] = uri
- searchers[uri] = info.searcher
+ ---@type collector
+ local clt = workspace.getScope(suri):get('requireName')
+ if clt then
+ for _, uri in clt:each(suri, fspath) do
+ local infos = m.getVisiblePath(suri, furi.decode(uri))
+ for _, info in ipairs(infos) do
+ local fsexpect = info.expect:gsub('%' .. separator, '/')
+ if fsexpect == fspath then
+ results[#results+1] = uri
+ searchers[uri] = info.searcher
+ end
end
end
end
@@ -132,12 +139,27 @@ local function createVisiblePath(uri)
m.getVisiblePath(nil, furi.decode(uri))
end
+local function removeVisiblePath(uri)
+ local path = furi.decode(uri)
+ path = workspace.normalize(path)
+ for _, scp in ipairs(workspace.folders) do
+ scp:get('visiblePath')[path] = nil
+ scp:get('requireName'):dropUri(uri)
+ end
+ workspace.getScope(nil):get('visiblePath')[path] = nil
+ workspace.getScope(nil):get('requireName'):dropUri(uri)
+end
+
function m.flush(suri)
local scp = workspace.getScope(suri)
scp:set('visiblePath', {})
- collector:dropAll()
+ ---@type collector
+ local clt = scp:get('requireName')
+ if clt then
+ clt:dropAll()
+ end
for uri in files.eachFile() do
- createVisiblePath(uri)
+ m.getVisiblePath(scp.uri, furi.decode(uri))
end
end
@@ -148,11 +170,7 @@ files.watch(function (ev, uri)
createVisiblePath(uri)
end
if ev == 'remove' then
- for _, scp in ipairs(workspace.folders) do
- scp:get('visiblePath')[uri] = nil
- end
- workspace.getScope(nil):get('visiblePath')[uri] = nil
- collector:dropUri(uri)
+ removeVisiblePath(uri)
end
end)