Skip to content

Commit bdb51e7

Browse files
committed
Auto merge of #10365 - itsjohncs:completion-speedup, r=alexcrichton
Improve startup time of bash completion. `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](https://github.com/sstephenson/bashprof). This commit defers running `cargo list` until we need it for the first time. # Testing After starting a new shell (which has loaded cargo's bash completion) type `cargo <TAB><TAB>` and see the output matches the output prior to this change (ie: the commands given by `cargo list`). You should observe a small delay. You should observe no such delay on subsequent attempts within the same session.
2 parents 3bc0e6d + 7f3f9ed commit bdb51e7

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/etc/cargo.bashcomp.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ _cargo()
105105
elif [[ "$cur" == +* ]]; then
106106
COMPREPLY=( $( compgen -W "$(_toolchains)" -- "$cur" ) )
107107
else
108-
COMPREPLY=( $( compgen -W "$__cargo_commands" -- "$cur" ) )
108+
_ensure_cargo_commands_cache_filled
109+
COMPREPLY=( $( compgen -W "$__cargo_commands_cache" -- "$cur" ) )
109110
fi
110111
else
111112
case "${prev}" in
@@ -140,7 +141,8 @@ _cargo()
140141
_filedir -d
141142
;;
142143
help)
143-
COMPREPLY=( $( compgen -W "$__cargo_commands" -- "$cur" ) )
144+
_ensure_cargo_commands_cache_filled
145+
COMPREPLY=( $( compgen -W "$__cargo_commands_cache" -- "$cur" ) )
144146
;;
145147
*)
146148
if [[ "$cmd" == "report" && "$prev" == future-incompat* ]]; then
@@ -164,7 +166,12 @@ _cargo()
164166
} &&
165167
complete -F _cargo cargo
166168

167-
__cargo_commands=$(cargo --list 2>/dev/null | awk 'NR>1 {print $1}')
169+
__cargo_commands_cache=
170+
_ensure_cargo_commands_cache_filled(){
171+
if [[ -z $__cargo_commands_cache ]]; then
172+
__cargo_commands_cache="$(cargo --list 2>/dev/null | awk 'NR>1 {print $1}')"
173+
fi
174+
}
168175

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

0 commit comments

Comments
 (0)