From 2f4ac035bcc66292250de7134d73007b147f64e8 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 30 Jun 2024 09:13:04 +0200 Subject: [PATCH] perf: suspend when tasks are active --- lua/lazy/async.lua | 19 ++++++++++++++++++- lua/lazy/manage/runner.lua | 4 +++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lua/lazy/async.lua b/lua/lazy/async.lua index 7848b7d..5cbdb1b 100644 --- a/lua/lazy/async.lua +++ b/lua/lazy/async.lua @@ -152,12 +152,29 @@ function M.step() table.insert(state._suspended and M._suspended or M._active, state) end - -- print("step", #M._active, #M._suspended) + -- M.debug() if #M._active == 0 then return M._executor:stop() end end +function M.debug() + local lines = { + "- active: " .. #M._active, + "- suspended: " .. #M._suspended, + } + for _, async in ipairs(M._active) do + local info = debug.getinfo(async._fn) + local file = vim.fn.fnamemodify(info.short_src:sub(1), ":~:.") + table.insert(lines, ("%s:%d"):format(file, info.linedefined)) + if #lines > 10 then + break + end + end + local msg = table.concat(lines, "\n") + M._notif = vim.notify(msg, nil, { replace = M._notif }) +end + ---@param async Async function M.add(async) table.insert(M._active, async) diff --git a/lua/lazy/manage/runner.lua b/lua/lazy/manage/runner.lua index d551ce5..81dcde0 100644 --- a/lua/lazy/manage/runner.lua +++ b/lua/lazy/manage/runner.lua @@ -153,7 +153,9 @@ function Runner:_start() end continue(true) end - coroutine.yield() + if active > 0 then + self._running:suspend() + end end end