Re: [PATCH] http-backend: drop strbuf pre-sizing from get_info_packs()
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Nov 13, 2026, 07:03 UTC
- Message-ID
- <ava2WJkDag6-uthA@pks.im>
- In-Reply-To
- <20261113-http-backend-drop-strbuf-presizing-v1-1-7d66aae03b22@gmail.com>
On Fri, Nov 13, 2026 at 02:17:58AM +0000, Yusuke Hayashi via B4 Relay wrote:
Show 14 quoted lines
> From: Yusuke Hayashi <yusuke8h@gmail.com> > > get_info_packs() iterates over all packs in the repository to count > local packs, calls `strbuf_grow(&buf, cnt * 53 + 2)`, and then iterates > over the packs a second time to format each entry with `strbuf_addf()`. > > This pre-sizing dates back to 2f4038ab33 (Git-aware CGI to provide dumb > HTTP transport, 2009-10-30) and is no longer worth keeping: `53` assumes > the length of a SHA-1 "P pack-<hex>.pack\n" line (under-counting the > 77-byte lines in SHA-256 repositories), `cnt * 53 + 2` uses unchecked > arithmetic, and `strbuf_addf()` / `strbuf_addch()` already grow the > buffer automatically without needing an extra pass over the pack list. > Similar pre-sizing was recently removed in a9ce8526dc (pretty: drop > strbuf pre-sizing from add_rfc2047(), 2026-05-12).
So this explains that the behaviour is buggy, but that may lead the reader to wonder why we don't just fix the bug. And sure, we know to grow the buffer automatically, but the hint is intended as a performance improvement so that we know to allocate the buffer properly right from the start.
Note that I'm not saying that this change doesn't make sense. But I think the argument that "others do it" is rather weak. The more important question to answer is why it's fine to skip it in this particular function.
Thanks!
Patrick