From 3c3b27a2cb17b529e766b80fcdc9f04df78d3311 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Mon, 9 Feb 2026 12:36:51 -0500 Subject: [PATCH] perf: cache repo root and harden async paths Problem: get_repo_root() shells out on every call, causing 4-6 redundant subprocesses per gdiff_file() invocation. highlight_vim_syntax() leaks a scratch buffer if nvim_buf_call errors. lib.ensure() silently drops callbacks during download, permanently missing intra-line highlights. The debounce timer callback can operate on an invalid buffer. Solution: Cache get_repo_root() results by parent directory. Wrap nvim_buf_call and nvim_buf_delete in pcall so the scratch buffer is always cleaned up. Queue pending callbacks in lib.ensure() so all callers receive the handle once the download completes. Guard the debounce timer callback with nvim_buf_is_valid. --- lua/diffs/git.lua | 6 ++++++ lua/diffs/highlight.lua | 4 ++-- lua/diffs/init.lua | 4 +++- lua/diffs/lib.lua | 30 +++++++++++++++++++----------- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/lua/diffs/git.lua b/lua/diffs/git.lua index 7695283..1aa6328 100644 --- a/lua/diffs/git.lua +++ b/lua/diffs/git.lua @@ -1,13 +1,19 @@ local M = {} +local repo_root_cache = {} + ---@param filepath string ---@return string? function M.get_repo_root(filepath) local dir = vim.fn.fnamemodify(filepath, ':h') + if repo_root_cache[dir] ~= nil then + return repo_root_cache[dir] + end local result = vim.fn.systemlist({ 'git', '-C', dir, 'rev-parse', '--show-toplevel' }) if vim.v.shell_error ~= 0 then return nil end + repo_root_cache[dir] = result[1] return result[1] end diff --git a/lua/diffs/highlight.lua b/lua/diffs/highlight.lua index 306b0e5..6cdfe92 100644 --- a/lua/diffs/highlight.lua +++ b/lua/diffs/highlight.lua @@ -238,7 +238,7 @@ local function highlight_vim_syntax(bufnr, ns, hunk, code_lines, covered_lines, local spans = {} - vim.api.nvim_buf_call(scratch, function() + pcall(vim.api.nvim_buf_call, scratch, function() vim.cmd('setlocal syntax=' .. ft) vim.cmd('redraw') @@ -256,7 +256,7 @@ local function highlight_vim_syntax(bufnr, ns, hunk, code_lines, covered_lines, spans = M.coalesce_syntax_spans(query_fn, code_lines) end) - vim.api.nvim_buf_delete(scratch, { force = true }) + pcall(vim.api.nvim_buf_delete, scratch, { force = true }) local hunk_line_count = #hunk.lines local extmark_count = 0 diff --git a/lua/diffs/init.lua b/lua/diffs/init.lua index cdc29d1..69a5cf9 100644 --- a/lua/diffs/init.lua +++ b/lua/diffs/init.lua @@ -200,7 +200,9 @@ local function create_debounced_highlight(bufnr) timer = nil t:close() end - highlight_buffer(bufnr) + if vim.api.nvim_buf_is_valid(bufnr) then + highlight_buffer(bufnr) + end end) ) end diff --git a/lua/diffs/lib.lua b/lua/diffs/lib.lua index 5b3254b..8376925 100644 --- a/lua/diffs/lib.lua +++ b/lua/diffs/lib.lua @@ -8,6 +8,9 @@ local cached_handle = nil ---@type boolean local download_in_progress = false +---@type fun(handle: table?)[] +local pending_callbacks = {} + ---@return string local function get_os() local os_name = jit.os:lower() @@ -164,9 +167,10 @@ function M.ensure(callback) return end + table.insert(pending_callbacks, callback) + if download_in_progress then - dbg('download already in progress') - callback(nil) + dbg('download already in progress, queued callback') return end @@ -192,21 +196,25 @@ function M.ensure(callback) vim.system(cmd, {}, function(result) download_in_progress = false vim.schedule(function() + local handle = nil if result.code ~= 0 then vim.notify('[diffs] failed to download libvscode_diff', vim.log.levels.WARN) dbg('curl failed: %s', result.stderr or '') - callback(nil) - return + else + local f = io.open(version_path(), 'w') + if f then + f:write(EXPECTED_VERSION) + f:close() + end + vim.notify('[diffs] libvscode_diff downloaded', vim.log.levels.INFO) + handle = M.load() end - local f = io.open(version_path(), 'w') - if f then - f:write(EXPECTED_VERSION) - f:close() + local cbs = pending_callbacks + pending_callbacks = {} + for _, cb in ipairs(cbs) do + cb(handle) end - - vim.notify('[diffs] libvscode_diff downloaded', vim.log.levels.INFO) - callback(M.load()) end) end) end