Last updated

From 11.34s to 0.625s for opening a .rb file in Vim

Would you believe me if I told you that opening a simple Ruby file on my 2011 MacBook Pro takes 11.34 seconds?

To test this, I’ve used this command:

$ vim --startuptime log-before.txt app/models/user.rb

This command will time everything that Vim does until the file is ready for you to edit down to the millisecond. This is a great way to find out what’s slowing things down.

I’ll highlight the most interesting parts of log-before.txt here:

  000.028  000.028: --- VIM STARTING ---
 6643.597  5496.976  5496.976: sourcing /usr/share/vim/vim73/ftplugin/ruby.vim
11262.686  3963.993  3963.993: sourcing /Users/ariejan/.vim/bundle/vim-css-color/after/syntax/css.vim
11263.907  3976.912  004.334: sourcing /usr/share/vim/vim73/syntax/html.vim
11263.997  3977.361  000.449: sourcing /usr/share/vim/vim73/syntax/xhtml.vim
11340.533  000.004: --- VIM STARTED ---

These are the big spenders of loading a ruby file. Firstly there is ruby.vim taking about 5.4 seconds to load. Then there is css.vim taking another 3.9 seconds - and this file doesn’t even include CSS!

These two time sinking hogs are keeping me back – 11 seconds at a time.

Let’s see, vim-css-color. This plugin shows color hashes in their actual colour. So #00f will have a blue background. Great when editing CSS files, but not all that import. I removed vim-css-color.

Note: the reason vim-css-color is slow with terminal vim is that is has to pre-compile colour hashes to ther xterm escape code equivalents. This is pretty time consuming.

Next up: ruby.vim. Why is this so bloody slow?

As it turns out, Vim has trouble finding the right ruby for me. This can be remedied by adding the following snippet to your ~/.vimrc. It sets a logical search path for ruby:

if !empty($MY_RUBY_HOME)
 let g:ruby_path = join(split(glob($MY_RUBY_HOME.'/lib/ruby/*.*')."\n".glob($MY_RUBY_HOME.'/lib/ruby/site_ruby/*'),"\n"),',')
endif

Again I ran my timer command (full output):

$ vim --startuptime log-after app/models/user.rb

Now look at that:

000.034  000.034: --- VIM STARTING ---
107.182  000.834  000.834: sourcing /usr/share/vim/vim73/ftplugin/ruby.vim
625.001  000.003: --- VIM STARTED ---

Yes, that is 0.625 seconds! I’m a happy ruby coder again.

Tags: Ruby vim