Skip to content

Commit 7f3f9ed

Browse files
committed
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. This commit defers running `cargo list` until we need it for the first time.
1 parent 3bc0e6d commit 7f3f9ed

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)