git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 08/10] name-rev: pre-size buffer in get_parent_name()

From
René Scharfe <l.s.r@web.de>
Date
Feb 5, 2020, 15:16 UTC
Message-ID
<ea3910e1-f1cb-8a3c-0e32-5c33eeeb8af6@web.de>
In-Reply-To
<9b98835d-d94c-19f5-42ba-d583a33306cc@gmail.com>
Am 05.02.20 um 04:19 schrieb Derrick Stolee:
Show 27 quoted lines
> On 2/4/2020 4:24 PM, René Scharfe wrote:
>> 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
>
> Nice!
>
>> +	if (name->generation > 0) {
>> +		strbuf_grow(&sb, len +
>> +			    1 + decimal_width(name->generation) +
>> +			    1 + decimal_width(parent_number));
>
> Just curious: these strbuf_grow() calls are what _really_ improve the
> performance, right? If you dropped them, then can we expect the performance
> to be similar to the old code?

The replaced xstrfmt() is defined in strbuf.c and trivially wraps xstrvfmt(), which in turn does:

	struct strbuf buf = STRBUF_INIT;
	strbuf_vaddf(&buf, fmt, ap);
	return strbuf_detach(&buf, NULL);

And strbuf_addf() trivially wraps strbuf_vaddf(). So indeed, all I did was to inline xstrfmt() and add the strbuf_grow() calls.

Their high impact shocked me a bit. vsnprintf(3) really doesn't seem to like working with a buffer that is too small (when it's supposed to just calculate how many bytes it would have needed), at least not in the one from glibc 2.29.

Show 16 quoted lines
>> +		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,
>> --
>> 2.25.0
>>
>
Previous: Derrick StoleeNext: René Scharfe
Message 16 of 27 in “name-rev: improve memory usage”
  1. 00/10 name-rev: improve memory usageRené Scharfe, Feb 4, 2020
  2. 01/10 name-rev: rewrite create_or_update_name()René Scharfe, Feb 4, 2020
  3. Derrick StoleeFeb 5, 2020
  4. Taylor BlauFeb 5, 2020
  5. Andrei RybakFeb 5, 2020
  6. René ScharfeFeb 5, 2020
  7. 02/10 name-rev: remove unused typedefRené Scharfe, Feb 4, 2020
  8. 03/10 name-rev: respect const qualifierRené Scharfe, Feb 4, 2020
  9. 04/10 name-rev: don't leak path copy in name_ref()René Scharfe, Feb 4, 2020
  10. Derrick StoleeFeb 5, 2020
  11. 05/10 name-rev: don't _peek() in create_or_update_name()René Scharfe, Feb 4, 2020
  12. 06/10 name-rev: put struct rev_name into commit slabRené Scharfe, Feb 4, 2020
  13. 07/10 name-rev: factor out get_parent_name()René Scharfe, Feb 4, 2020
  14. 08/10 name-rev: pre-size buffer in get_parent_name()René Scharfe, Feb 4, 2020
  15. Derrick StoleeFeb 5, 2020
  16. René ScharfeFeb 5, 2020
  17. 09/10 name-rev: generate name strings only if they are betterRené Scharfe, Feb 4, 2020
  18. Derrick StoleeFeb 5, 2020
  19. René ScharfeFeb 5, 2020
  20. 10/10 name-rev: release unused name stringsRené Scharfe, Feb 4, 2020
  21. Derrick StoleeFeb 5, 2020
  22. Derrick StoleeFeb 5, 2020
  23. Derrick StoleeFeb 5, 2020
  24. 01/10 name-rev: rewrite create_or_update_name()René Scharfe, Feb 5, 2020
  25. 11/10 name-rev: sort tip names before applyingRené Scharfe, Feb 5, 2020
  26. Junio C HamanoFeb 5, 2020
  27. René ScharfeFeb 5, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.