From 63d7acd686da35be0227af8ccf709bc3a7f54d90 Mon Sep 17 00:00:00 2001 From: iguanacucumber Date: Sun, 13 Oct 2024 17:13:07 +0400 Subject: [PATCH] @ perf: avoid source slowdown due to unnecessary context switches #2060 --- lua/cmp/source.lua | 13 +++++++++---- merged.md | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lua/cmp/source.lua b/lua/cmp/source.lua index 7f41458..7b2fd5f 100644 --- a/lua/cmp/source.lua +++ b/lua/cmp/source.lua @@ -128,7 +128,7 @@ source.get_entries = function(self, ctx) ---@type cmp.Entry[] local entries = {} local matching_config = self:get_matching_config() - for _, e in ipairs(target_entries) do + for i, e in ipairs(target_entries) do local o = e.offset if not inputs[o] then inputs[o] = string.sub(ctx.cursor_before_line, o) @@ -145,9 +145,14 @@ source.get_entries = function(self, ctx) entries[#entries + 1] = e end end - async.yield() - if ctx.aborted then - async.abort() + + -- Yield to event loop once in a while to avoid UI freezes + -- But avoid yielding too often causing too many context switches + if i % 1000 == 999 then + async.yield() + if ctx.aborted then + async.abort() + end end end diff --git a/merged.md b/merged.md index 993f8cf..ddf4d33 100644 --- a/merged.md +++ b/merged.md @@ -5,6 +5,7 @@ > Therefore these PRs won't have a patch file for them in the PRs folder (this folder contains unedited patch files from PRs from nvim-cmp) ## Perf +- @ perf: avoid source slowdown due to unnecessary context switches #2060 - perf: improve for source providing huge list of items #1980 - perf: use builtin for key normalization #1935 - Do not use sync code in InserEnter #1911