[vim/vim] pair matching is slow when there are many intervening pairs on a single line (Issue #10738)

70 views
Skip to first unread message

roblox_rkeller Ray

unread,
Jul 14, 2022, 4:00:46 PM7/14/22
to vim/vim, Subscribed

Steps to reproduce

  1. edit the output of a single-line REST JSON doc with lots of nesting
  2. or create a doc "foo" like so:
    vim -u =(echo set nocompatible) -i NONE +"set nowrap" foo with Vim edit commands like i[<CR><Esc>888a[]<Esc><CR>[<Esc>
    That is, a doc something like:
[
[][][][] x a few hundred
]
  1. then try to move your cursor onto and off of the outside-most brackets.

Expected behaviour

I expect not to experience lag of upwards of a few dozen seconds for the cursor (and pair match highlighting) to draw.

I note that the lag only seems to happen when the intervening pairs are together on a single line.

Version of Vim

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Jun 15 2022 17:08:42) macOS version - x86_64 Included patches: 1-5100 Compiled by Homebrew Huge version without GUI.

Environment

Operating system: macOS Catalina 10.15.7 (19H1615)
Terminal: iTerm2 3.4.16
Value of $TERM: screen
Shell: zsh

Logs and stack traces

FUNCTION  <SNR>6_Highlight_Matching_Pair()
    Defined: /usr/local/Cellar/vim/8.2.5100/share/vim/vim82/plugin/matchparen.vim:40
Called 1 time
Total time: 118.203978
 Self time: 118.203960

count  total (s)   self (s)
                              " Remove any previous match.
    1   0.000026   0.000008   call s:Remove_Matches()
                            
                              " Avoid that we remove the popup menu.
                              " Return when there are no colors (looks like the cursor jumps).
    1              0.000007   if pumvisible() || (&t_Co < 8 && !has("gui_running"))
                                return
    1              0.000000   endif
                            
                              " Get the character under the cursor and check if it's in 'matchpairs'.
    1              0.000004   let c_lnum = line('.')
    1              0.000003   let c_col = col('.')
    1              0.000002   let before = 0
                            
    1              0.000003   let text = getline(c_lnum)
    1              0.000016   let matches = matchlist(text, '\(.\)\=\%'.c_col.'c\(.\=\)')
    1              0.000002   if empty(matches)
                                let [c_before, c] = ['', '']
    1              0.000001   else
    1              0.000005     let [c_before, c] = matches[1:2]
    1              0.000001   endif
    1              0.000013   let plist = split(&matchpairs, '.\zs[:,]')
    1              0.000004   let i = index(plist, c)
    1              0.000001   if i < 0
                                " not found, in Insert mode try character before the cursor
                                if c_col > 1 && (mode() == 'i' || mode() == 'R')
                                  let before = strlen(c_before)
                                  let c = c_before
                                  let i = index(plist, c)
                                endif
                                if i < 0
                                  " not found, nothing to do
                                  return
                                endif
    1              0.000000   endif
                            
                              " Figure out the arguments for searchpairpos().
    1              0.000002   if i % 2 == 0
                                let s_flags = 'nW'
                                let c2 = plist[i + 1]
    1              0.000000   else
    1              0.000001     let s_flags = 'nbW'
    1              0.000002     let c2 = c
    1              0.000003     let c = plist[i - 1]
    1              0.000001   endif
    1              0.000002   if c == '['
    1              0.000001     let c = '\['
    1              0.000001     let c2 = '\]'
    1              0.000001   endif
                            
                              " Find the match.  When it was just before the cursor move it there for a
                              " moment.
    1              0.000001   if before > 0
                                let has_getcurpos = exists("*getcurpos")
                                if has_getcurpos
                                  " getcurpos() is more efficient but doesn't exist before 7.4.313.
                                  let save_cursor = getcurpos()
                                else
                                  let save_cursor = winsaveview()
                                endif
                                call cursor(c_lnum, c_col - before)
    1              0.000000   endif
                            
    1              0.000007   if !has("syntax") || !exists("g:syntax_on")
    1              0.000002     let s_skip = "0"
                              else
                                " Build an expression that detects whether the current cursor position is
                                " in certain syntax types (string, comment, etc.), for use as
                                " searchpairpos()'s skip argument.
                                " We match "escape" for special items, such as lispEscapeSpecial, and
                                " match "symbol" for lispBarSymbol.
                                let s_skip = '!empty(filter(map(synstack(line("."), col(".")), ''synIDattr(v:val, "name")''), ' . '''v:val =~? "string\\|character\\|singlequote\\|escape\\|symbol\\|comment"''))'
                                " If executing the expression determines that the cursor is currently in
                                " one of the syntax types, then we want searchpairpos() to find the pair
                                " within those syntax types (i.e., not skip).  Otherwise, the cursor is
                                " outside of the syntax types and s_skip should keep its value so we skip
                                " any matching pair inside the syntax types.
                                " Catch if this throws E363: pattern uses more memory than 'maxmempattern'.
                                try
                                  execute 'if ' . s_skip . ' | let s_skip = "0" | endif'
                                catch /^Vim\%((\a\+)\)\=:E363/
                                  " We won't find anything, so skip searching, should keep Vim responsive.
                                  return
                                endtry
    1              0.000000   endif
                            
                              " Limit the search to lines visible in the window.
    1              0.000004   let stoplinebottom = line('w$')
    1              0.000003   let stoplinetop = line('w0')
    1              0.000001   if i % 2 == 0
                                let stopline = stoplinebottom
    1              0.000001   else
    1              0.000002     let stopline = stoplinetop
    1              0.000001   endif
                            
                              " Limit the search time to 300 msec to avoid a hang on very long lines.
                              " This fails when a timeout is not supported.
    1              0.000004   if mode() == 'i' || mode() == 'R'
                                let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
    1              0.000000   else
    1              0.000004     let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
    1              0.000000   endif
    1              0.000001   try
    1            118.203676     let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout)
                              catch /E118/
                                " Can't use the timeout, restrict the stopline a bit more to avoid taking
                                " a long time on closed folds and long lines.
                                " The "viewable" variables give a range in which we can scroll while
                                " keeping the cursor at the same position.
                                " adjustedScrolloff accounts for very large numbers of scrolloff.
                                let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
                                let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
                                let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
                                " one of these stoplines will be adjusted below, but the current values are
                                " minimal boundaries within the current window
                                if i % 2 == 0
                                  if has("byte_offset") && has("syntax_items") && &smc > 0
                            	let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
                            	let stopline = min([bottom_viewable, byte2line(stopbyte)])
                                  else
                            	let stopline = min([bottom_viewable, c_lnum + 100])
                                  endif
                                  let stoplinebottom = stopline
                                else
                                  if has("byte_offset") && has("syntax_items") && &smc > 0
                            	let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
                            	let stopline = max([top_viewable, byte2line(stopbyte)])
                                  else
                            	let stopline = max([top_viewable, c_lnum - 100])
                                  endif
                                  let stoplinetop = stopline
                                endif
                                let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
    1              0.000002   endtry
                            
    1              0.000002   if before > 0
                                if has_getcurpos
                                  call setpos('.', save_cursor)
                                else
                                  call winrestview(save_cursor)
                                endif
    1              0.000000   endif
                            
                              " If a match is found setup match highlighting.
    1              0.000002   if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom 
    1              0.000004     if exists('*matchaddpos')
    1              0.000009       call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3)
                                else
                                  exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
    1              0.000001     endif
    1              0.000002     let w:paren_hl_on = 1
    1              0.000001   endif

FUNCTION  <SNR>6_Remove_Matches()
    Defined: /usr/local/Cellar/vim/8.2.5100/share/vim/vim82/plugin/matchparen.vim:197
Called 1 time
Total time:   0.000018
 Self time:   0.000018

count  total (s)   self (s)
    1              0.000010   if exists('w:paren_hl_on') && w:paren_hl_on
                                silent! call matchdelete(3)
                                let w:paren_hl_on = 0
    1              0.000002   endif

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
    1 118.203978 118.203960  <SNR>6_Highlight_Matching_Pair()
    1   0.000018             <SNR>6_Remove_Matches()

FUNCTIONS SORTED ON SELF TIME
count  total (s)   self (s)  function
    1 118.203978 118.203960  <SNR>6_Highlight_Matching_Pair()
    1              0.000018  <SNR>6_Remove_Matches()


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/10738@github.com>

roblox_rkeller Ray

unread,
Jul 28, 2022, 11:00:57 AM7/28/22
to vim/vim, Subscribed

I'm not experiencing the same slowness in this newer version:
VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Jul 12 2022 16:34:31)


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/10738/1198269335@github.com>

Christian Brabandt

unread,
Jul 28, 2022, 1:54:04 PM7/28/22
to vim/vim, Subscribed

closing then


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/10738/1198457063@github.com>

Christian Brabandt

unread,
Jul 28, 2022, 1:54:06 PM7/28/22
to vim/vim, Subscribed

Closed #10738 as completed.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/10738/issue_event/7083676780@github.com>

Reply all
Reply to author
Forward
0 new messages