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