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

Re: [PATCH] pretty: drop strbuf pre-sizing from add_rfc2047()

From
Jeff King <peff@peff.net>
Date
May 13, 2026, 18:54 UTC
Message-ID
<20260513185408.GA147423@coredump.intra.peff.net>
In-Reply-To
<xmqqtsscjf30.fsf@gitster.g>
On Wed, May 13, 2026 at 10:03:31AM +0900, Junio C Hamano wrote:
Show 8 quoted lines
> Someday we may want to go through the output from
> 
>     $ git grep -e 'strbuf_grow(' \*.c
> 
> and remove this ineffective presizing.  I think any call to
> strbuf_grow() that is immediately followed by a call to
> strbuf_addX() is suspect, like in the following illustration (there
> are others in these files).

Yup. I think this could be #leftoverbits material, but anybody who wants to pick this up should be careful to read through the whole function and make sure there's no subtle dependency on the grown buffer.

Skimming through, it looks like most are just leftovers from when old code was converted to strbuf, and the pre-growth was kept mostly out of conservatism.

Some of them are truly ugly to look at, like:
  http-backend.c: strbuf_grow(&buf, cnt * 53 + 2);
and I think in some cases we can even drop some surrounding code, like:
diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index 070a5af3e4..aa32ebc8ab 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -1337,21 +1337,14 @@ static int tecmp1 (const void *_a, const void *_b)
 
 static void mktree(struct tree_content *t, int v, struct strbuf *b)
 {
-	size_t maxlen = 0;
 	unsigned int i;
 
 	if (!v)
 		QSORT(t->entries, t->entry_count, tecmp0);
 	else
 		QSORT(t->entries, t->entry_count, tecmp1);
 
-	for (i = 0; i < t->entry_count; i++) {
-		if (t->entries[i]->versions[v].mode)
-			maxlen += t->entries[i]->name->str_len + 34;
-	}
-
 	strbuf_reset(b);
-	strbuf_grow(b, maxlen);
 	for (i = 0; i < t->entry_count; i++) {
 		struct tree_entry *e = t->entries[i];
 		if (!e->versions[v].mode)

So probably some satisfying cleanup opportunities available for somebody
who wants to spend a little time with it. ;)

-Peff
Previous: Junio C Hamano
Message 3 of 3 in “pretty: drop strbuf pre-sizing from add_rfc2047()”
  1. pretty: drop strbuf pre-sizing from add_rfc2047()Jeff King, May 12, 2026
  2. Junio C HamanoMay 13, 2026
  3. Jeff KingMay 13, 2026

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.