tpope / vim-markdown

Vim Markdown runtime files
1.23k stars 191 forks source link

Slow folding in medium-sized file #162

Closed ropeladder closed 4 years ago

ropeladder commented 4 years ago

I'm using vim on a 2011 netbook and it's quite slow editing markdown files with folding enabled.

I'm testing on a markdown file that is around 36kb and has around 25 headers/sections.

To test I go to a random line in the file, enter insert mode, mash the keyboard, and then hit ctrl-w a few times to delete everything I just wrote.

Profiling shows it's the MarkdownFold function:

FUNCTION  MarkdownFold()
    Defined: /usr/share/vim/vim82/ftplugin/markdown.vim:26
Called 12006 times
Total time:   4.978929
 Self time:   4.878142

count  total (s)   self (s)
12006              0.328848   let line = getline(v:lnum)

12006   0.578361   0.477574   if line =~# '^#\+ ' && s:NotCodeBlock(v:lnum)
   58              0.003087     return ">" . match(line, ' ')
11948              0.090348   endif

11948              0.373271   let nextline = getline(v:lnum + 1)
11948              1.177313   if (line =~ '^.\+$') && (nextline =~ '^=\+$') && s:NotCodeBlock(v:lnum + 1)
                                return ">1"
11948              0.082670   endif

11948              1.156591   if (line =~ '^.\+$') && (nextline =~ '^-\+$') && s:NotCodeBlock(v:lnum + 1)
                                return ">2"
11948              0.082481   endif

11948              0.114498   return "="

Is there any way to speed this up?

tpope commented 4 years ago

Probably not, folding has to look at potentially a lot of surrounding context and VimL is slow. But if you figure out any improvements do let me know.

ropeladder commented 3 years ago

Just wanted to follow up and mention that I recently found vim-fastfold and that it does a great job of speeding up text editing. (as I understand it, it stops vim from recalculating folds unless you are operating directly on a fold)

For all I know there may be some reasons why you wouldn't want to have this be default folding behavior (or would want to keep it as a separate plugin), but for me it's been a huge improvement.