feat(performance): Major performance improvements using async v2 from @oberblastmeister (#987)
* start: Working w/ async jobs * short circuit to using bad finder if you pass writer.
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
local log = require "telescope.log"
|
||||
local Job = require "plenary.job"
|
||||
|
||||
local async_lib = require "plenary.async_lib"
|
||||
local async = async_lib.async
|
||||
-- local await = async_lib.await
|
||||
local void = async_lib.void
|
||||
local async_job = require "telescope._"
|
||||
local LinesPipe = require("telescope._").LinesPipe
|
||||
|
||||
local make_entry = require "telescope.make_entry"
|
||||
local log = require "telescope.log"
|
||||
|
||||
return function(opts)
|
||||
log.trace("Creating async_job:", opts)
|
||||
local entry_maker = opts.entry_maker or make_entry.gen_from_string()
|
||||
local fn_command = function(prompt)
|
||||
local command_list = opts.command_generator(prompt)
|
||||
@@ -18,58 +15,61 @@ return function(opts)
|
||||
|
||||
local command = table.remove(command_list, 1)
|
||||
|
||||
return {
|
||||
local res = {
|
||||
command = command,
|
||||
args = command_list,
|
||||
}
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
local job
|
||||
return setmetatable({
|
||||
close = function() end,
|
||||
}, {
|
||||
__call = void(async(function(prompt, process_result, process_complete)
|
||||
print("are we callin anything?", job)
|
||||
if job and not job.is_shutdown then
|
||||
log.debug "Shutting down old job"
|
||||
job:shutdown()
|
||||
end
|
||||
|
||||
local job_opts = fn_command(prompt)
|
||||
if not job_opts then
|
||||
local callable = function(_, prompt, process_result, process_complete)
|
||||
if job then
|
||||
job:close(true)
|
||||
end
|
||||
|
||||
local job_opts = fn_command(prompt)
|
||||
if not job_opts then
|
||||
return
|
||||
end
|
||||
|
||||
local writer = nil
|
||||
-- if job_opts.writer and Job.is_job(job_opts.writer) then
|
||||
-- writer = job_opts.writer
|
||||
if opts.writer then
|
||||
error "async_job_finder.writer is not yet implemented"
|
||||
writer = async_job.writer(opts.writer)
|
||||
end
|
||||
|
||||
local stdout = LinesPipe()
|
||||
|
||||
job = async_job.spawn {
|
||||
command = job_opts.command,
|
||||
args = job_opts.args,
|
||||
cwd = job_opts.cwd or opts.cwd,
|
||||
writer = writer,
|
||||
|
||||
stdout = stdout,
|
||||
}
|
||||
|
||||
for line in stdout:iter(true) do
|
||||
if process_result(entry_maker(line)) then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
local writer = nil
|
||||
if job_opts.writer and Job.is_job(job_opts.writer) then
|
||||
writer = job_opts.writer
|
||||
elseif opts.writer then
|
||||
writer = Job:new(job_opts.writer)
|
||||
process_complete()
|
||||
end
|
||||
|
||||
return setmetatable({
|
||||
close = function()
|
||||
if job then
|
||||
job:close(true)
|
||||
end
|
||||
|
||||
job = Job:new {
|
||||
command = job_opts.command,
|
||||
args = job_opts.args,
|
||||
cwd = job_opts.cwd or opts.cwd,
|
||||
maximum_results = opts.maximum_results,
|
||||
writer = writer,
|
||||
enable_recording = false,
|
||||
|
||||
on_stdout = vim.schedule_wrap(function(_, line)
|
||||
if not line or line == "" then
|
||||
return
|
||||
end
|
||||
|
||||
-- TODO: shutdown job here.
|
||||
process_result(entry_maker(line))
|
||||
end),
|
||||
|
||||
on_exit = function()
|
||||
process_complete()
|
||||
end,
|
||||
}
|
||||
|
||||
job:start()
|
||||
end)),
|
||||
end,
|
||||
}, {
|
||||
__call = callable,
|
||||
})
|
||||
end
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
local async_lib = require "plenary.async_lib"
|
||||
local async = async_lib.async
|
||||
local await = async_lib.await
|
||||
local void = async_lib.void
|
||||
|
||||
local AWAITABLE = 1000
|
||||
local async = require "plenary.async"
|
||||
local async_job = require "telescope._"
|
||||
local LinesPipe = require("telescope._").LinesPipe
|
||||
|
||||
local make_entry = require "telescope.make_entry"
|
||||
|
||||
local Job = require "plenary.job"
|
||||
|
||||
return function(opts)
|
||||
opts = opts or {}
|
||||
|
||||
@@ -21,64 +16,65 @@ return function(opts)
|
||||
|
||||
local job_started = false
|
||||
local job_completed = false
|
||||
local stdout = nil
|
||||
|
||||
return setmetatable({
|
||||
close = function()
|
||||
results = {}
|
||||
job_started = false
|
||||
end,
|
||||
-- close = function() results = {}; job_started = false end,
|
||||
close = function() end,
|
||||
results = results,
|
||||
}, {
|
||||
__call = void(async(function(_, prompt, process_result, process_complete)
|
||||
__call = function(_, prompt, process_result, process_complete)
|
||||
if not job_started then
|
||||
local job_opts = fn_command()
|
||||
|
||||
local writer
|
||||
if job_opts.writer and Job.is_job(job_opts.writer) then
|
||||
writer = job_opts.writer
|
||||
elseif job_opts.writer then
|
||||
writer = Job:new(job_opts.writer)
|
||||
end
|
||||
-- TODO: Handle writers.
|
||||
-- local writer
|
||||
-- if job_opts.writer and Job.is_job(job_opts.writer) then
|
||||
-- writer = job_opts.writer
|
||||
-- elseif job_opts.writer then
|
||||
-- writer = Job:new(job_opts.writer)
|
||||
-- end
|
||||
|
||||
local job = Job:new {
|
||||
stdout = LinesPipe()
|
||||
local _ = async_job.spawn {
|
||||
command = job_opts.command,
|
||||
args = job_opts.args,
|
||||
cwd = job_opts.cwd or cwd,
|
||||
maximum_results = opts.maximum_results,
|
||||
writer = writer,
|
||||
enable_recording = false,
|
||||
cwd = cwd,
|
||||
|
||||
on_stdout = vim.schedule_wrap(function(_, line)
|
||||
num_results = num_results + 1
|
||||
|
||||
local v = entry_maker(line)
|
||||
results[num_results] = v
|
||||
process_result(v)
|
||||
end),
|
||||
|
||||
on_exit = function()
|
||||
process_complete()
|
||||
job_completed = true
|
||||
end,
|
||||
stdout = stdout,
|
||||
}
|
||||
|
||||
job:start()
|
||||
job_started = true
|
||||
end
|
||||
|
||||
if not job_completed then
|
||||
for line in stdout:iter(true) do
|
||||
num_results = num_results + 1
|
||||
|
||||
local v = entry_maker(line)
|
||||
results[num_results] = v
|
||||
process_result(v)
|
||||
end
|
||||
|
||||
process_complete()
|
||||
job_completed = true
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
local current_count = num_results
|
||||
for index = 1, current_count do
|
||||
-- TODO: Figure out scheduling...
|
||||
async.util.scheduler()
|
||||
|
||||
if process_result(results[index]) then
|
||||
break
|
||||
end
|
||||
|
||||
if index % AWAITABLE == 0 then
|
||||
await(async_lib.scheduler())
|
||||
end
|
||||
end
|
||||
|
||||
if job_completed then
|
||||
process_complete()
|
||||
end
|
||||
end)),
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
local async_lib = require "plenary.async_lib"
|
||||
local async = async_lib.async
|
||||
local await = async_lib.await
|
||||
local void = async_lib.void
|
||||
local scheduler = require("plenary.async").util.scheduler
|
||||
|
||||
local make_entry = require "telescope.make_entry"
|
||||
|
||||
@@ -29,18 +26,18 @@ return function(opts)
|
||||
results = results,
|
||||
close = function() end,
|
||||
}, {
|
||||
__call = void(async(function(_, _, process_result, process_complete)
|
||||
__call = function(_, _, process_result, process_complete)
|
||||
for i, v in ipairs(results) do
|
||||
if process_result(v) then
|
||||
break
|
||||
end
|
||||
|
||||
if i % 1000 == 0 then
|
||||
await(async_lib.scheduler())
|
||||
scheduler()
|
||||
end
|
||||
end
|
||||
|
||||
process_complete()
|
||||
end)),
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user