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(' \*.cand 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);