{"thread":{"id":"57331","subject":"[PATCH 2/2] fetch: skip computing output width when not printing anything","startedAt":"2022-01-28T10:17:38Z","lastAt":"2022-01-28T10:17:38Z","messageCount":1,"participants":["Patrick Steinhardt"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"447184","messageId":"5a3fd3232fd9e19e6f0054717a1f54c71bd8f272.1643364639.git.ps@pks.im","threadId":"57331","inReplyTo":"cover.1643364639.git.ps@pks.im","subject":"[PATCH 2/2] fetch: skip computing output width when not printing anything","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2022-01-28T10:17:10Z","receivedAt":"2022-01-28T10:17:38Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"When updating references via git-fetch(1), then by default we report to\nthe user which references have been changed. This output is formatted in\na nice table such that the different columns are aligned. Because the\nfirst column contains abbreviated object IDs we thus need to iterate\nover all refs which have changed and compute the minimum length for\ntheir respective abbreviated hashes. While this effort makes sense in\nmost cases, it is wasteful when the user passes the `--quiet` flag: we\ndon't print the summary, but still compute the length.\n\nSkip computing the summary width when the user asked for us to be quiet.\nThis gives us a small speedup of nearly 10% when doing a dry-run\nmirror-fetch in a repository with thousands of references being updated:\n\n    Benchmark 1: git fetch --prune --dry-run +refs/*:refs/* (HEAD~)\n      Time (mean ± σ):     34.048 s ±  0.233 s    [User: 30.739 s, System: 4.640 s]\n      Range (min … max):   33.785 s … 34.296 s    5 runs\n\n    Benchmark 2: git fetch --prune --dry-run +refs/*:refs/* (HEAD)\n      Time (mean ± σ):     30.768 s ±  0.287 s    [User: 27.534 s, System: 4.565 s]\n      Range (min … max):   30.432 s … 31.181 s    5 runs\n\n    Summary\n      'git fetch --prune --dry-run +refs/*:refs/* (HEAD)' ran\n        1.11 ± 0.01 times faster than 'git fetch --prune --dry-run +refs/*:refs/* (HEAD~)'\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/fetch.c | 8 ++++++--\n 1 file changed, 6 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 5f06b21f8e..ebbde5d56d 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -1093,12 +1093,15 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,\n \tstruct ref *rm;\n \tchar *url;\n \tint want_status;\n-\tint summary_width = transport_summary_width(ref_map);\n+\tint summary_width = 0;\n \n \trc = open_fetch_head(&fetch_head);\n \tif (rc)\n \t\treturn -1;\n \n+\tif (verbosity >= 0)\n+\t\tsummary_width = transport_summary_width(ref_map);\n+\n \tif (raw_url)\n \t\turl = transport_anonymize_url(raw_url);\n \telse\n@@ -1344,7 +1347,6 @@ static int prune_refs(struct refspec *rs, struct ref *ref_map,\n \tint url_len, i, result = 0;\n \tstruct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);\n \tchar *url;\n-\tint summary_width = transport_summary_width(stale_refs);\n \tconst char *dangling_msg = dry_run\n \t\t? _(\"   (%s will become dangling)\")\n \t\t: _(\"   (%s has become dangling)\");\n@@ -1373,6 +1375,8 @@ static int prune_refs(struct refspec *rs, struct ref *ref_map,\n \t}\n \n \tif (verbosity >= 0) {\n+\t\tint summary_width = transport_summary_width(stale_refs);\n+\n \t\tfor (ref = stale_refs; ref; ref = ref->next) {\n \t\t\tstruct strbuf sb = STRBUF_INIT;\n \t\t\tif (!shown_url) {\n-- \n2.35.0\n\n"}]}