Faster Launching MacVim Windows – Don’t Fork

MacVim.jpg

I read an interesting message on the MacVim mailing list this morning about the speed of opening new windows with the mvim command - a shell command that's included in the MacVim package. What the developer (the main maintainer) did was to time several ways of opening up the window and the sub-components of that process. What he found was that the fork() was about two-thirds of the time! Surely, there is a faster way to do this.

And there is... he even had an answer: don't fork on opening a new window from the command line. The change to the mvim script is pretty easy. Change the script from:

  1. if [ "$gui" ]; then
  2. # Note: this isn't perfect, because any error output goes to the
  3. # terminal instead of the console log.
  4. # But if you use open instead, you will need to fully qualify the
  5. # path names for any filenames you specify, which is hard.
  6. exec "$binary" -g $opts ${1:+"$@"}
  7. else
  8. exec "$binary" $opts ${1:+"$@"}
  9. fi

to:

  1. if [ "$gui" ]; then
  2. # Note: this isn't perfect, because any error output goes to the
  3. # terminal instead of the console log.
  4. # But if you use open instead, you will need to fully qualify the
  5. # path names for any filenames you specify, which is hard.
  6. exec "$binary" -g -f $opts ${1:+"$@"} <&0 &
  7. else
  8. exec "$binary" $opts ${1:+"$@"}
  9. fi

This takes the launch time from a little over a second on my MacBook Pro to less than half a sec. OK, sure... is this that important? No, of course not, but the change is not bad, either. Not forking is a good way to keep system load down. Doesn't hurt, and that it's faster is even better.

So... if you're using MacVim - think about updating the mvim script and try it. It's a good idea.