Friday, October 23, 2015

Why is bash-complete so slow!

So, I've been using home-brew for a little while on my macbook, and I noticed that after I installed a few things with it, my tab completion was REALLY slow.... A little poking around was in order.

First I looked in my ~/.bash_profile, and I saw the following

if [ -f $(brew --prefix)/etc/bash_completion ]; then
  . $(brew --prefix)/etc/bash_completion
That pointed me in the direction I needed to go... Lets run the subtle command to see where it points!
MacBook-Pro-2:puppet-sandbox dawiest$ brew --prefix
So lets see how many files (and lines) are contained in those files..

MacBook-Pro-2:bash_completion.d dawiest$ ls /usr/local/etc/bash_completion.d/ | wc -l
MacBook-Pro-2:puppet-sandbox dawiest$ find /usr/local/etc/bash_completion{,.d/*} |xargs  wc -l | grep total
wc: /usr/local/etc/bash_completion.d/helpers: read: Is a directory
   25412 total
Holy smokes!  186 files, with 25k lines!  No wonder it takes so long!

After I opened up the list of files in the bash_completion.d/ directory, removed the ones I didn't care about (they are all just symlinks pointing back to ../../Cellar/bash-completion/1.3/etc/bash_completion.d/), my auto-complete's in my shell are noticeably faster! 

MacBook-Pro-2:puppet-sandbox dawiest$ ls /usr/local/etc/bash_completion.d/ | wc -l
MacBook-Pro-2:puppet-sandbox dawiest$ find /usr/local/etc/bash_completion{,.d/*} |xargs  wc -l | grep total
wc: /usr/local/etc/bash_completion.d/helpers: read: Is a directory
   14115 total

50 files, and 14k lines to execute, but I think I have removed the biggest offenders.  I should figure out a way to profile each of the scripts to see how long they each take to source!  It may be better to find the biggest offenders, and remove them if they aren't part of your usual command set.  


  1. I encountered this problem as well. Without deleting any of the bash completion scripts, I was able to reduce my load time from 10 seconds to 0.25 seconds using the workaround I described on SuperUser.

    I still have some more investigating to do. I don't know why the type command is so slow in OS X compared to Linux.

    1. Interesting... So you said your delay time was when loading new terminals?

      Most of my (noticeable) delay is caused by actually tabbing to complete things (directories, etc).

    2. Yeah, mine was in loading all the completion scripts. The tab-completion part seems to work okay for me (so far, at least).

      If you're having trouble with the tab-completion running slowly, I wonder if it's due to a single completion script that's taking a while to do its work or if you have a bunch of things in your paths that it's checking.

      I know, for instance, that tab-completing scp arguments on remote servers is slow, but that's because it has to transfer all the file listings to display the completion possibilities.