perf: speed up foldexpr by removing vimscript (#37)

Going through the aerial#foldexpr() script was taking some overhead. By
directly using v:lua we can avoid that and get a ~30% speed boost.
This commit is contained in:
Steven Arcangeli 2022-01-01 17:20:52 -08:00
parent 7d0531bd2f
commit 7f084b0b2b
4 changed files with 6 additions and 7 deletions

View file

@ -1,7 +1,3 @@
function! aerial#foldexpr() abort
return luaeval('require"aerial.fold".foldexpr(_A)', v:lnum)
endfunction
function! aerial#fzf() abort
let l:labels = luaeval("require('aerial.fzf').get_labels()")
if type(l:labels) == type(v:null) && l:labels == v:null

View file

@ -59,7 +59,7 @@ local function compute_folds(bufnr)
return fold_levels
end
M.foldexpr = function(lnum)
M.foldexpr = function()
if util.is_aerial_buffer() then
return "0"
end
@ -73,6 +73,7 @@ M.foldexpr = function(lnum)
if not cache then
cache = compute_folds(bufnr)
end
local lnum = vim.v.lnum
-- Clear the cache after calling foldexpr on all lines
if lnum == vim.api.nvim_buf_line_count(bufnr) then
fold_cache[bufnr] = nil
@ -119,7 +120,7 @@ M.maybe_set_foldmethod = function(bufnr)
vim.api.nvim_win_set_var(winid, prev_fdm, fdm)
vim.api.nvim_win_set_var(winid, prev_fde, fde)
vim.api.nvim_win_set_option(winid, "foldmethod", "expr")
vim.api.nvim_win_set_option(winid, "foldexpr", "aerial#foldexpr()")
vim.api.nvim_win_set_option(winid, "foldexpr", "v:lua.aerial_foldexpr()")
if config.link_tree_to_folds then
vim.api.nvim_win_set_option(winid, "foldlevel", 99)
end

View file

@ -248,4 +248,6 @@ M.info = function()
print(string.format("Show symbols: %s", config.get_filter_kind_map()))
end
_G.aerial_foldexpr = fold.foldexpr
return M

View file

@ -190,7 +190,7 @@ M.is_floating_win = function(winid)
end
M.is_managing_folds = function(winid)
return vim.api.nvim_win_get_option(winid or 0, "foldexpr") == "aerial#foldexpr()"
return vim.api.nvim_win_get_option(winid or 0, "foldexpr") == "v:lua.aerial_foldexpr()"
end
M.detect_split_direction = function(bufnr)