From 842af06c5d7aa3aeb4cfbee6272661856048714c Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Mon, 27 Jun 2022 17:06:44 +0200 Subject: [PATCH] completions/git: Cache subcommand v2 This is sort of slow because it's called hundreds of times. We used to have a cache, introduced in ad9b4290e, but it was removed in fee5a9125a581598a7491ab83006247b62347041 because it had false-positives. So what we do, because the issue is that this is called hundreds of times per-commandline, we cache it keyed on the commandline. This speeds up `complete -C'git sta'` by a factor of 2.3x. --- share/completions/git.fish | 15 +++++++++++++++ tests/checks/git.fish | 2 ++ 2 files changed, 17 insertions(+) diff --git a/share/completions/git.fish b/share/completions/git.fish index ad7ef5bfb..28cc976b8 100644 --- a/share/completions/git.fish +++ b/share/completions/git.fish @@ -565,6 +565,19 @@ end function __fish_git_needs_command # Figure out if the current invocation already has a command. + # + # This is called hundreds of times and the argparse is kinda slow, + # so we cache it as long as the commandline doesn't change. + set -l cmdline "$(commandline -c)" + if set -q __fish_git_cmdline; and test "$cmdline" = "$__fish_git_cmdline" + if set -q __fish_git_cmd[1] + echo -- $__fish_git_cmd + return 1 + end + return 0 + end + set -g __fish_git_cmdline $cmdline + set -l cmd (commandline -opc) set -e cmd[1] argparse -s (__fish_git_global_optspecs) -- $cmd 2>/dev/null @@ -576,9 +589,11 @@ function __fish_git_needs_command set -q _flag_info_path; and return 1 if set -q argv[1] # Also print the command, so this can be used to figure out what it is. + set -g __fish_git_cmd $argv[1] echo $argv[1] return 1 end + set -g __fish_git_cmd return 0 end diff --git a/tests/checks/git.fish b/tests/checks/git.fish index 6c6c29701..0ca313e1a 100644 --- a/tests/checks/git.fish +++ b/tests/checks/git.fish @@ -139,3 +139,5 @@ end $fish -c 'complete -C "git -C ./.gi"' # CHECK: ./.git/ Directory + +$fish -c 'complete -C "git diff -c"'