vim-matchup icon indicating copy to clipboard operation
vim-matchup copied to clipboard

Freeze in large C file with treesitter

Open zeertzjq opened this issue 2 years ago • 7 comments

Is your feature request related to a problem? Please describe. When treesitter highlighting is enabled, vim-matchup unconditionally uses treesitter delim skip, which can be very slow. In some large C files this causes vim-matchup to freeze Nvim for seconds when moving cursor onto a curly brace, and the following change makes the lag disappear:

diff --git a/autoload/matchup/loader.vim b/autoload/matchup/loader.vim
index e78243b..b943f7a 100644
--- a/autoload/matchup/loader.vim
+++ b/autoload/matchup/loader.vim
@@ -58,12 +58,7 @@ function! matchup#loader#init_buffer() abort " {{{1
   let b:matchup_delim_re = s:init_delim_regexes()
 
   " process b:match_skip
-  if l:has_ts_hl
-    let b:matchup_delim_skip
-          \ = "matchup#ts_syntax#skip_expr(s:effline('.'),s:effcol('.'))"
-  else
     let b:matchup_delim_skip = s:init_delim_skip()
-  endif
 
   " enable matching engines
   let b:matchup_active_engines = {}

Describe the solution you'd like Add an option to vim-matchup to disable treesitter delim skip.

zeertzjq avatar Jul 05 '23 04:07 zeertzjq

Because it uses the Treesitter AST, my expectation is matchup#ts_syntax#skip_expr would be very fast. (At least faster than vim syntax highlighting.) Maybe there is something wrong in the way it's computed or cached.

Can you provide a sample C file which has the issue?

andymass avatar Jul 05 '23 12:07 andymass

src/nvim/regexp_nfa.c in Nvim source code.

zeertzjq avatar Jul 05 '23 12:07 zeertzjq

I may have made a mistake when testing this. It seems that when I fully enable matchup treesitter support (matchup = { enable = true }), vim-matchup freezes Nvim every time viewport scrolls by a screen. However, if I disable that (matchup = { enable = false }), vim-matchup only freezes the first time I stop cursor on a curly brace and is fast afterwards. This is probably not needed then.

zeertzjq avatar Jul 05 '23 15:07 zeertzjq

Still seems a bug. The promise of treesitter is that things are blazing fast. I will investigate nevertheless.

On Wed, Jul 5, 2023, 11:10 AM zeertzjq @.***> wrote:

I may have made a mistake when testing this. It seems that when I fully enable matchup treesitter support ( matchup = { enable = true },), vim-matchup freezes Nvim every time viewport scrolls by a screen. However, if I disable that ( matchup = { enable = false },), vim-matchup only freezes the first time I stop cursor on a curly brace and is fast afterwards. This is probably not needed then.

— Reply to this email directly, view it on GitHub https://github.com/andymass/vim-matchup/issues/302#issuecomment-1621959312, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABSY3DKWZBSJ2EVUX7KUWIDXOV7WDANCNFSM6AAAAAAZ6MLRCU . You are receiving this because you commented.Message ID: @.***>

andymass avatar Jul 05 '23 15:07 andymass

I see a huge amount of lag in regexp_nfa.c when scrolling but it doesn't seem to be caused by ts syntax skip but instead this call to get_active_nodes

https://github.com/andymass/vim-matchup/blob/3c4ccc489002fe72b5d2e2ffc4b3b5a7d3ab65d8/lua/treesitter-matchup/internal.lua#L221

When I comment this line out the lag disappears. What I don't understand is that get_active_nodes is gated behind ts_utils.memoize_by_buf_tick https://github.com/andymass/vim-matchup/blob/3c4ccc489002fe72b5d2e2ffc4b3b5a7d3ab65d8/lua/treesitter-matchup/internal.lua#L95

Yet when scrolling it seems to be called again and again, even if the buffer hasn't changed.

andymass avatar Jul 11 '23 13:07 andymass

Had this same issue but in Lua files, when moving cursor up/down and hitting a bracked it would freeze the editor for a bit (on a very beefy machine), fixed by disabling the plugin. Might be a Neovim issue, disabled Navic as well for the same reason.

3rd avatar Jul 24 '23 20:07 3rd