Implement limits for maximum number of symbols in autocomplete

Implemented to avoid excessive memory usage when loading big files with
many unique words.
This commit is contained in:
Francesco Abbate 2020-11-14 23:47:39 +01:00
parent abad5cce0f
commit eb41569e8d
2 changed files with 14 additions and 1 deletions

View File

@ -16,5 +16,6 @@ config.line_height = 1.2
config.indent_size = 2 config.indent_size = 2
config.tab_type = "soft" config.tab_type = "soft"
config.line_limit = 80 config.line_limit = 80
config.max_symbols = 2000
return config return config

View File

@ -25,17 +25,29 @@ function autocomplete.add(t)
autocomplete.map[t.name] = { files = t.files or ".*", items = items } autocomplete.map[t.name] = { files = t.files or ".*", items = items }
end end
local max_symbols = config.max_symbols or 2000
core.add_thread(function() core.add_thread(function()
local cache = setmetatable({}, { __mode = "k" }) local cache = setmetatable({}, { __mode = "k" })
local function get_symbols(doc) local function get_symbols(doc)
if doc.disable_symbols then return {} end
local i = 1 local i = 1
local s = {} local s = {}
local symbols_count = 0
while i < #doc.lines do while i < #doc.lines do
for sym in doc.lines[i]:gmatch(config.symbol_pattern) do for sym in doc.lines[i]:gmatch(config.symbol_pattern) do
if not s[sym] then
symbols_count = symbols_count + 1
if symbols_count > max_symbols then
s = nil
doc.disable_symbols = true
collectgarbage('collect')
return {}
end
s[sym] = true s[sym] = true
end end
end
i = i + 1 i = i + 1
if i % 100 == 0 then coroutine.yield() end if i % 100 == 0 then coroutine.yield() end
end end