Skip to content

Foldexpr_markdown very slow for some documents #162

Open
@khughitt

Description

@khughitt

When working on a mixed Markdown/LaTeX document, I noticed after a while the performance of very basic actions like deleting a word or pasting text was becoming very slow.

I profiled vim to see what the culprit was, and it turned out the be the vim-markdown (commit 091091f from 2015/01/02) Foldexpr_markdown function. After just a few seconds of profiling and performing basic text manipulation operations on a paragraph of plain-text, Foldexpr_markdown was called over 100,000 times:

FUNCTION  Foldexpr_markdown()                                                                  
Called 107204 times                                                                            
Total time:   3.945203                                                                         
 Self time:   3.945203                                                                         

count  total (s)   self (s)                                                                    
107204              0.177798     if (a:lnum == 1)                                              
                                    let l0 = ''                                                
                                else                                                           
107204              0.371518         let l0 = getline(a:lnum-1)                                
107204              0.095889     endif                                                         

107204              0.270002     let l1 = getline(a:lnum)                                      

107204              0.316180     let l2 = getline(a:lnum+1)                                    

107204              0.444422     if  l2 =~ '^==\+\s*'                                          
                                    " next line is underlined (level 1)                        
                                    return '>1'                                                
                                elseif l2 =~ '^--\+\s*'                                        
                                    " next line is underlined (level 2)                        
   72              0.000086         return '>2'                                                
                                elseif l1 =~ '^#'                                              
                                    " don't include the section title in the fold              
                                    return '-1'                                                
                                elseif l0 =~ '^#'                                              
                                    " current line starts with hashes                          
                                    return '>'.matchend(l0, '^#\+')                            
                                else                                                           
                                    " keep previous foldlevel                                  
107132              0.097830         return '='                                                
                                endif                                  

To be sure that this was indeed the issue, I disabled vim-markdown folding using let g:vim_markdown_folding_disabled=1 and the issue went away.

Unfortunately I am not sure how to reproduce the issue outside of the larger document I am working on, so I cannot pinpoint the cause of the problem.

The document is not all that special, except that is contains a couple simple LaTeX equations and images. The image blocks are the most complex bit of LaTeX, and they look something like:

\begin{wrapfigure}{r}{0.5\textwidth}                                                           
\begin{framed}\raggedleft%\centering                                                           
  \vspace{-20pt}                                                                               
  \begin{center}                                                                               
    \includegraphics[width=1\textwidth]{images/image}                       
  \end{center}                                                                                 
  \vspace{-20pt}                                                                               
  \caption{\textbf{text...}  text..
    more text...}
  \vspace{-10pt}                                                                               
\end{framed}                                                                                   
\end{wrapfigure}

Vim version:
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 27 2014 04:46:10)
Included patches: 1-560

I'm sorry I can't provide a more reproducible example. Hopefully this might give you some ideas about the underlying issue, or perhaps someone else with the same problem can help to clarify.

Let me know if there is anything else I can do to help track down the issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions