preservim / vim-markdown

Markdown Vim Mode
4.69k stars 523 forks source link

MarkdownHighlightSources is slow on large files #544

Open g33kex opened 3 years ago

g33kex commented 3 years ago

The function MarkdownHighlightSources() seems to be slowing down vim a lot with big files. With a markdown file of 6000 lines, it takes half a second to get in and out of insert mode. It becomes worse the more there are source code blocks in the file. With 1000 code blocks, vim freezes for a few seconds when scrolling the buffer up, but it is also slow with no code blocks at all.

There is a minimal config:

set nocompatible
call plug#begin()
Plug 'plasticboy/vim-markdown'
call plug#end()

Here are the profiling results:

FUNCTION  <SNR>25_MarkdownHighlightSources()
  Defined: ~/.vim/plugged/vim-markdown/ftplugin/markdown.vim:786
 Called 14 times
 Total time:   1.252655
  Self time:   1.252655

This part seems to be the culprit:

" Look for code blocks in the current file
84252              0.123689     for line in getline(1, '$')
84238              0.540144         let ft = matchstr(line, '```\s*\zs[0-9A-Za-z_+-]*\ze.*')
185290              0.382791         if !empty(ft) && ft !~ '^\d*$' | let filetypes[ft] = 1 | endif
84252              0.074192     endfor

The whole file is read at each update to look for source code blocks. Couldn't this be limited to the area around the current viewport? Or at least add a variable to limit the maximum length of code blocks to check for so we can start the check from the current position minus this maximum length.

This really makes the editing of long files feel pretty sluggish.

jm33-m0 commented 3 years ago

I have the same issue, when editing large files it's almost unusable in insert mode, I had to disabled this plugin to edit those files.