1From f6bb64df82ddd050894ca8a2a0bfbd1997602500 Mon Sep 17 00:00:00 2001
2Message-Id: <f6bb64df82ddd050894ca8a2a0bfbd1997602500.1631166264.git.ps@pks.im>
3From: Patrick Steinhardt <ps@pks.im>
4Date: Mon, 30 Aug 2021 12:54:26 +0200
5Subject: [PATCH] fetch: skip formatting updated refs with `--quiet`
6MIME-Version: 1.0
7Content-Type: text/plain; charset=UTF-8
8Content-Transfer-Encoding: 8bit
9
10When fetching, Git will by default print a list of all updated refs in a
11nicely formatted table. In order to come up with this table, Git needs
12to iterate refs twice: first to determine the maximum column width, and
13a second time to actually format these changed refs.
14
15While this table will not be printed in case the user passes `--quiet`,
16we still go out of our way and do all these steps. In fact, we even do
17more work compared to not passing `--quiet`: without the flag, we will
18skip all references in the column width computation which have not been
19updated, but if it is set we will now compute widths for all refs.
20
21Fix this issue by completely skipping both preparation of the format and
22formatting data for display in case the user passes `--quiet`, improving
23performance especially with many refs. The following benchmark shows a
24nice speedup for a quiet mirror-fetch in a repository with 2.3M refs:
25
26    Benchmark #1: HEAD~: git-fetch
27      Time (mean ± σ):     26.929 s ±  0.145 s    [User: 24.194 s, System: 4.656 s]
28      Range (min … max):   26.692 s … 27.068 s    5 runs
29
30    Benchmark #2: HEAD: git-fetch
31      Time (mean ± σ):     25.189 s ±  0.094 s    [User: 22.556 s, System: 4.606 s]
32      Range (min … max):   25.070 s … 25.314 s    5 runs
33
34    Summary
35      'HEAD: git-fetch' ran
36        1.07 ± 0.01 times faster than 'HEAD~: git-fetch'
37
38While at it, this patch also fixes `adjust_refcol_width()` such that it
39skips unchanged refs in case the user passed `--quiet`, where verbosity
40will be negative. While this function won't be called anymore if so,
41this brings the comment in line with actual code. Furthermore, needless
42`verbosity >= 0` checks are now removed in `store_updated_refs()`: we
43never print to the `note` buffer anymore in case `verbosity < 0`, so we
44won't end up in that code block anyway.
45
46Signed-off-by: Patrick Steinhardt <ps@pks.im>
47Signed-off-by: Junio C Hamano <gitster@pobox.com>
48---
49 builtin/fetch.c | 17 ++++++++++++-----
50 1 file changed, 12 insertions(+), 5 deletions(-)
51
52diff --git a/builtin/fetch.c b/builtin/fetch.c
53index 25740c13df..334bc7efa6 100644
54--- a/builtin/fetch.c
55+++ b/builtin/fetch.c
56@@ -712,7 +712,7 @@ static void adjust_refcol_width(const struct ref *ref)
57 	int max, rlen, llen, len;
58
59 	/* uptodate lines are only shown on high verbosity level */
60-	if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
61+	if (verbosity <= 0 && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
62 		return;
63
64 	max    = term_columns();
65@@ -748,6 +748,9 @@ static void prepare_format_display(struct ref *ref_map)
66 	struct ref *rm;
67 	const char *format = "full";
68
69+	if (verbosity < 0)
70+		return;
71+
72 	git_config_get_string_tmp("fetch.output", &format);
73 	if (!strcasecmp(format, "full"))
74 		compact_format = 0;
75@@ -827,7 +830,12 @@ static void format_display(struct strbuf *display, char code,
76 			   const char *remote, const char *local,
77 			   int summary_width)
78 {
79-	int width = (summary_width + strlen(summary) - gettext_width(summary));
80+	int width;
81+
82+	if (verbosity < 0)
83+		return;
84+
85+	width = (summary_width + strlen(summary) - gettext_width(summary));
86
87 	strbuf_addf(display, "%c %-*s ", code, width, summary);
88 	if (!compact_format)
89@@ -1202,13 +1210,12 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
90 					       "FETCH_HEAD", summary_width);
91 			}
92 			if (note.len) {
93-				if (verbosity >= 0 && !shown_url) {
94+				if (!shown_url) {
95 					fprintf(stderr, _("From %.*s\n"),
96 							url_len, url);
97 					shown_url = 1;
98 				}
99-				if (verbosity >= 0)
100-					fprintf(stderr, " %s\n", note.buf);
101+				fprintf(stderr, " %s\n", note.buf);
102 			}
103 		}
104 	}
105--
1062.33.0
107
108