@matheusbsbs / matheus-bagliano / commits / 1c56fc2084
Fork 0

name-rev: pre-size buffer in get_parent_name()

We can calculate the size of new name easily and precisely. Open-code the xstrfmt() calls and grow the buffers as needed before filling them. This provides a surprisingly large benefit when working with the Chromium repository; here are the numbers measured using hyperfine before: Benchmark #1: ./git -C ../chromium/src name-rev --all Time (mean ± σ): 5.822 s ± 0.013 s [User: 5.304 s, System: 0.516 s] Range (min … max): 5.803 s … 5.837 s 10 runs ... and with this patch: Benchmark #1: ./git -C ../chromium/src name-rev --all Time (mean ± σ): 1.527 s ± 0.003 s [User: 1.015 s, System: 0.511 s] Range (min … max): 1.524 s … 1.535 s 10 runs Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Feb 4, 2020 at 22:24 UTC 1c56fc208400011b196f571041a0bc694fc9637c
1 file changed +14 -6
builtin/name-rev.c
+14 -6
@@ -103,15 +103,23 @@ static struct rev_name *create_or_update_name(struct commit *commit,
103
104 static char *get_parent_name(const struct rev_name *name, int parent_number)
105 {
106 + struct strbuf sb = STRBUF_INIT;
107 size_t len;
108
109 strip_suffix(name->tip_name, "^0", &len);
109 - if (name->generation > 0)
110 - return xstrfmt("%.*s~%d^%d", (int)len, name->tip_name,
111 - name->generation, parent_number);
112 - else
113 - return xstrfmt("%.*s^%d", (int)len, name->tip_name,
114 - parent_number);
110 + if (name->generation > 0) {
111 + strbuf_grow(&sb, len +
112 + 1 + decimal_width(name->generation) +
113 + 1 + decimal_width(parent_number));
114 + strbuf_addf(&sb, "%.*s~%d^%d", (int)len, name->tip_name,
115 + name->generation, parent_number);
116 + } else {
117 + strbuf_grow(&sb, len +
118 + 1 + decimal_width(parent_number));
119 + strbuf_addf(&sb, "%.*s^%d", (int)len, name->tip_name,
120 + parent_number);
121 + }
122 + return strbuf_detach(&sb, NULL);
123 }
124
125 static void name_rev(struct commit *start_commit,