Skip to content

Commit

Permalink
Improve startup time of bash completion.
Browse files Browse the repository at this point in the history
`cargo list` takes about .15 seconds on my computer which is
substantial enough to be the slowest command run when my shell starts
according to sstephenson's bashprof.

This commit defers running `cargo list` until we need it for the first
time.
  • Loading branch information
itsjohncs committed Feb 6, 2022
1 parent 3bc0e6d commit 7f3f9ed
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/etc/cargo.bashcomp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ _cargo()
elif [[ "$cur" == +* ]]; then
COMPREPLY=( $( compgen -W "$(_toolchains)" -- "$cur" ) )
else
COMPREPLY=( $( compgen -W "$__cargo_commands" -- "$cur" ) )
_ensure_cargo_commands_cache_filled
COMPREPLY=( $( compgen -W "$__cargo_commands_cache" -- "$cur" ) )
fi
else
case "${prev}" in
Expand Down Expand Up @@ -140,7 +141,8 @@ _cargo()
_filedir -d
;;
help)
COMPREPLY=( $( compgen -W "$__cargo_commands" -- "$cur" ) )
_ensure_cargo_commands_cache_filled
COMPREPLY=( $( compgen -W "$__cargo_commands_cache" -- "$cur" ) )
;;
*)
if [[ "$cmd" == "report" && "$prev" == future-incompat* ]]; then
Expand All @@ -164,7 +166,12 @@ _cargo()
} &&
complete -F _cargo cargo

__cargo_commands=$(cargo --list 2>/dev/null | awk 'NR>1 {print $1}')
__cargo_commands_cache=
_ensure_cargo_commands_cache_filled(){
if [[ -z $__cargo_commands_cache ]]; then
__cargo_commands_cache="$(cargo --list 2>/dev/null | awk 'NR>1 {print $1}')"
fi
}

_locate_manifest(){
cargo locate-project --message-format plain 2>/dev/null
Expand Down

0 comments on commit 7f3f9ed

Please sign in to comment.