git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH v2] fetch: skip formatting updated refs with `--quiet`
Date: Mon, 30 Aug 2021 12:54:26 +0200	[thread overview]
Message-ID: <e5ffa17753d4aca57d486d500a2d114290361ea7.1630320848.git.ps@pks.im> (raw)
In-Reply-To: <40c385048a023dbd447c5f0b4c95ff32485e1e23.1629906005.git.ps@pks.im>

[-- Attachment #1: Type: text/plain, Size: 6463 bytes --]

When fetching, Git will by default print a list of all updated refs in a
nicely formatted table. In order to come up with this table, Git needs
to iterate refs twice: first to determine the maximum column width, and
a second time to actually format these changed refs.

While this table will not be printed in case the user passes `--quiet`,
we still go out of our way and do all these steps. In fact, we even do
more work compared to not passing `--quiet`: without the flag, we will
skip all references in the column width computation which have not been
updated, but if it is set we will now compute widths for all refs.

Fix this issue by completely skipping both preparation of the format and
formatting data for display in case the user passes `--quiet`, improving
performance especially with many refs. The following benchmark shows a
nice speedup for a quiet mirror-fetch in a repository with 2.3M refs:

    Benchmark #1: HEAD~: git-fetch
      Time (mean ± σ):     26.929 s ±  0.145 s    [User: 24.194 s, System: 4.656 s]
      Range (min … max):   26.692 s … 27.068 s    5 runs

    Benchmark #2: HEAD: git-fetch
      Time (mean ± σ):     25.189 s ±  0.094 s    [User: 22.556 s, System: 4.606 s]
      Range (min … max):   25.070 s … 25.314 s    5 runs

    Summary
      'HEAD: git-fetch' ran
        1.07 ± 0.01 times faster than 'HEAD~: git-fetch'

While at it, this patch also fixes `adjust_refcol_width()` such that it
skips unchanged refs in case the user passed `--quiet`, where verbosity
will be negative. While this function won't be called anymore if so,
this brings the comment in line with actual code. Furthermore, needless
`verbosity >= 0` checks are now removed in `store_updated_refs()`: we
never print to the `note` buffer anymore in case `verbosity < 0`, so we
won't end up in that code block anyway.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
Range-diff against v1:
1:  40c385048a ! 1:  e5ffa17753 fetch: skip formatting updated refs with `--quiet`
    @@ Commit message
               'HEAD: git-fetch' ran
                 1.07 ± 0.01 times faster than 'HEAD~: git-fetch'
     
    +    While at it, this patch also fixes `adjust_refcol_width()` such that it
    +    skips unchanged refs in case the user passed `--quiet`, where verbosity
    +    will be negative. While this function won't be called anymore if so,
    +    this brings the comment in line with actual code. Furthermore, needless
    +    `verbosity >= 0` checks are now removed in `store_updated_refs()`: we
    +    never print to the `note` buffer anymore in case `verbosity < 0`, so we
    +    won't end up in that code block anyway.
    +
         Signed-off-by: Patrick Steinhardt <ps@pks.im>
     
      ## builtin/fetch.c ##
    +@@ builtin/fetch.c: static void adjust_refcol_width(const struct ref *ref)
    + 	int max, rlen, llen, len;
    + 
    + 	/* uptodate lines are only shown on high verbosity level */
    +-	if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
    ++	if (verbosity <= 0 && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
    + 		return;
    + 
    + 	max    = term_columns();
     @@ builtin/fetch.c: static void prepare_format_display(struct ref *ref_map)
    - 		die(_("configuration fetch.output contains invalid value %s"),
    - 		    format);
    + 	struct ref *rm;
    + 	const char *format = "full";
      
     +	if (verbosity < 0)
     +		return;
     +
    - 	for (rm = ref_map; rm; rm = rm->next) {
    - 		if (rm->status == REF_STATUS_REJECT_SHALLOW ||
    - 		    !rm->peer_ref ||
    + 	git_config_get_string_tmp("fetch.output", &format);
    + 	if (!strcasecmp(format, "full"))
    + 		compact_format = 0;
     @@ builtin/fetch.c: static void format_display(struct strbuf *display, char code,
      			   const char *remote, const char *local,
      			   int summary_width)
    @@ builtin/fetch.c: static void format_display(struct strbuf *display, char code,
      
      	strbuf_addf(display, "%c %-*s ", code, width, summary);
      	if (!compact_format)
    +@@ builtin/fetch.c: static int store_updated_refs(const char *raw_url, const char *remote_name,
    + 					       "FETCH_HEAD", summary_width);
    + 			}
    + 			if (note.len) {
    +-				if (verbosity >= 0 && !shown_url) {
    ++				if (!shown_url) {
    + 					fprintf(stderr, _("From %.*s\n"),
    + 							url_len, url);
    + 					shown_url = 1;
    + 				}
    +-				if (verbosity >= 0)
    +-					fprintf(stderr, " %s\n", note.buf);
    ++				fprintf(stderr, " %s\n", note.buf);
    + 			}
    + 		}
    + 	}

 builtin/fetch.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/builtin/fetch.c b/builtin/fetch.c
index e064687dbd..fc7b6bb84e 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -712,7 +712,7 @@ static void adjust_refcol_width(const struct ref *ref)
 	int max, rlen, llen, len;
 
 	/* uptodate lines are only shown on high verbosity level */
-	if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
+	if (verbosity <= 0 && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
 		return;
 
 	max    = term_columns();
@@ -748,6 +748,9 @@ static void prepare_format_display(struct ref *ref_map)
 	struct ref *rm;
 	const char *format = "full";
 
+	if (verbosity < 0)
+		return;
+
 	git_config_get_string_tmp("fetch.output", &format);
 	if (!strcasecmp(format, "full"))
 		compact_format = 0;
@@ -827,7 +830,12 @@ static void format_display(struct strbuf *display, char code,
 			   const char *remote, const char *local,
 			   int summary_width)
 {
-	int width = (summary_width + strlen(summary) - gettext_width(summary));
+	int width;
+
+	if (verbosity < 0)
+		return;
+
+	width = (summary_width + strlen(summary) - gettext_width(summary));
 
 	strbuf_addf(display, "%c %-*s ", code, width, summary);
 	if (!compact_format)
@@ -1202,13 +1210,12 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
 					       "FETCH_HEAD", summary_width);
 			}
 			if (note.len) {
-				if (verbosity >= 0 && !shown_url) {
+				if (!shown_url) {
 					fprintf(stderr, _("From %.*s\n"),
 							url_len, url);
 					shown_url = 1;
 				}
-				if (verbosity >= 0)
-					fprintf(stderr, " %s\n", note.buf);
+				fprintf(stderr, " %s\n", note.buf);
 			}
 		}
 	}
-- 
2.33.0


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2021-08-30 10:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-25 15:45 [PATCH] fetch: skip formatting updated refs with `--quiet` Patrick Steinhardt
2021-08-25 16:57 ` Ævar Arnfjörð Bjarmason
2021-08-25 17:51 ` Junio C Hamano
2021-08-25 18:03   ` Patrick Steinhardt
2021-08-25 18:37     ` Junio C Hamano
2021-08-30 10:54 ` Patrick Steinhardt [this message]
2021-08-30 17:17   ` [PATCH v2] " Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e5ffa17753d4aca57d486d500a2d114290361ea7.1630320848.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).