From eb41569e8d536dec276d45f5a162c1c00cb38a6b Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Sat, 14 Nov 2020 23:47:39 +0100 Subject: [PATCH] Implement limits for maximum number of symbols in autocomplete Implemented to avoid excessive memory usage when loading big files with many unique words. --- data/core/config.lua | 1 + data/plugins/autocomplete.lua | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/data/core/config.lua b/data/core/config.lua index a9f106f6..0115cab8 100644 --- a/data/core/config.lua +++ b/data/core/config.lua @@ -16,5 +16,6 @@ config.line_height = 1.2 config.indent_size = 2 config.tab_type = "soft" config.line_limit = 80 +config.max_symbols = 2000 return config diff --git a/data/plugins/autocomplete.lua b/data/plugins/autocomplete.lua index 79e45133..7a43fd17 100644 --- a/data/plugins/autocomplete.lua +++ b/data/plugins/autocomplete.lua @@ -25,16 +25,28 @@ function autocomplete.add(t) autocomplete.map[t.name] = { files = t.files or ".*", items = items } end +local max_symbols = config.max_symbols or 2000 core.add_thread(function() local cache = setmetatable({}, { __mode = "k" }) local function get_symbols(doc) + if doc.disable_symbols then return {} end local i = 1 local s = {} + local symbols_count = 0 while i < #doc.lines do for sym in doc.lines[i]:gmatch(config.symbol_pattern) do - s[sym] = true + 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 + end end i = i + 1 if i % 100 == 0 then coroutine.yield() end