diff --git a/lua/telescope/example.lua b/lua/telescope/example.lua new file mode 100644 index 0000000..13c30c5 --- /dev/null +++ b/lua/telescope/example.lua @@ -0,0 +1 @@ +return function() return 5 end diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index a3fcc57..3c77b92 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -364,25 +364,11 @@ function Picker:find() log.trace("Processing result... ", entry) - local sort_ok, sort_score = nil, 0 if sorter then - sort_ok, sort_score = pcall(function () - return sorter:score(prompt, entry) - end) - - if not sort_ok then - log.warn("Sorting failed with:", prompt, entry, sort_score) - return - end - - if sort_score == -1 then - filtered_amount = filtered_amount + 1 - log.trace("Filtering out result: ", entry) - return - end + require('telescope.sorters.multi_thread').score_entry(prompt, entry, self) + else + self.manager:add_entry(0, entry) end - - self.manager:add_entry(sort_score, entry) end local process_complete = vim.schedule_wrap(function() diff --git a/lua/telescope/sorters.lua b/lua/telescope/sorters.lua index dd7cf4f..4ae9faf 100644 --- a/lua/telescope/sorters.lua +++ b/lua/telescope/sorters.lua @@ -1,4 +1,3 @@ -local log = require('telescope.log') local util = require('telescope.utils') local sorters = {} @@ -65,7 +64,6 @@ sorters.get_levenshtein_sorter = function() return Sorter:new { scoring_function = function(_, prompt, line) local result = require('telescope.algos.string_distance')(prompt, line) - log.info("Sorting result for", prompt, line, " = ", result) return result end } diff --git a/lua/telescope/sorters/multi_thread.lua b/lua/telescope/sorters/multi_thread.lua new file mode 100644 index 0000000..9f9aec3 --- /dev/null +++ b/lua/telescope/sorters/multi_thread.lua @@ -0,0 +1,30 @@ +local log = require('telescope.log') + +local M = {} + +function M.score_entry(prompt, entry, picker) + local worker = vim.loop.new_work(function(path, prompt, entry) + package.path = path + + if not FuzzySorter then + FuzzySorter = require('telescope.sorters').get_fuzzy_file() + end + + -- return pcall(FuzzySorter.score, FuzzySorter, prompt, entry) + return true, 3 + end, vim.schedule_wrap(function(score_ok, sort_score) + -- TODO: we should totally make sure that this picker is still doing stuff... + -- it could otherwise be done. + if not score_ok or sort_score == -1 then + log.warn("Sorting failed with:", prompt, entry, sort_score) + return + end + + -- picker.manager:add_entry(sort_score, entry) + print(score_ok, sort_score) + end)) + + worker:queue(package.path, prompt, type(entry) == "string" and entry or entry.ordinal) +end + +return M diff --git a/scratch/cqueues_tester.lua b/scratch/cqueues_tester.lua new file mode 100644 index 0000000..5c6aa2e --- /dev/null +++ b/scratch/cqueues_tester.lua @@ -0,0 +1,49 @@ +-- Actually works & runs +local cqueues = require "cqueues" +local uv = require "luv" + +local cq = cqueues.new() + +do + local timer = uv.new_timer() + local function reset_timer() + local timeout = cq:timeout() + if timeout then + -- libuv takes milliseconds as an integer, + -- while cqueues gives timeouts as a floating point number + -- use `math.ceil` as we'd rather wake up late than early + timer:set_repeat(math.ceil(timeout * 1000)) + timer:again() + else + -- stop timer for now; it may be restarted later. + timer:stop() + end + end + local function onready() + -- Step the cqueues loop once (sleeping for max 0 seconds) + assert(cq:step(0)) + reset_timer() + end + -- Need to call `start` on libuv timer now + -- to provide callback and so that `again` works + timer:start(0, 0, onready) + -- Ask libuv to watch the cqueue pollfd + uv.new_poll(cq:pollfd()):start(cq:events(), onready) +end + +-- Adds a new function to the scheduler `cq` +-- The functions is an infinite loop that sleeps for 1 second and prints +cq:wrap(function() + while true do + cqueues.sleep(1) + print("HELLO FROM CQUEUES") + end +end) + +-- Start a luv timer that fires every 1 second +uv.new_timer():start(1000, 1000, function() + print("HELLO FROM LUV") +end) + +-- Run luv mainloop +uv.run() diff --git a/scratch/new_thread_examples.lua b/scratch/new_thread_examples.lua new file mode 100644 index 0000000..874bdfb --- /dev/null +++ b/scratch/new_thread_examples.lua @@ -0,0 +1,36 @@ +local uv = vim.loop + +-- local pipe_to_share = uv.new_pipe(false) +-- https://github.com/luvit/luv/blob/master/docs.md#uvwrite2stream-data-send_handle-callback + +-- Requirements: +-- I only want to import the sorter ONCE (don't reload a million times) +-- I want to run a callback when we're done. +-- I want to be able to re-use sorters in the background +-- I don't want the thread to busy wait +-- I don't wnat a lot of overhead of sending tons of data back and forth between procs. + +local pipe = uv.new_pipe(false) +local socket_name = '/tmp/sock.test_3' + +pipe:bind(socket_name) +pipe:read_start(function(...) + print('we readin from this pipe') + print(...) +end) + +local other_pipe = uv.pipe_open(pipe) + +print(uv.pipe_getsockname(pipe)) + +pipe:listen(128, function() + local client = uv.new_pipe(false) + pipe:accept(client) + + client:write("hello!\n") + client:close() +end) + +other_pipe:write("other pipe!\n") + +pipe:close() diff --git a/scratch/new_thread_writer_example.lua b/scratch/new_thread_writer_example.lua new file mode 100644 index 0000000..3344b9b --- /dev/null +++ b/scratch/new_thread_writer_example.lua @@ -0,0 +1,10 @@ +local uv = vim.loop + +local pipe = uv.new_pipe(false) + +pipe:bind('/tmp/sock.test_2') +pipe:read_start(function(...) + print('we readin from this pipe') + print(...) +end) +pipe:write("hello??") diff --git a/scratch/thread_from_luv.lua b/scratch/thread_from_luv.lua new file mode 100644 index 0000000..1c01aee --- /dev/null +++ b/scratch/thread_from_luv.lua @@ -0,0 +1,39 @@ +local uv = require('luv') + +local step = 10 + +local hare_id = uv.new_thread(function(step,...) + local ffi = require'ffi' + local uv = require('luv') + local sleep + if ffi.os=='Windows' then + ffi.cdef "void Sleep(int ms);" + sleep = ffi.C.Sleep + else + ffi.cdef "unsigned int usleep(unsigned int seconds);" + sleep = ffi.C.usleep + end + while (step>0) do + step = step - 1 + sleep(math.random(1000)) + -- print("Hare ran another step") + end + -- print("Hare done running!") +end, step,true,'abcd','false') + +local tortoise_id = uv.new_thread(function(step,...) + local uv = require('luv') + while (step>0) do + step = step - 1 + uv.sleep(math.random(100)) + -- this is just normal lua print, not neovim lua print + -- print("Tortoise ran another step") + end + -- print("Tortoise done running!") +end,step,'abcd','false') + +print(hare_id == hare_id, uv.thread_equal(hare_id, hare_id)) +print(tortoise_id == hare_id, uv.thread_equal(tortoise_id, hare_id)) + +print(uv.thread_join(hare_id)) +print(uv.thread_join(tortoise_id)) diff --git a/scratch/threaded_lua.lua b/scratch/threaded_lua.lua index 68eddb0..1875b91 100644 --- a/scratch/threaded_lua.lua +++ b/scratch/threaded_lua.lua @@ -1,15 +1,44 @@ -local uv = require('luv') +local uv = vim.loop --- print(vim.inspect(uv)) +ThreadsAvailable = {} + +local work = vim.loop.new_work(function(path, prompt, line) + package.path = path + local had_to_load = false + + local uv = require('luv') + if not fuzzy_sorter then + had_to_load = true + fuzzy_sorter = require('telescope.sorters').get_fuzzy_file() + end + + -- return fuzzy_sorter:score(prompt, line), had_to_load, uv.hrtime(), tostring(uv.thread_self()) + return tostring(uv.thread_self()) +end, function(thread_string) + -- print(vim.inspect(vim.split(x, ';'))) + ThreadsAvailable[thread_string] = true +end) + +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") +work:queue(package.path, "hello", "hello world") -local my_table = {} -local my_value = 1 - -local table_adder = uv.new_thread(function(tbl) - table.insert(tbl, "HELLO") -end, my_table) - -uv.thread_join(table_adder) --- print(vim.inspect(MY_TABLE)) -