From 7f3f9ed3e735b3053ca2c3783e493e48e09b776d Mon Sep 17 00:00:00 2001 From: itsjohncs Date: Sun, 6 Feb 2022 15:45:57 -0800 Subject: [PATCH] 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. --- src/etc/cargo.bashcomp.sh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/etc/cargo.bashcomp.sh b/src/etc/cargo.bashcomp.sh index a1034bd7d97..ae335661093 100644 --- a/src/etc/cargo.bashcomp.sh +++ b/src/etc/cargo.bashcomp.sh @@ -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 @@ -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 @@ -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