summaryrefslogtreecommitdiff
path: root/builtin
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2020-02-04 21:24:24 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-02-05 18:24:15 (GMT)
commit1c56fc208400011b196f571041a0bc694fc9637c (patch)
tree22ec3e5b7dfdbc75309d9deca1bb9d46fc70de5d /builtin
parentddc42ec786ab2b64ef70bbd1e456db423dae031e (diff)
downloadgit-1c56fc208400011b196f571041a0bc694fc9637c.zip
git-1c56fc208400011b196f571041a0bc694fc9637c.tar.gz
git-1c56fc208400011b196f571041a0bc694fc9637c.tar.bz2
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>
Diffstat (limited to 'builtin')
-rw-r--r--builtin/name-rev.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index 6701fb1..793356e 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -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,