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

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

From
Junio C Hamano <gitster@pobox.com>
Date
May 13, 2026, 01:03 UTC
Message-ID
<xmqqtsscjf30.fsf@gitster.g>
In-Reply-To
<20260512162022.GA69669@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 17 quoted lines
> At the top of add_rfc2047() we do this:
>
>   strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
>
> where "len" is the size of the header (like an author name) we are about
> to encode into the buffer. This pre-sizing is purely an optimization; we
> use strbuf_addf() and friends to actually write into the buffer, and
> they will grow the buffer as necessary.
> ...
> Is the optimization helping? I don't think so. Even for a gigantic case
> like the 1.4GB author name, I couldn't measure any slowdown when
> removing it. And most input will be much smaller, and added to a running
> strbuf containing the rest of the email-header output. We can just rely
> on strbuf's usual amortized-linear growth.
>
> So deleting the line seems like the best way to go. It eliminates the
> integer overflow and makes the code a tiny bit simpler.
Very nice.
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).

diff --git c/apply.c w/apply.c
index 3de4aa4d2e..b0f146276e 100644
--- c/apply.c
+++ w/apply.c
@@ -3305,7 +3305,6 @@ static int apply_fragments(struct apply_state *state, struct image *img, struct
 static int read_blob_object(struct strbuf *buf, const struct object_id *oid, unsigned mode)
 {
 	if (S_ISGITLINK(mode)) {
-		strbuf_grow(buf, 100);
 		strbuf_addf(buf, "Subproject commit %s\n", oid_to_hex(oid));
 	} else {
 		enum object_type type;
diff --git c/archive.c w/archive.c
index fcd474c682..47b8725f0a 100644
--- c/archive.c
+++ w/archive.c
@@ -164,7 +164,6 @@ static int write_archive_entry(const struct object_id *oid, const char *base,
 
 	args->convert = 0;
 	strbuf_reset(&path);
-	strbuf_grow(&path, PATH_MAX);
 	strbuf_add(&path, args->base, args->baselen);
 	strbuf_add(&path, base, baselen);
 	strbuf_addstr(&path, filename);
Previous: Jeff KingNext: Jeff King
Message 2 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.