fetch: skip formatting updated refs with `--quiet`

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 30, 2021 at 12:54 UTC f6bb64df82ddd050894ca8a2a0bfbd1997602500
1 file changed +12 -5
builtin/fetch.c
+12 -5
@@ -712,7 +712,7 @@ static void adjust_refcol_width(const struct ref *ref)
712 int max, rlen, llen, len;
713
714 /* uptodate lines are only shown on high verbosity level */
715 - if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
715 + if (verbosity <= 0 && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
716 return;
717
718 max = term_columns();
@@ -748,6 +748,9 @@ static void prepare_format_display(struct ref *ref_map)
748 struct ref *rm;
749 const char *format = "full";
750
751 + if (verbosity < 0)
752 + return;
753 +
754 git_config_get_string_tmp("fetch.output", &format);
755 if (!strcasecmp(format, "full"))
756 compact_format = 0;
@@ -827,7 +830,12 @@ static void format_display(struct strbuf *display, char code,
830 const char *remote, const char *local,
831 int summary_width)
832 {
830 - int width = (summary_width + strlen(summary) - gettext_width(summary));
833 + int width;
834 +
835 + if (verbosity < 0)
836 + return;
837 +
838 + width = (summary_width + strlen(summary) - gettext_width(summary));
839
840 strbuf_addf(display, "%c %-*s ", code, width, summary);
841 if (!compact_format)
@@ -1202,13 +1210,12 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
1210 "FETCH_HEAD", summary_width);
1211 }
1212 if (note.len) {
1205 - if (verbosity >= 0 && !shown_url) {
1213 + if (!shown_url) {
1214 fprintf(stderr, _("From %.*s\n"),
1215 url_len, url);
1216 shown_url = 1;
1217 }
1210 - if (verbosity >= 0)
1211 - fprintf(stderr, " %s\n", note.buf);
1218 + fprintf(stderr, " %s\n", note.buf);
1219 }
1220 }
1221 }