Bash completion is slow for brew commands

When I type

brew out

and hit the tab key, I expect outdated to appear instantly. Unlike auto-completing formula names, the list of sub-command is fixed and very limited, so I should not have felt any latency. I expected less than 100ms latency, but I saw more than 1 second. I’m using Bash so there is no other things to blame like OMZ.

5 Likes

I am in the same situation and investigated the root cause.

The completion code invokes brew commands --quiet internally. Therefore you have to wait ruby interpreter to be ready and execute the commands. No doubt it’s slow.
( the code is here )

I modified the completion code in my environments like below.
It saves the brew commands result to a file and use it for subsequent completion.
It’s not fast nor ideal but acceptable for me.

if [[ "$i" -eq "$COMP_CWORD" ]]
then
  # Do not auto-complete "*instal" or "*uninstal" aliases for "*install" commands.
  # Prefix newline to prevent not checking the first command.
  local commands_cache="$(brew --prefix)/Homebrew/completions/bash/commands_cache"
  local cmds
  [[ -r "$commands_cache" ]] \
    && cmds=$(cat "$commands_cache") \
    || cmds=$'\n'"$(brew commands --quiet --include-aliases | \grep -v instal$ | tee "$commands_cache")"

  __brewcomp "$cmds"
  return
fi

That problem was known for ages, but I think no one really tried to fix it - it happens because of ruby code execution time for bash completion.
I think that solution with some kind of cache was rejected for certain reasons.