Skip to content

Commit

Permalink
perf: speed up foldexpr by removing vimscript (#37)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stevearc committed Jan 2, 2022
1 parent 7d0531b commit 7f084b0
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 7 deletions.
4 changes: 0 additions & 4 deletions autoload/aerial.vim
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions lua/aerial/fold.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lua/aerial/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lua/aerial/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7f084b0

Please sign in to comment.