Skip to content

Commit 827a8f3

Browse files
authored
perf: various performance fixes (#40)
1 parent e66bb52 commit 827a8f3

File tree

6 files changed

+87
-46
lines changed

6 files changed

+87
-46
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
on: [push, pull_request]
2-
1+
---
32
name: CI
43

4+
on:
5+
push:
6+
branches:
7+
- main
8+
pull_request:
9+
510
jobs:
611
check:
712
name: Check
813
runs-on: ubuntu-22.04
914
steps:
10-
- uses: actions/checkout@v3
15+
- uses: actions/checkout@v4
1116
- uses: lunarmodules/luacheck@v0
1217
fmt:
1318
name: Fmt
1419
runs-on: ubuntu-22.04
1520
steps:
16-
- uses: actions/checkout@v3
21+
- uses: actions/checkout@v4
1722
- uses: JohnnyMorganz/stylua-action@v2
1823
with:
1924
token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ luac.out
3939
*.x86_64
4040
*.hex
4141

42+
.nvimlog

lua/remote-sshfs/connections.lua

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,9 @@ M.mount_host = function(host, mount_dir, ask_pass)
133133
local function start_job()
134134
vim.notify("Connecting to host (" .. remote_host .. ")...")
135135
local skip_clean = false
136-
mount_point = mount_dir .. "/"
137-
current_host = host
138-
-- Spawn SSHFS without a shell
139-
sshfs_job_id = vim.fn.jobstart(cmd, {
136+
local spec_mount_point = mount_dir .. "/"
137+
local spec_host = host
138+
local id = vim.fn.jobstart(cmd, {
140139
on_stdout = function(_, data)
141140
handler.sshfs_wrapper(data, mount_dir, function(event)
142141
if event == "ask_pass" then
@@ -153,18 +152,27 @@ M.mount_host = function(host, mount_dir, ask_pass)
153152
end
154153
end)
155154
end,
156-
on_exit = function(_, _, data)
155+
on_exit = function(jid, _, data)
156+
if jid ~= sshfs_job_id then
157+
return
158+
end
157159
handler.on_exit_handler(data, mount_dir, skip_clean, function()
158160
sshfs_job_id = nil
159161
mount_point = nil
160162
current_host = nil
161163
end)
162164
end,
163165
})
164-
-- If using password, send it on stdin
166+
if id <= 0 then
167+
vim.notify("[remote-sshfs] failed to start sshfs (code " .. tostring(id) .. ")", vim.log.levels.ERROR)
168+
return
169+
end
170+
sshfs_job_id = id
171+
mount_point = spec_mount_point
172+
current_host = spec_host
165173
if ask_pass then
166174
local password = vim.fn.inputsecret "Enter password for host: "
167-
vim.fn.chansend(sshfs_job_id, password .. "\n")
175+
vim.fn.chansend(id, password .. "\n")
168176
end
169177
end
170178
start_job()
@@ -187,6 +195,8 @@ M.unmount_host = function()
187195
sshfs_job_id = nil
188196
mount_point = nil
189197
current_host = nil
198+
-- Clear Telescope extension cache for remote-find commands
199+
pcall(require, "telescope._extensions.remote-sshfs").clear_cache()
190200
end
191201
end
192202

lua/remote-sshfs/log.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ end
2929

3030
M.setup = function(opts)
3131
if opts.log.enabled and opts.log.types then
32-
M.path = string.format("%s/remote-sshfs.log", vim.fn.stdpath "cache", os.date "%H:%M:%S", vim.env.USER)
32+
M.path = string.format("%s/remote-sshfs.log", vim.fn.stdpath "cache")
3333
M.types = opts.log.types
3434
if opts.log.truncate then
3535
os.remove(M.path)

lua/remote-sshfs/utils.lua

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ local M = {}
55
M.setup_sshfs = function(config)
66
local sshfs_folder = config.mounts.base_dir
77
if not vim.loop.fs_stat(sshfs_folder) then
8-
vim.loop.fs_mkdir(sshfs_folder, tonumber("700", 8), function(err)
9-
if err then
10-
vim.notify("Error creating mount base dir (" .. sshfs_folder .. "):", err)
11-
return
12-
end
13-
end)
8+
local ok, err = vim.loop.fs_mkdir(sshfs_folder, tonumber("700", 8))
9+
if not ok then
10+
vim.notify("Error creating mount base dir (" .. sshfs_folder .. "): " .. tostring(err), vim.log.levels.ERROR)
11+
end
1412
end
1513
end
1614

@@ -23,15 +21,13 @@ M.setup_mount_dir = function(mount_dir, callback)
2321
log.line("util", "Setting up mount directory " .. mount_dir)
2422
if not M.file_exists(mount_dir) then
2523
log.line("util", "Creating mount directory " .. mount_dir)
26-
local success = vim.loop.fs_mkdir(mount_dir, tonumber("700", 8))
27-
if not success then
28-
vim.notify("Error creating mount directory (" .. mount_dir .. ").")
29-
else
30-
callback()
24+
local ok, err = vim.loop.fs_mkdir(mount_dir, tonumber("700", 8))
25+
if not ok then
26+
vim.notify("Error creating mount directory (" .. mount_dir .. "): " .. tostring(err), vim.log.levels.ERROR)
27+
return
3128
end
32-
else
33-
callback()
3429
end
30+
callback()
3531
end
3632

3733
M.cleanup_mount_dir = function(mount_dir, callback)

lua/telescope/_extensions/remote-sshfs.lua

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,44 @@ local function edit_config(_)
114114
:find()
115115
end
116116

117+
local command_exists_cache = {}
118+
117119
local function command_exists_on_remote(command, server)
120+
local key = server .. ":" .. command
121+
if command_exists_cache[key] ~= nil then
122+
return command_exists_cache[key]
123+
end
118124
local ssh_cmd = string.format('ssh %s "which %s"', server, command)
119-
local result = vim.fn.system(ssh_cmd)
120-
return result ~= ""
125+
vim.fn.system(ssh_cmd)
126+
local exists = (vim.v.shell_error == 0)
127+
command_exists_cache[key] = exists
128+
return exists
129+
end
130+
131+
-- Cache per-host computed find command to avoid repeated ssh which calls
132+
local _find_command_cache = {}
133+
local function get_find_command_for_host(server)
134+
if _find_command_cache[server] ~= nil then
135+
return _find_command_cache[server]
136+
end
137+
local cmd = nil
138+
if command_exists_on_remote("rg", server) then
139+
cmd = { "ssh", server, "-C", "rg", "--files", "--color", "never" }
140+
elseif command_exists_on_remote("fd", server) then
141+
cmd = { "ssh", server, "fd", "--type", "f", "--color", "never" }
142+
elseif command_exists_on_remote("fdfind", server) then
143+
cmd = { "ssh", server, "fdfind", "--type", "f", "--color", "never" }
144+
elseif command_exists_on_remote("where", server) then
145+
cmd = { "ssh", server, "where", "/r", ".", "*" }
146+
end
147+
_find_command_cache[server] = cmd
148+
return cmd
149+
end
150+
151+
-- Clears cached remote-find commands and existence checks
152+
local function clear_cache()
153+
command_exists_cache = {}
154+
_find_command_cache = {}
121155
end
122156

123157
-- Remote find_files implementation
@@ -160,22 +194,16 @@ local function find_files(opts)
160194
local mount_point = opts.mount_point or connections.get_current_mount_point()
161195
local current_host = connections.get_current_host()
162196

163-
local find_command = (function()
164-
if opts.find_command then
165-
if type(opts.find_command) == "function" then
166-
return opts.find_command(opts)
167-
end
168-
return opts.find_command
169-
elseif command_exists_on_remote("rg", current_host["Name"]) then
170-
return { "ssh", current_host["Name"], "-C", "rg", "--files", "--color", "never" }
171-
elseif command_exists_on_remote("fd", current_host["Name"]) then
172-
return { "ssh", current_host["Name"], "fd", "--type", "f", "--color", "never" }
173-
elseif command_exists_on_remote("fdfind", current_host["Name"]) then
174-
return { "ssh", current_host["Name"], "fdfind", "--type", "f", "--color", "never" }
175-
elseif command_exists_on_remote("where", current_host["Name"]) then
176-
return { "ssh", current_host["Name"], "where", "/r", ".", "*" }
197+
local find_command
198+
if opts.find_command then
199+
if type(opts.find_command) == "function" then
200+
find_command = opts.find_command(opts)
201+
else
202+
find_command = opts.find_command
177203
end
178-
end)()
204+
else
205+
find_command = get_find_command_for_host(current_host["Name"])
206+
end
179207

180208
if not find_command then
181209
vim.notify "Remote host does not support any available find commands (rg, fd, fdfind, where)."
@@ -477,18 +505,19 @@ local present, telescope = pcall(require, "telescope")
477505
if present then
478506
return telescope.register_extension {
479507
exports = {
480-
connect = function(_)
481-
connect(_)
508+
connect = function(opts)
509+
connect(opts)
482510
end,
483-
edit = function(_)
484-
edit_config(_)
511+
edit = function(opts)
512+
edit_config(opts)
485513
end,
486514
find_files = function(opts)
487515
find_files(opts)
488516
end,
489517
live_grep = function(opts)
490518
live_grep(opts)
491519
end,
520+
clear_cache = clear_cache,
492521
},
493522
}
494523
else

0 commit comments

Comments
 (0)