Skip to content

Commit

Permalink
name-rev: pre-size buffer in get_parent_name()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rscharfe authored and gitster committed Feb 5, 2020
1 parent ddc42ec commit 1c56fc2
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions builtin/name-rev.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,23 @@ static struct rev_name *create_or_update_name(struct commit *commit,

static char *get_parent_name(const struct rev_name *name, int parent_number)
{
struct strbuf sb = STRBUF_INIT;
size_t len;

strip_suffix(name->tip_name, "^0", &len);
if (name->generation > 0)
return xstrfmt("%.*s~%d^%d", (int)len, name->tip_name,
name->generation, parent_number);
else
return xstrfmt("%.*s^%d", (int)len, name->tip_name,
parent_number);
if (name->generation > 0) {
strbuf_grow(&sb, len +
1 + decimal_width(name->generation) +
1 + decimal_width(parent_number));
strbuf_addf(&sb, "%.*s~%d^%d", (int)len, name->tip_name,
name->generation, parent_number);
} else {
strbuf_grow(&sb, len +
1 + decimal_width(parent_number));
strbuf_addf(&sb, "%.*s^%d", (int)len, name->tip_name,
parent_number);
}
return strbuf_detach(&sb, NULL);
}

static void name_rev(struct commit *start_commit,
Expand Down

0 comments on commit 1c56fc2

Please sign in to comment.