Skip to content

Commit

Permalink
perf: use timer instead of check for async executor
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 28, 2024
1 parent ab46edb commit f85575a
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lua/lazy/async.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ local M = {}

---@type Async[]
M._queue = {}
M._executor = assert(vim.loop.new_check())
M._running = false
M._executor = assert(vim.loop.new_timer())

M.TIMER = 10
M.BUDGET = 100

---@type table<thread, Async>
M._threads = setmetatable({}, { __mode = "k" })
Expand Down Expand Up @@ -68,11 +70,10 @@ end

---@async
function Async:sleep(ms)
self._suspended = true
vim.defer_fn(function()
self._suspended = false
self:resume()
end, ms)
coroutine.yield()
self:suspend()
end

---@async
Expand Down Expand Up @@ -120,12 +121,11 @@ function Async:step()
end

function M.step()
M._running = true
local budget = 1 * 1e6
local start = vim.loop.hrtime()
local budget = M.BUDGET * 1e6
local start = vim.uv.hrtime()
local count = #M._queue
local i = 0
while #M._queue > 0 and vim.loop.hrtime() - start < budget do
while #M._queue > 0 and vim.uv.hrtime() - start < budget do
---@type Async
local state = table.remove(M._queue, 1)
if state:step() then
Expand All @@ -136,7 +136,6 @@ function M.step()
break
end
end
M._running = false
if #M._queue == 0 then
return M._executor:stop()
end
Expand All @@ -146,7 +145,7 @@ end
function M.add(async)
table.insert(M._queue, async)
if not M._executor:is_active() then
M._executor:start(vim.schedule_wrap(M.step))
M._executor:start(1, M.TIMER, vim.schedule_wrap(M.step))
end
return async
end
Expand Down

0 comments on commit f85575a

Please sign in to comment.