Skip to content

Commit 1c56fc2

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ddc42ec commit 1c56fc2

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

builtin/name-rev.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,23 @@ static struct rev_name *create_or_update_name(struct commit *commit,
103103

104104
static char *get_parent_name(const struct rev_name *name, int parent_number)
105105
{
106+
struct strbuf sb = STRBUF_INIT;
106107
size_t len;
107108

108109
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);
115123
}
116124

117125
static void name_rev(struct commit *start_commit,

0 commit comments

Comments
 (0)