perf(source): only filter up to 200 entries and dont use the cache (#1574)

This commit is contained in:
Folke Lemaitre
2023-05-16 07:00:44 +02:00
committed by GitHub
parent d153771162
commit 768548bf49

View File

@@ -18,6 +18,7 @@ local char = require('cmp.utils.char')
---@field public incomplete boolean ---@field public incomplete boolean
---@field public is_triggered_by_symbol boolean ---@field public is_triggered_by_symbol boolean
---@field public entries cmp.Entry[] ---@field public entries cmp.Entry[]
---@field public filtered {entries: cmp.Entry[], ctx: cmp.Context}
---@field public offset integer ---@field public offset integer
---@field public request_offset integer ---@field public request_offset integer
---@field public context cmp.Context ---@field public context cmp.Context
@@ -53,6 +54,7 @@ source.reset = function(self)
self.is_triggered_by_symbol = false self.is_triggered_by_symbol = false
self.incomplete = false self.incomplete = false
self.entries = {} self.entries = {}
self.filtered = {}
self.offset = -1 self.offset = -1
self.request_offset = -1 self.request_offset = -1
self.completion_context = nil self.completion_context = nil
@@ -88,6 +90,10 @@ source.get_entries = function(self, ctx)
return {} return {}
end end
if self.filtered.ctx and self.filtered.ctx.id == ctx.id then
return self.filtered.entries
end
local target_entries = (function() local target_entries = (function()
local key = { 'get_entries', self.revision } local key = { 'get_entries', self.revision }
for i = ctx.cursor.col, self.offset, -1 do for i = ctx.cursor.col, self.offset, -1 do
@@ -103,7 +109,9 @@ source.get_entries = function(self, ctx)
local entry_filter = self:get_entry_filter() local entry_filter = self:get_entry_filter()
local inputs = {} local inputs = {}
---@type cmp.Entry[]
local entries = {} local entries = {}
local max_item_count = self:get_source_config().max_item_count or 200
local matching_config = self:get_matching_config() local matching_config = self:get_matching_config()
for _, e in ipairs(target_entries) do for _, e in ipairs(target_entries) do
local o = e:get_offset() local o = e:get_offset()
@@ -119,21 +127,22 @@ source.get_entries = function(self, ctx)
e.exact = e:get_filter_text() == inputs[o] or e:get_word() == inputs[o] e.exact = e:get_filter_text() == inputs[o] or e:get_word() == inputs[o]
if entry_filter(e, ctx) then if entry_filter(e, ctx) then
table.insert(entries, e) entries[#entries + 1] = e
end if max_item_count and #entries >= max_item_count then
end
end
self.cache:set({ 'get_entries', tostring(self.revision), ctx.cursor_before_line }, entries)
local max_item_count = self:get_source_config().max_item_count or 200
local limited_entries = {}
for _, e in ipairs(entries) do
table.insert(limited_entries, e)
if max_item_count and #limited_entries >= max_item_count then
break break
end end
end end
return limited_entries end
end
-- only save to cache, when there are no additional entries that could match the filter
-- This also prevents too much memory usage
if #entries < max_item_count then
self.cache:set({ 'get_entries', tostring(self.revision), ctx.cursor_before_line }, entries)
end
self.filtered = { entries = entries, ctx = ctx }
return entries
end end
---Get default insert range (UTF8 byte index). ---Get default insert range (UTF8 byte index).