Skip to content

Commit f85575a

Browse files
committed
perf: use timer instead of check for async executor
1 parent ab46edb commit f85575a

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

lua/lazy/async.lua

+10-11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ local M = {}
22

33
---@type Async[]
44
M._queue = {}
5-
M._executor = assert(vim.loop.new_check())
6-
M._running = false
5+
M._executor = assert(vim.loop.new_timer())
6+
7+
M.TIMER = 10
8+
M.BUDGET = 100
79

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

6971
---@async
7072
function Async:sleep(ms)
71-
self._suspended = true
7273
vim.defer_fn(function()
73-
self._suspended = false
74+
self:resume()
7475
end, ms)
75-
coroutine.yield()
76+
self:suspend()
7677
end
7778

7879
---@async
@@ -120,12 +121,11 @@ function Async:step()
120121
end
121122

122123
function M.step()
123-
M._running = true
124-
local budget = 1 * 1e6
125-
local start = vim.loop.hrtime()
124+
local budget = M.BUDGET * 1e6
125+
local start = vim.uv.hrtime()
126126
local count = #M._queue
127127
local i = 0
128-
while #M._queue > 0 and vim.loop.hrtime() - start < budget do
128+
while #M._queue > 0 and vim.uv.hrtime() - start < budget do
129129
---@type Async
130130
local state = table.remove(M._queue, 1)
131131
if state:step() then
@@ -136,7 +136,6 @@ function M.step()
136136
break
137137
end
138138
end
139-
M._running = false
140139
if #M._queue == 0 then
141140
return M._executor:stop()
142141
end
@@ -146,7 +145,7 @@ end
146145
function M.add(async)
147146
table.insert(M._queue, async)
148147
if not M._executor:is_active() then
149-
M._executor:start(vim.schedule_wrap(M.step))
148+
M._executor:start(1, M.TIMER, vim.schedule_wrap(M.step))
150149
end
151150
return async
152151
end

0 commit comments

Comments
 (0)