There are a bunch of reasons why I like using Vim as my main editor. Speed and customization are two of them, but if I had to decide between the two, I rather have speed. This is a story about how you can use Vim profiling to catch slow plugins.
The other day I found myself opening a diff file that took a very long time to load. The file had 58187 (this number will be important later on) lines in it but I never thought Vim would choke with something that was less than 2M size.
Googling around I found that you can very easily profile Vim, to see where time is spent while loading a file. All I had to do was to
Open Vim and start the profiling
:profile start /tmp/profile.log :profile func * :profile file *
This tells Vim to save the results of the profile into
/tmp/profile.logand to run the profile for every file and function.
Then do the action that was taking a long time in Vim, in my case opening the diff file.
:edit /tmp/file.diff :profile pause :q!
profile.logfile only gets generated until you close Vim.
Now you can open
profile.logand analyze the data
There is a lot of information in this file but you can start by focusing on the
Total time. In my case there was a clear offender with a total time of more than 14 seconds!
FUNCTION <SNR>24_PreviewColorInLine() Called 58187 times Total time: 14.430544 Self time: 2.961442
Remember the number of lines in the file. For me it was interesting to see that the function gets called just as many times.
Finding out where this function is defined is very easy
Thanks to the
<SNR>tag and the number right after it. You simply need to run
:scriptnamesand scroll until you find the index number you are looking for, in my case 24.
I personally don’t think the benefit that a plugin can bring is acceptable at the cost of making trivial functionality like opening a file take as long as quarter of a minute so I removed it and opened an issue explaining the problem on GitHub. But with the knowledge of which function is causing the problem you can get to the task of fixing it as well.
Update: The issue was never addressed so maybe the project is unmaintained.