Skip to content

Using coc.nvim causes matchstrlist() to lag after the Vim instance has been open for several hours. #5476

@jiangtianli91

Description

@jiangtianli91

Result from CocInfo

versions

vim version: VIM - Vi IMproved 9.1 9011128
node version: v23.9.0
coc.nvim version: 0.0.82-51d5b66 2025-11-03 20:01:55 +0800
coc.nvim directory: /Users/.../.vim/plugged/coc.nvim
term: dumb
platform: darwin

## Log of coc.nvim

2025-11-15T21:42:06.332 INFO (pid:40322) [plugin] - coc.nvim initialized with node: v23.9.0 after 60

Describe the bug

I initially noticed this bug from the increased loading time of .tex file using VimTex. Slow opening of tex file. With the help from @lervag, I eventually found out that issue was due to the lag in the function matchstrlist(), which is used in vimtex to parse the preamble in .tex files. This lag does not happen when a new vim instance is immediately created. Instead, it happens when the vim instance is left open for minutes to hours. It increases running time of the following matchstrlist() function from around 0.08 seconds to 6 or 7 second.

function! Testing() abort
  let l:not_bslash =  '\v%(\\@<!%(\\\\)*)@<='
  let l:not_comment = '\v%(' . l:not_bslash . '\%.*)@<!'
  let l:pat = l:not_comment .. l:not_bslash
        \ .. '\v\\%(usep|RequireP)ackage\s*%(\[([^[\]]*)\])?\s*\{\s*\zs%([^{}]+\S)\ze\s*\}'

  let l:input = "\\documentclass[letterpaper, 11pt]{article}\\usepackage[backend=biber,style=apa,sorting=nyt,date=year,alldates=year]{biblatex}\\addbibresource{/Users/jiangtianli/MyWriting/Li_bib.bib}\\usepackage[margin=1in]{geometry}\\usepackage{hyperref}\\usepackage{enumitem}\\usepackage{subcaption}\\usepackage{float}\\usepackage{wrapfig2}\\usepackage{svg}\\setlength{\\parskip}{0.4em}\\usepackage{fancyhdr}\\pagestyle{fancy}\\fancyhead{} \\fancyhead[R]{Jiangtian Li}\\fancyhead[L]{Application Documents}\\usepackage{caption}\\captionsetup{font=footnotesize}\\makeatletter\\renewcommand{\\maketitle}{ \\vspace*{-2.5em}   \\begin{center}    {\\LARGE \\@title \\par}    \\vskip 1em {\\large \\@author \\par}    \\vskip 1em    {\\small \\@date \\par} \\end{center}}\\makeatother\\title{Research Statement}\\author{}\\date{}\\begin{document}"

  let l:t0 = reltimefloat(reltime())
  let l:output = matchstrlist([l:input], l:pat, #{submatches: v:true})
  let l:t1 = reltimefloat(reltime())

  let l:timing = l:t1 - l:t0
  echo "Timing:" l:timing
  return l:timing
endfunction

Reproduce the bug

Here is the minimal vimrc. You may need to adjust the part to load coc.nvim.

execute 'source' expand('~/.vim/autoload/plug.vim')

call plug#begin('~/.vim/plugged')
Plug 'neoclide/coc.nvim', {'branch': 'release'}
call plug#end()

set nocompatible
filetype plugin indent on
syntax enable

function! Testing() abort
  let l:not_bslash =  '\v%(\\@<!%(\\\\)*)@<='
  let l:not_comment = '\v%(' . l:not_bslash . '\%.*)@<!'
  let l:pat = l:not_comment .. l:not_bslash
        \ .. '\v\\%(usep|RequireP)ackage\s*%(\[([^[\]]*)\])?\s*\{\s*\zs%([^{}]+\S)\ze\s*\}'

  let l:input = "\\documentclass[letterpaper, 11pt]{article}\\usepackage[backend=biber,style=apa,sorting=nyt,date=year,alldates=year]{biblatex}\\addbibresource{/Users/jiangtianli/MyWriting/Li_bib.bib}\\usepackage[margin=1in]{geometry}\\usepackage{hyperref}\\usepackage{enumitem}\\usepackage{subcaption}\\usepackage{float}\\usepackage{wrapfig2}\\usepackage{svg}\\setlength{\\parskip}{0.4em}\\usepackage{fancyhdr}\\pagestyle{fancy}\\fancyhead{} \\fancyhead[R]{Jiangtian Li}\\fancyhead[L]{Application Documents}\\usepackage{caption}\\captionsetup{font=footnotesize}\\makeatletter\\renewcommand{\\maketitle}{ \\vspace*{-2.5em}   \\begin{center}    {\\LARGE \\@title \\par}    \\vskip 1em {\\large \\@author \\par}    \\vskip 1em    {\\small \\@date \\par} \\end{center}}\\makeatother\\title{Research Statement}\\author{}\\date{}\\begin{document}"

  let l:t0 = reltimefloat(reltime())
  let l:output = matchstrlist([l:input], l:pat, #{submatches: v:true})
  let l:t1 = reltimefloat(reltime())

  let l:timing = l:t1 - l:t0
  echo "Timing:" l:timing
  return l:timing
endfunction

nnoremap <c-T> <cmd>call Testing()<cr>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions