Skip to content

Improve Git completion - #2145

Closed
faho wants to merge 7 commits into
fish-shell:masterfrom
faho:git-completion
Closed

faho wants to merge 7 commits into
fish-shell:masterfrom
faho:git-completion

Conversation

@faho

@faho faho commented Jun 21, 2015

Copy link
Copy Markdown
Member

No description provided.

@ridiculousfish

Copy link
Copy Markdown
Member

Thanks for this! Testing this, it subjectively feels substantially slower, and instrumenting it shows a slowdown of ~9x when tab completing as git checkout<TAB> and ~2x when as git checkout <TAB> (i.e. with a space). It looks like a main culprit is the invocation of __fish_git_aliases and __fish_git_custom_commands in __fish_git_needs_command - those result in multiple new invocations of git. Can we eliminate those?

@faho

faho commented Jun 24, 2015

Copy link
Copy Markdown
Member Author

For that case (i.e. we don't need a command), there are two things that spring to mind:

The first is splitting the if in __fish_git_needs_command like in the commit I just added - the idea is not checking aliases and custom commands unless it can't be a builtin command.

The second is saving a list of aliases and custom commands for later use, i.e. not calling git all the time but doing it once when the completion is loaded. This would speed up later completion, but the problem is that it would only update when the completion is re-loaded - which is when fish is started if I understand correctly.

Also, how are you instrumenting it? For me it's pretty quick (half a second delay at most for the case without the space), so I'm having a hard time testing.

@pickfire

Copy link
Copy Markdown
Contributor

Half a second on one computer can mean half of ten seconds on another computer. What computer are you using?

I hope that you would not make fish to be like zsh with a framework. It took me 3 and more seconds to start zsh.

@faho

faho commented Jun 24, 2015

Copy link
Copy Markdown
Member Author

An Acer Aspire 5750g, with an i7 and spinning rust (HDD), no SSD, so it's certainly not top-of-the-line. I'm using Archlinux, so I've got the newest git, linux kernel, etc.

The main issue why I brought that time up was that I can't really go by feeling since the difference between, say, 0.5 seconds, 0.4 seconds and 0.2 seconds is hard to notice. So I need to measure.

@pickfire

Copy link
Copy Markdown
Contributor

Yeah, I am using archlinux too. But I use it on raspberry pi, so it will be easy to measure, but I don't know how.

@ridiculousfish

Copy link
Copy Markdown
Member

The way I measured was to get the current time at the beginning of complete() (in complete.cpp) and then again at the end, and print out the difference. It would be nice to have better techniques than this :)

diff --git a/complete.cpp b/complete.cpp
index 1f3cc65..1db4ff8 100644
--- a/complete.cpp
+++ b/complete.cpp
@@ -1841,6 +1841,11 @@ bool completer_t::try_complete_user(const wcstring &str)

 void complete(const wcstring &cmd_with_subcmds, std::vector<completion_t> &comps, completion_request_flags_t flags)
 {
+    double before = timef();
+    if (! (flags & COMPLETION_REQUEST_AUTOSUGGESTION)) {
+        fprintf(stderr, "<%ls> enter\n", cmd_with_subcmds.c_str());
+    }
+
     /* Determine the innermost subcommand */
     const wchar_t *cmdsubst_begin, *cmdsubst_end;
     parse_util_cmdsubst_extent(cmd_with_subcmds.c_str(), cmd_with_subcmds.size(), &cmdsubst_begin, &cmdsubst_end);
@@ -2083,6 +2088,10 @@ void complete(const wcstring &cmd_with_subcmds, std::vector<completion_t> &comps
     }

     comps = completer.get_completions();
+    if (! (flags & COMPLETION_REQUEST_AUTOSUGGESTION)) {
+        double after = timef();
+        fprintf(stderr, "<%ls> %f msec\n", cmd_with_subcmds.c_str(), (after - before) * 1000.);
+    }
 }

@faho

faho commented Jun 25, 2015

Copy link
Copy Markdown
Member Author

Urgh, I just noticed I broke completion after aliases (my git config-fu was lacking).

Want me to do a fixup commit or fix all problems then create a new PR?

@ridiculousfish

Copy link
Copy Markdown
Member

You can tweak this PR, either by making a new commit or by overwriting one (i.e. commit --amend followed by push --force), whatever you prefer.

@faho

faho commented Jun 26, 2015

Copy link
Copy Markdown
Member Author

So, I've measured, and I can't find a large difference.

For the old version, I get around 210ms.
For this version, I get around 230ms.
For a hacky version I made where I basically do what __fish_seen_subcommand_from does manually, I get around 200ms.

This is with hot cache, cold cache results are all over the place (between 200 and 800ms), completely inconclusive and not relevant to practical usage.

My guess is something different:

For each completion, __fish_git_needs_command is executed twice - once for finding out if it should complete "--help" (which is incorrect, btw, it can also appear after a command), and once for all other commands.

That's not too much, considering the one git call in __fish_git_aliases.

"__fish_git_using_command" is executed a whopping 27 times per completion though (once per git-command), and the sed call got a bit more complex (btw: Which commands are okay to use in completions?), so that may be where you're seeing the slowdowns. This might also be an OSX vs Gnu/Linux issue, like gnu grep beating osx/bsd grep - maybe gnu sed beats bsd sed, too?

(The new git config | sed is about 0.003s slower for me than the old one, multiplied by 27 that's 8.1msec - measured using bash's time)

@ridiculousfish

Copy link
Copy Markdown
Member

Thanks @faho! What was the scenario you were testing?

@faho

faho commented Jun 26, 2015

Copy link
Copy Markdown
Member Author

git check<TAB>, git checkout<TAB>, git checkout <TAB> (i.e. exactly what you mentioned) in my clone of the fish repository. I also tried a few other things (aliases, other commands than checkout) to confirm.

@faho

faho commented Jul 14, 2015

Copy link
Copy Markdown
Member Author

Any more thoughts on this? The first two commits should be fairly uncontroversial and usable without the third.

I haven't come up with a way to speed it up.

@pickfire

Copy link
Copy Markdown
Contributor

For master branch, fish, version 2.2.0-53-g9cf2bb1:

<git check> 546.122074 msec
<git checkout> 372.807026 msec
<git checkout > 1153.476954 msec
<git checkout -> 1138.718128 msec

For faho/git-completion branch, commit 43a7d45:

<git check> 370.239973 msec
<git checkout> 366.889954 msec
<git checkout > 1178.724051 msec
<git checkout -> 1141.587019 msec

@faho

faho commented Jul 15, 2015

Copy link
Copy Markdown
Member Author

In case it is an issue with sed, I've just pushed a commit that replaces it.

@ridiculousfish can you re-test?

@pickfire

Copy link
Copy Markdown
Contributor

@faho I had tested your git-completion branch, commit 8aa806d, and it seems that it is a lot faster then master branch and your previous commit:

<git check> 355.481148 msec
<git checkout> 367.099047 msec
<git checkout > 1130.183935 msec
<git checkout -> 1145.460129 msec

I had also found some error reporting unused function when running make.

Thanks for showing me how to use benchmark, @ridiculousfish. But I only find the following useful:

diff --git a/complete.cpp b/complete.cpp
index 1f3cc65..c4705e6 100644
--- a/complete.cpp
+++ b/complete.cpp
@@ -1841,6 +1841,8 @@ bool completer_t::try_complete_user(const wcstring &str)

 void complete(const wcstring &cmd_with_subcmds, std::vector<completion_t> &comps, completion_request_flags_t flags)
 {
+     double before = timef();
+
     /* Determine the innermost subcommand */
     const wchar_t *cmdsubst_begin, *cmdsubst_end;
     parse_util_cmdsubst_extent(cmd_with_subcmds.c_str(), cmd_with_subcmds.size(), &cmdsubst_begin, &cmdsubst_end);
@@ -2083,6 +2085,10 @@ void complete(const wcstring &cmd_with_subcmds, std::vector<completion_t> &comps
     }

     comps = completer.get_completions();
+    if (! (flags & COMPLETION_REQUEST_AUTOSUGGESTION)) {
+        double after = timef();
+        fprintf(stderr, "<%ls> %f msec\n", cmd_with_subcmds.c_str(), (after - before) * 1000.);
+    }
 }

@faho

faho commented Aug 6, 2015

Copy link
Copy Markdown
Member Author

@ridiculousfish: Do you want this in or should I drop it?

@ridiculousfish

Copy link
Copy Markdown
Member

Please leave it open, I want to revisit it.

@pickfire

Copy link
Copy Markdown
Contributor

@ridiculousfish It seems to have be faster, is there any reason why you would not want to merge it?

faho added a commit that referenced this pull request Aug 17, 2015
This is already done by fish before calling the completion.

It breaks completion with combiners (#2025) and also with wrappers.

(This does not include git because that's better solved in #2145)
faho added 7 commits August 25, 2015 15:19
Might print unrelated crap if we try to complete while not in a git repository
I.e. not "git <TAB>", but "git some-alias <TAB>"

I broke this when fixing option-before-command
Not all of them and only those that don't accept arguments for now

Fixes fish-shell#1996
@faho

faho commented Sep 8, 2015

Copy link
Copy Markdown
Member Author

@ridiculousfish: Ping?

@pickfire

pickfire commented Sep 8, 2015

Copy link
Copy Markdown
Contributor

It seems that the completions are a bit slower on the first run but faster on most of the time.

<git check> 402.564049msc
<git check> 225.533962msc
<git checkout> 230.180025msc
<git checkout > 1154.037952msc
<git checkout > 1118.919849msc
<git checkout -> 1085.398912msc

faho added a commit to faho/fish-shell that referenced this pull request Sep 8, 2015
This is already done by fish before calling the completion.

It breaks completion with combiners (fish-shell#2025) and also with wrappers.

(This does not include git because that's better solved in fish-shell#2145)
@faho

faho commented Sep 10, 2015

Copy link
Copy Markdown
Member Author

I'm now going to cherry-pick what I believe to be the uncontested parts of this:

The rest can be left for later.

@faho

faho commented Sep 10, 2015

Copy link
Copy Markdown
Member Author

Pushed now, I'll redo the rest if there's any need.

@faho faho closed this Sep 10, 2015
@faho
faho deleted the git-completion branch September 10, 2015 13:44
@pickfire

Copy link
Copy Markdown
Contributor

In master branch, it seems a bit weird this time as it does completion twice for git check and git checkout:

[I] me@alarmpi ~/D/fish-shell (fish)> git check<git-> 58.693171msc
<git check> 241.715908msc
[I] me@alarmpi ~/D/fish-shell (fish) [1]> git checkout<git-> 56.536198msc
<git checkout> 231.436968msc
[I] me@alarmpi ~/D/fish-shell (fish) [1]> git checkout <git checkout > 1280.000925msc
[I] me@alarmpi ~/D/fish-shell (fish)> git checkout -<git checkout -> 1107.008934msc

@faho

faho commented Sep 10, 2015

Copy link
Copy Markdown
Member Author

That's weird, I'll see if I can find something.

I've now written a different __fish_git_needs_command implementation that matches more closely how git behaves (turns out subcommand-options aren't valid before the subcommands) and should be quicker since it run contains for every git command, as long as our wildcard matching is quick.

function __fish_git_needs_command
  set cmd (commandline -opc)
  if [ (count $cmd) -eq 1 ]
      return 0
  else
      set -l skip_next 1
      # Skip first word because it's "git" or a wrapper
      for c in $cmd[2..-1]
          test $skip_next -eq 0; and set skip_next 1; and continue
          # git can only take a few options before a command, these are the ones mentioned in the "git" man page
          # e.g. `git --follow log` is wrong, `git --help log` is okay (and `git --help log $branch` is superfluous but works)
          switch $c
              # General options that can still take a command
              case "--help" "-p" "--paginate" "--no-pager" "--bare" "--no-replace-objects" --{literal,glob,noglob,icase}-pathspecs  --{exec-path,git-dir,work-tree,namespace}"=*"
                  continue
              # General options with an argument we need to skip. The option=value versions have already been handled above
              case --{exec-path,git-dir,work-tree,namespace}
                  set skip_next 0
                  continue
              # General options that cause git to do something and exit - these behave like commands and everything after them is ignored
              case "--version" --{html,man,info}-path
                  return 1
              # We assume that any bare word that's not an argument to a general option is a command
              case "*"
                  return 1
          end
      end
      return 0
  end
  return 1
end

@zanchey zanchey added this to the next-2.x milestone Oct 15, 2015
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants