Vim slow at rendering the buffer with LaTeX syntax highlighting

gvimlatexvimvim-plugins

After fixing a hard-drive failure, I am installing GVIM on my Thinkpad X230T laptop running Windows 10. Things are working well except for the rending speed of buffers for *.tex files. The problem is: with syntax on, the buffer of latex renders exceptionally slow. Here goes a list of "features":

  1. It takes seconds to refresh a buffer of size 1920*1080 (in pixels) when I press <C-f>;
  2. If I shrink the size of the buffer, the rendering gets faster. Still, I shall experience a second lag at minimum. During the lag, I can only see a blank buffer.
  3. Text operations are also slow. Simply starting a new line below by pressing o shall also end up with a one-second-lag. This is largely due to the rendering of the whole buffer after adding that single line: all the lines that follows shall be refreshed for their "new" position.

As sample screencast:
enter image description here

I have applied all the suggested options mentioned in this post (https://stackoverflow.com/questions/8300982/vim-slow-running-latex-files), yet I still get the same slow rendering speed.

I was only experiencing this exceptional slowdown on my X230T. With exactly the same _vimrc on the other two desktop machines, I don't even experience a tiny lag in the rendering speed. Nor did I experience the rendering problem with previous installation of Windows 7 and 8 on the same X230T laptop.

Lastly, for hard drive, I am having a SSD on my X230T, which worked pretty well so far; and I have HDD on both the other two desktop machines.

Any advice would be very helpful!

Thanks a lot!

All the best,

-Linfeng

Best Answer

The problem is, vim's regular expression engine is really slow, and I guess latex is pretty demanding on regex.

I was able to get vim a bit faster by doing:

:syn clear texSectionFold
:syn clear texPreamble

These were the main offending syntax group regexes.

I found them by doing:

:syntime on

Now press ctrl+L a ton of times and wait for vim to catch up. Then:

:syntime report

This gave the following output:

  TOTAL      COUNT  MATCH   SLOWEST     AVERAGE   NAME               PATTERN
  8.903872   1911   52      0.017387    0.004659  texSectionFold     \v%(%(\\begin\{document\}.*$\n)@<=^|\\section)
  4.979438   1859   0       0.016382    0.002679  texPreamble        \v%(\\documentclass)@=
  0.634906   1976   182     0.010863    0.000321  texEnvName         \v%(\\%(begin|end)\{)@<=\a+\*?\ze\}
  0.373173   1859   0       0.000880    0.000201  texArgsEnvNormReq  \v(\\begin\{%(theorem|lemma|proposition|corollary|conjecture|definition|remark|example|proof)\*?\}\s*)@<=\{
  0.317732   1859   0       0.000468    0.000171  texArgsEnvNormOpt  \v(\\begin\{%(theorem|lemma|proposition|corollary|conjecture|definition|remark|example|proof)\*?\}\s*)@<=\[
  0.223595   1859   0       0.000341    0.000120  texDimen           \v-?%(\.[0-9]+|([0-9]+(\.[0-9]+)?))%(pt|pc|bp|in|cm|mm|dd|cc|sp|ex|em)>
...

So you can see that those two regexes are way more expensive than others. I don't use folds, so I had no issue disabling that. Not sure why finding the preamble is so slow.

Perhaps this helps you too, @llinfeng. I hope so!

EDIT:

To run those syn clear commands when vim starts, you would need to put them in ~/.vim/after/syntax/tex.vim. It won't work in an ftplugin file, as the syntax definitions are not yet loaded at that point.

EDIT2:

Turns out some of the syntax groups I posted above came from a plugin. Disabling the plugin doesn't help, as then some other syntax groups are slow. Eesh!

My final fix, and all I ask from syntax highlight, is:

$ cat ~/.vim/after/syntax/tex.vim 
syn clear
syn match texComment   "%.*$"                                            
hi def link texComment  Comment

It's really fast.

Related Question