vimtex icon indicating copy to clipboard operation
vimtex copied to clipboard

Lag, (probably not related with known matchparen issue

Open 945fc41467 opened this issue 1 year ago • 4 comments

Description

Using basic motion key lag on certain specific places when vimtex is activated

For example ([ ] representing the position of the cursor) :

  \begin{Defin[i]tion}[Produit matriciel :]
    Soient $A,[ ]B \in M_{m;l}(\R)×M_{l;n}(\R)$, $A \cdot B = 
    \left(\sum_{k=1}^{l}{a_{i;k}\cdot b_{k;j}}\right)_{
      {i\in\llbracket 1; m \rrbracket}\atop
      {j\in\llbracket 1; n \rrbracket}}$
  \end{Definition}

the switch between [ ] and [i] is ok, but it lag in the reverse order.

You can reproduce it with your minimal example :

\documentclass{minimal}
\begin{document}
Hello world!
\end{document}

Just try to navigate in the text with hjkl keys.

I tried with let g:vimtex_matchparen_enabled = 0. It doesn’t change anything. I added let loaded_matchparen = 1. Still the same. The use of vim-matchup doesn’t change anything. There is no lag if vimtex is disabled.

I use nvim, with neovide.

Here is my config:

" VIM Configuration

" --Thème
let &colorcolumn=join(range(81,160),",")

" -- Affichage
set title           " Met a jour le titre de votre fenêtre ou de votre terminal
set number          " Affiche le numéro des lignes
set textwidth=80    " Affiche les lignes trop longues sur plusieurs lignes
set linebreak       " Coupe le mot entier
set scrolloff=3     " Minimum de 3 lignes autour du curseur lors du scroll
set lines=80 columns=100
set cursorline

" -- Recherche
set ignorecase      " Ignore la casse lors d'une recherche…
set smartcase       " …sauf si la recherche contient une majuscule 

" -- Clipboard

set clipboard+=unnamedplus

" -- Beep
"set visualbell            " Empêche Vim de beeper
"set noerrorbells          " Empêche Vim de beeper

" Active le comportement 'habituel' de la touche retour en arrière
let mapleader = ","

" --Coloration syntaxique
filetype plugin on
filetype indent on
set listchars=tab:▸-,extends:>,precedes:<,leadmultispace:·,nbsp:~
autocmd BufRead,BufNewFile *.txt,*.md setlocal listchars+=multispace:·
set list
"autocmd BufRead,BufNewFile *.txt,*.md highlight NBSP guibg=darkblue
autocmd BufRead,BufNewFile *.txt,*.md syn match NBSP '\%xa0' conceal cchar=~
autocmd BufRead,BufNewFile *.txt,*.md syn match NNBSP '\%u202F' conceal cchar=·
" -- Typo
autocmd BufRead,BufNewFile *.txt,*.md highlight typo guibg=darkred
autocmd BufRead,BufNewFile *.txt,*.md syn match typo /\%u202F:/
autocmd BufRead,BufNewFile *.txt,*.md syn match typo /\%xa0[;?!]/
autocmd BufRead,BufNewFile *.txt,*.md syn match typo /—\%xa0.*\%xa0—/


"highlight ColorColumn guibg=#010101
highlight NbSp ctermbg=white guibg=green
match NbSp /\%xa0/


" --Tabulations
set tabstop=2 " Largeur de tabulation
set shiftwidth=2
set expandtab " Remplacer les tabulation par des espaces


:imap ’’ <Esc>
:map ’’ <Esc>


set foldmethod=indent
set foldlevelstart=99
set foldlevel=99
set autochdir
set signcolumn=yes

" -- Autocompletion
" Use <tab> to trigger completion.
inoremap <silent><expr> <tab> coc#refresh()

" Make <space> to accept selected completion item or notify coc.nvim to format
" <C-g>u breaks current undo, please make your own choice
inoremap <silent><expr> <C-space>
      \ coc#pum#visible() ? coc#pum#confirm():
      \                     "\<C-g>u\<C-space>"

" Use <C-t> for trigger completion with characters ahead and navigate
inoremap <silent><expr> <C-t>
      \ coc#pum#visible() ? coc#pum#next(1) :
      \                     CheckBackspace() ? "\<C-t>" :
      \                                        coc#refresh()
inoremap <expr><C-s> coc#pum#visible() ? coc#pum#prev(1) : "\<C-s>"

function! CheckBackspace() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

" Remap keys for applying refactor code actions
nmap <silent> <leader>re <Plug>(coc-codeaction-refactor)
xmap <silent> <leader>r  <Plug>(coc-codeaction-refactor-selected)
nmap <silent> <leader>r  <Plug>(coc-codeaction-refactor-selected)

" Use <TAB> to trigger orthograph corrector
nmap <TAB>  <Plug>(coc-codeaction-cursor)

"hi LanguageToolGrammarError  guisp=green gui=undercurl guifg=NONE guibg=NONE ctermfg=white ctermbg=blue term=underline cterm=none
"hi GrammalecteSpellingError guisp=red  gui=undercurl guifg=NONE guibg=NONE ctermfg=white ctermbg=red  term=underline cterm=none
":let g:grammalecte_win_height=-1
"set spl=fr spell

" -- Latex configuration
" Viewer options: One may configure the viewer either by specifying a built-in
" with a generic interface:
"let g:vimtex_view_general_viewer = 'evince'
"let g:vimtex_view_general_options = '--unique file:@pdf\#src:@line@tex'
let loaded_matchparen = 1
let g:vimtex_matchparen_enabled = 0
let g:matchup_override_vimtex = 1
let g:matchup_matchparen_deferred = 1

" -- csv configuration
let g:csv_bind_B = 1
let g:csv_start = 1
let g:csv_end = 100
let g:csv_arrange_align = 'l*'

call plug#begin('~/.local/share/nvim/site/plugged')
Plug 'rakr/vim-one'
Plug 'altercation/solarized'
Plug 'vim-airline/vim-airline'
Plug 'neovim/neovim-ruby'
"Plug 'vim-scripts/LustyExplorer'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'chrisbra/csv.vim'
Plug 'lervag/vimtex'
Plug 'andymass/vim-matchup'
Plug 'michamos/vim-bepo'
call plug#end()


colorscheme one

Steps to reproduce

No response

Expected behavior

No response

Actual behavior

No response

Do you use a latexmkrc file?

For the moment I use default configuration. I am still trying to learn how it works

VimtexInfo

System info:
  OS: Manjaro Linux
  Vim version: NVIM v0.9.5
  Has clientserver: true
  Servername: /run/user/1000/nvim.79898.0

VimTeX project: example
  base: example.tex
  root: ~/
  tex: ~/example.tex
  main parser: current file verified
  document class: extarticle
  packages: authoraftertitle derivative enumitem fancyhdr fontspec geometry linegoal mathtools multicol ntheorem paracol polyglossia siunitx stmaryrd titlesec unicode-math xcolor xltabular
  compiler: latexmk
    engine: -pdf
    options:
      -verbose
      -file-line-error
      -synctex=1
      -interaction=nonstopmode
    callback: 1
    continuous: 1
    executable: latexmk
  viewer: General
  qf method: LaTeX logfile

945fc41467 avatar May 08 '24 10:05 945fc41467

Using basic motion key lag on certain specific places when vimtex is activated …

You can reproduce it with your minimal example : …

I'm sorry if I'm daft, but the minimal example here that you claim should reproduce this is:

\documentclass{minimal}
\begin{document}
Hello world!

\begin{Definition}[Produit matriciel :]
  Soient $A,█B \in M_{m;l}(\R)×M_{l;n}(\R)$, $A \cdot B = 
  \left(\sum_{k=1}^{l}{a_{i;k}\cdot b_{k;j}}\right)_{
    {i\in\llbracket 1; m \rrbracket}\atop
    {j\in\llbracket 1; n \rrbracket}}$
\end{Definition}

\end{document}

where indicates the initial cursor position. You are saying that moving the cursor with k is ok, but moving it back down with j is laggy. Did I understand correctly?

lervag avatar May 08 '24 16:05 lervag

For the record: I can not reproduce this lag on my end.

lervag avatar May 08 '24 16:05 lervag

That’s right. I tried with you example and I have the same behavior. I didn’t identify the logic in which move is laggy or not. It look rather random. But the navigation is very difficult like this.

945fc41467 avatar May 19 '24 08:05 945fc41467

Ok. So, the main approahc for helping you with this is to have you create a minimal configuration that reproduces this. But before we start doing that, can you do this:

  1. Open the example LaTeX file provided above with neovim as you usually do.
  2. Report output of :scriptnames (I would prefer if you could paste the output verbatim in your reply instead of using a screenshot).
  3. :profile start profile.log
  4. :profile func *
  5. :profile file *
  6. Navigate for a while, make sure you experience the lag.
  7. :profile pause
  8. :qa!
  9. Please provide the contents of profile.log here.

I may be able to figure out your problem from this output.

lervag avatar May 19 '24 21:05 lervag

@lervag I also experience lags when moving cursors via ijkl, which is mitigated after disabling vimtex_matchparen_enabled, but not completely resolved. Profiling as you noted above doesn't show related function calls, so I tried to comment out vimtex components.

By trial-and-error, I found that commenting out the following line fixes the lag https://github.com/lervag/vimtex/blob/5ac62e0315c6f54f53a7d6da7c485cf8e9cf7240/autoload/vimtex/state/class.vim#L49 since the function returns w/o the compiler field, setting vimtex_compiler_enabled = 0 also fixes the lag. you may want to check in details and @945fc41467 may want to test if the lag issue is also fixed

moetayuko avatar Jul 31 '24 09:07 moetayuko

By trial-and-error, I found that commenting out the following line fixes the lag

Interesting. That is definitely not a direct cause, though. Instead, I think this means that there is some syntax package that is slow here.

If you clean your project (e.g. \lc) - do you still see the lag?

lervag avatar Jul 31 '24 09:07 lervag

To be more specific/clear: The line you indicated will have VimTeX source the .fls-file for all packages used by your document. The package list is used by the syntax engine - we only load the syntax packages that are used by the document. Thus, it is a high probability that your lag is related to one of these syntax packages.

lervag avatar Jul 31 '24 10:07 lervag

By trial-and-error, I found that commenting out the following line fixes the lag

Interesting. That is definitely not a direct cause, though. Instead, I think this means that there is some syntax package that is slow here.

If you clean your project (e.g. \lc) - do you still see the lag?

no, the lag is gone after clean the project and restart nvim, and is back once I compile it

To be more specific/clear: The line you indicated will have VimTeX source the .fls-file for all packages used by your document. The package list is used by the syntax engine - we only load the syntax packages that are used by the document. Thus, it is a high probability that your lag is related to one of these syntax packages.

Makes sense, I'll remove individual packages and let you know once I find the buggy one. BTW is it possible that the lag increases as the number of packages grows?

moetayuko avatar Jul 31 '24 10:07 moetayuko

@lervag the lagging package is fontawesome5 in my case, and is fixed via vimtex_syntax_conceal_disable = 1. However, I really love this feature, is there a chance to improve the performance of concealing?

moetayuko avatar Jul 31 '24 11:07 moetayuko

To be more specific/clear: The line you indicated will have VimTeX source the .fls-file for all packages used by your document. The package list is used by the syntax engine - we only load the syntax packages that are used by the document. Thus, it is a high probability that your lag is related to one of these syntax packages.

Makes sense, I'll remove individual packages and let you know once I find the buggy one. BTW is it possible that the lag increases as the number of packages grows?

The lag will grow with the number of packages, but most packages really should not add any significant or noticable lag.

@lervag the lagging package is fontawesome5 in my case, and is fixed via vimtex_syntax_conceal_disable = 1. However, I really love this feature, is there a chance to improve the performance of concealing?

Ah. Well, that does explain the problem. I've never used this myself.

So, this syntax package adds 1456 new syntax rules to match the various symbols from fontawesome5. And all of these rules are slightly advanced regular expressions that are hard to optimize further.

I might be able to make minor adjustment that could be significant, but then it would be useful to get a good example document first to use as a benchmarking document. If you would be able to help provide that, then I can see if I'm able to improve it.

lervag avatar Jul 31 '24 20:07 lervag

I might be able to make minor adjustment that could be significant, but then it would be useful to get a good example document first to use as a benchmarking document. If you would be able to help provide that, then I can see if I'm able to improve it.

the official package documentation is sufficient to reproduce for me https://mirrors.ctan.org/fonts/fontawesome5/doc/fontawesome5.tex

when continuously moving the cursor by holding one of ijkl, it sometimes "jumps" on the screen if concealing is enabled

moetayuko avatar Aug 01 '24 04:08 moetayuko

Well, on my end there is no lag on this document. Perhaps my computer is much faster? Just to be sure, does the result depend on let g:vimtex_matchparen_enabled on your side? I.e., does the lag only depend on the fontawesome package? You should be able to test this by removing the \usepackage{fontawesome} at the top of the document.

lervag avatar Aug 14 '24 21:08 lervag

Well, on my end there is no lag on this document. Perhaps my computer is much faster?

may also related to other enabled plugins, here's my config for reference https://github.com/moetayuko/nvimrc

Just to be sure, does the result depend on let g:vimtex_matchparen_enabled on your side? I.e., does the lag only depend on the fontawesome package? You should be able to test this by removing the \usepackage{fontawesome} at the top of the document.

yes, I have already set vim.g.vimtex_matchparen_enabled = 0 and there's still a bit of lag (but much better). the lag is completely gone after disabling concealing for fontawesome5 https://github.com/moetayuko/nvimrc/blob/ef075783c3ed2cb9bb29a894212c313cea731d3a/lua/plugins/tex.lua#L24-L28

moetayuko avatar Aug 15 '24 03:08 moetayuko

To be sure it's not related to any other plugin, let's try with a real minimal config. Create a file test.vim like this:

set nocompatible
" UPDATE THE FOLLOWING TWO LINES TO THE CORRECT PATHS!
set runtimepath^=~/.local/plugged/vimtex
set runtimepath+=~/.local/plugged/vimtex/after
filetype plugin indent on
syntax enable

let g:vimtex_cache_root = "."
let g:vimtex_cache_persistent = v:false
let g:vimtex_matchparen_enabled = 0

set nomore
set spell
set conceallevel=2

" Use a more colorful colorscheme
colorscheme morning

highlight Conceal ctermfg=4 ctermbg=7 guibg=NONE guifg=blue
highlight texCmdRef ctermfg=6 guifg=cyan

Then do nvim --clean -u test.vim mwe.tex, where mwe.tex is one of the files in which you find the laggy behaviour. If you do this, do you still experience the lag?

By the way, regarding your real config: Please don't use the opts. Use the init function to configure VimTeX. That is, use this:

return {
  {
    "lervag/vimtex",
    vscode = true,     -- What is this?
    lazy = false,      -- Only necessary if you use default lazy with lazy.nvim
    init = function()
      -- VimTeX config goes here
    end,
  },
  -- ...
}

lervag avatar Aug 15 '24 12:08 lervag

To be sure it's not related to any other plugin, let's try with a real minimal config. Create a file test.vim like this:

set nocompatible
" UPDATE THE FOLLOWING TWO LINES TO THE CORRECT PATHS!
set runtimepath^=~/.local/plugged/vimtex
set runtimepath+=~/.local/plugged/vimtex/after
filetype plugin indent on
syntax enable

let g:vimtex_cache_root = "."
let g:vimtex_cache_persistent = v:false
let g:vimtex_matchparen_enabled = 0

set nomore
set spell
set conceallevel=2

" Use a more colorful colorscheme
colorscheme morning

highlight Conceal ctermfg=4 ctermbg=7 guibg=NONE guifg=blue
highlight texCmdRef ctermfg=6 guifg=cyan

Then do nvim --clean -u test.vim mwe.tex, where mwe.tex is one of the files in which you find the laggy behaviour. If you do this, do you still experience the lag?

Yes! The original https://mirrors.ctan.org/fonts/fontawesome5/doc/fontawesome5.tex was just too short to expose the problem with your clean vimrc.

I simply duplicated the paragraph L59-L61 for 100 times to make it larger. Then, gg to move to top and hold ctrl-d to scroll down. There's a noticable lag when scrolling, an is gone w/ let g:vimtex_syntax_packages = {'fontawesome5': {'conceal': 0}}

moetayuko avatar Aug 15 '24 13:08 moetayuko

Thanks. I can "barely" reproduce this. That is, I do notice a very slight lag on my side as well if I repeat those lines. I made an automated test to measure things, and I do notice that the fontawesome rules will significantly impact the syntax performance.

I've now spent quite some time trying to figure out a way to improve the speed. I may have improved things by ~5 %, but unfortunately, I think the only way to significantly improve this is to reduce the number of rules. But it is not possible to reduce the number of rules without also reducing the number of supported symbols.

I mean, we could probably do some smart parsing of the document to enable only the rules that are actually in use in the document. But that seems a complicated solution, and I'm not so motivated by this.

I therefore think I'll leave this as it is now. Those who prefer less lag can disable the fontawesome support with let g:vimtex_syntax_packages = {'fontawesome5': {'conceal': 0}}.

lervag avatar Aug 16 '24 09:08 lervag

Thanks. I can "barely" reproduce this. That is, I do notice a very slight lag on my side as well if I repeat those lines. I made an automated test to measure things, and I do notice that the fontawesome rules will significantly impact the syntax performance.

I've now spent quite some time trying to figure out a way to improve the speed. I may have improved things by ~5 %, but unfortunately, I think the only way to significantly improve this is to reduce the number of rules. But it is not possible to reduce the number of rules without also reducing the number of supported symbols.

I mean, we could probably do some smart parsing of the document to enable only the rules that are actually in use in the document. But that seems a complicated solution, and I'm not so motivated by this.

I therefore think I'll leave this as it is now. Those who prefer less lag can disable the fontawesome support with let g:vimtex_syntax_packages = {'fontawesome5': {'conceal': 0}}.

Thank you for the effort in investigating this.

moetayuko avatar Aug 17 '24 01:08 moetayuko

No problem, and thanks for raising my awareness of the issue. It will help if other people report similar issues.

lervag avatar Aug 17 '24 09:08 lervag