Skip to content
Myr

[bash-completion] Optimize __fzf_orig_completion_filter

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Commit d4ad4a25 slowed loading of completion.bash significantly (on my laptop from 10 ms to 30 ms), then 54891d11 improved that (to 20 ms) but it still stands out as the heavy part of my .bashrc. Rewriting __fzf_orig_completion_filter to pure bash without forking to sed/awk brings this back under 10 ms. before: $ HISTFILE=/tmp/bashhist hyperfine 'bash --rcfile shell/completion.bash -i' Benchmark #1: bash --rcfile shell/completion.bash -i Time (mean ± σ): 21.2 ms ± 0.3 ms [User: 24.9 ms, System: 6.4 ms] Range (min … max): 20.7 ms … 23.3 ms 132 runs after: $ HISTFILE=/tmp/bashhist hyperfine 'bash --rcfile shell/completion.bash -i' Benchmark #1: bash --rcfile shell/completion.bash -i Time (mean ± σ): 9.6 ms ± 0.3 ms [User: 8.0 ms, System: 2.2 ms] Range (min … max): 9.3 ms … 11.4 ms 298 runs

authored by Tomas Janousek · committed by Junegunn Choi · parent 218b3c8 · Browse files · 1 file+16 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Fixes: d4ad4a25db5f ("[bash-completion] Fix default alias/variable completion")
  • Fixes: 54891d11e09d ("[bash-completion] Minor optimization")

Diff stats

1 file+16 added−8 removed

  • other:+16 added−8 removed

1 file+16 added−8 removed

modified:shell/completion.bash+16 added-8 removed
44 unmodified lines
45 fzf "$@"46 fi47}4849__fzf_orig_completion_filter() {50 sed '/ -F/!d; / _fzf/d; s/^\(.*-F\) *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\3="\1 %s \3 #\2"; [[ "\1" = *" -o nospace "* ]] \&\& [[ ! "$__fzf_nospace_commands" = *" \3 "* ]] \&\& __fzf_nospace_commands="$__fzf_nospace_commands \3 ";/' |51 awk -F= '{OFS = FS} {gsub(/[^A-Za-z0-9_= ;]/, "_", $1);}1'49__fzf_orig_completion() {50 local l comp f cmd51 while read -r l; do52 if [[ "$l" =~ ^(.*\ -F)\ *([^ ]*).*\ ([^ ]*)$ ]]; then53 comp="${BASH_REMATCH[1]}"54 f="${BASH_REMATCH[2]}"55 cmd="${BASH_REMATCH[3]}"56 export "_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}=${comp} %s ${cmd} #${f}"57 if [[ "$l" = *" -o nospace "* ]] && [[ ! "$__fzf_nospace_commands" = *" $cmd "* ]]; then58 __fzf_nospace_commands="$__fzf_nospace_commands $cmd "59 fi60 fi61 done62}6364_fzf_opts_completion() {65 local cur prev opts
80 unmodified lines
146 _completion_loader "$@"147 ret=$?148 # _completion_loader may not have updated completion for the command149 if [ "$(complete -p "$orig_cmd" 2> /dev/null)" != "$orig_complete" ]; then140 eval "$(complete | command grep " -F.* $orig_cmd$" | __fzf_orig_completion_filter)"150 __fzf_orig_completion < <(complete -p "$orig_cmd" 2> /dev/null)151 if [[ "$__fzf_nospace_commands" = *" $orig_cmd "* ]]; then152 eval "${orig_complete/ -F / -o nospace -F }"153 else154 eval "$orig_complete"
160 unmodified lines
315 ln ls mv open rm rsync scp316 svn tar unzip zip"317318# Preserve existing completion309eval "$(complete |310 sed -E "/ ($(echo $d_cmds $a_cmds | sed 's/ /|/g; s/+/\\+/g'))$/"'!d' |311 __fzf_orig_completion_filter)"319__fzf_orig_completion < <(complete -p $d_cmds $a_cmds 2> /dev/null)320321if type _completion_loader > /dev/null 2>&1; then322 _fzf_completion_loader=1323fi
36 unmodified lines
360 echo "usage: ${FUNCNAME[0]} path|dir|var|alias|host|proc COMMANDS..."361 return 1362 fi363 shift356 eval "$(complete -p "$@" 2> /dev/null | __fzf_orig_completion_filter)"364 __fzf_orig_completion < <(complete -p "$@" 2> /dev/null)365 for cmd in "$@"; do366 case "$kind" in367 dir) __fzf_defc "$cmd" "$fn" "-o nospace -o dirnames" ;;368 var) __fzf_defc "$cmd" "$fn" "-o default -o nospace -v" ;;
12 unmodified lines