Skip to content

about slow syntax highlighting #2877

@ces42

Description

@ces42

Description

vimtex's syntax highlighting is a bit slow at times. It's not terrible but if I open a large tex file and scroll up and down with my touchpad it is noticably not smooth. I've tried to look at the output of :syntime report and see if there's anything that can be improved. Here's the output

  TOTAL      COUNT  MATCH   SLOWEST     AVERAGE   NAME               PATTERN
  0.222296   81139  74810   0.000459    0.000003  texMathDelim       [()[\]]\|\\[{}]
  0.146481   21673  49      0.000507    0.000007  texLigature        \v%(``|''|,,)
  0.105568   25166  79      0.000556    0.000004  texSpecialChar     \%(\\\@<!\)\@<=\~
  0.090826   21627  0       0.000207    0.000004  texMathZoneLI      \%(\\\@<!\)\@<=\\(
  0.084873   70591  61790   0.000521    0.000001  texMathSuperSub    [_^]
  0.060983   74427  498     0.000408    0.000001  texMathZoneTI      \\\\\|\\\$
  0.060684   38720  30212   0.000448    0.000002  texMathOper        [-+=/<>|]
  0.039987   51544  45011   0.000128    0.000001  texMathCmd         \\\a\+
  0.022891   26430  6532    0.000241    0.000001  texComment         %.*$
  0.022842   25192  16306   0.000065    0.000001  texMathDelimMod    \\\(left\|right\)\>
  0.021043   23855  4193    0.000384    0.000001  texMathGroup       \\\\\|\\}
  0.020043   32248  17368   0.000079    0.000001  texCmd             \\[a-zA-Z@]\+
  0.020011   5365   209     0.000412    0.000004  texCommentAcronym  \v<(\u|\d){3,}s?>
  0.019337   5194   3       0.000303    0.000004  texCommentURL      \w\+:\/\/[^[:space:]]\+
  0.018543   21627  0       0.000066    0.000001  texCmdConditionalINC \\\w*@ifnextchar\>
  0.016475   21627  0       0.000101    0.000001  texCmdLigature     \v\\%([ijolL]|ae|oe|ss|AA|AE|OE)\ze[^a-zA-Z@]
  0.016086   21627  0       0.000052    0.000001  texSynIgnoreZone   ^\c\s*% VimTeX: SynIgnore\%( on\| enable\)\?\s*$
  0.014915   16338  3614    0.000067    0.000001  texMathArg         \\\\\|\\}
  0.014743   21627  0       0.000047    0.000001  texCmdSpaceCode    \v\\%(math|cat|del|lc|sf|uc)code`
  0.014660   21627  38      0.000082    0.000001  texMathZoneEnv     \\begin{\z(cd\*\?\)}
  0.014640   14477  12771   0.000071    0.000001  texMathTextAfter   \w\+
  0.014227   22104  563     0.000064    0.000001  texCmdCRef         \v\\%(%(label)?c%(page)?|C)ref>
  0.014106   25136  0       0.000119    0.000001  texCmdRef          \\\(page\|eq\)ref\>
  0.014085   23605  6905    0.000066    0.000001  texCmdEnv          \v\\%(begin|end)>
  0.013869   21627  0       0.000103    0.000001  texCmdLigature     \v\\%([ijolL]|ae|oe|ss|AA|AE|OE)$
  0.013798   25136  0       0.000148    0.000001  texComment         ^\s*\\iffalse\>
  0.013527   25136  0       0.000438    0.000001  texCmdRef          \\v\?ref\>
  0.013382   25136  0       0.000124    0.000001  texComment         ^\s*%\s*!.*
  0.012572   21627  0       0.000061    0.000001  texCmdPart         \\\(front\|main\|back\)matter\>
  0.012235   25172  48      0.000079    0.000000  texSpecialChar     \\[,;:!>]
  0.011866   21654  103     0.000117    0.000001  texCmdConditional  \\\(if[a-zA-Z@]\+\|fi\|else\)\>
  0.011844   21627  0       0.000047    0.000001  texConditionalTrueZone ^\s*\\iftrue\>

First of all, I think the very slow \v%(``|''|,,) can be replaced by the equivalent \([`',]\)\1, which was slightly faster for me, averaging 4us instead of 7us.

I was very confused by \%(\\\@<!\)\@<=\~. Am I correct in understanding that

  • it is equivalent to \\\@<!\~
  • the point of making it more complicated is that it will match faster (with a naive regex engine): After finding a ~, it will only try to check if there's a backslash before the ~ once, instead of trying to match every substring ending before ~ against the regex \\?
    If so then the same behavior could be achieved with \\\@1<!\~ which looks simpler. Unfortunately it doesn't seem to give a speedup.

Another point is that this regex will parse something like a\\~b wrongly. This is more relevant for parsing something like \\\(a^2\) -- this is valid latex but vimtex's highlighting currently doesn't recognize the math mode (OTOH I don't know why anyone would ever write that). The regex \%(\\\@!\%(\\\\\)*)\@<=\\( would fix this, checking if there's an even number of backslashes before the \(. Same goes for detecting ~. The performance of this seems to be slightly worse than \%(\\\@!)\@<=\\( though. I got 11us vs 9us.

Do you use a latexmkrc file?

No

VimtexInfo

System info:
  OS: Ubuntu 23.10
  Vim version: NVIM v0.10.0-dev-2175+g85a041716
  Has clientserver: true
  Servername: /run/user/1000/nvim.242708.0

VimTeX project: m
  base: m.tex
  root: /home/ca/vim
  tex: /home/ca/vim/m.tex
  main parser: current file verified
  document class: article
  packages: accents aliascnt aliasctr amsbsy amsfonts amsgen amsmath amsopn amssymb amstext amsthm atbegshi atbegshi-ltx atveryend atveryend-ltx autonum auxhook bigintcalc bitset calc cleveref color csquotes enumitem epstopdf-base etex etextools etoolbox geometry gettitlestring graphics graphicx hycolor hypcap hyperref iftex ifthen ifvtex infwarerr inputenc intcalc keyval kvdefinekeys kvoptions kvsetkeys letltxmacro ltxcmds mathrsfs mathtools mhsetup mleftright nameref parseargs pdfescape pdftexcmds pgf pgfcomp-version-0-65 pgfcomp-version-1-18 pgfcore pgffor pgfkeys pgfmath pgfrcs pgfsys refcount rerunfilecheck rotating textpos tgpagella thm-amsthm thm-autoref thm-kv thm-listof thm-patch thm-restate thmtools tikz tikz-cd todonotes trig uniquecounter url xcolor xkeyval
  source files:
    m.tex
    ../texmf/tex/latex/preamble.tex
  compiler: latexmk
    engine: -pdf
    options:
      -verbose
      -file-line-error
      -synctex=1
      -interaction=nonstopmode
    callback: 1
    continuous: 1
    executable: latexmk
  viewer: Zathura
    xwin id: 0
  qf method: LaTeX logfile

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions