Re: [PATCH v2] http-backend: drop strbuf pre-sizing from get_info_packs()
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Nov 16, 2026, 06:29 UTC
- Message-ID
- <avqi2Jpd-QT5iNU9@pks.im>
- In-Reply-To
- <20261115-http-backend-drop-strbuf-presizing-v2-1-b7ffe1be4482@gmail.com>
On Sun, Nov 15, 2026 at 09:58:47AM +0000, Yusuke Hayashi via B4 Relay wrote:
Show 20 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): `53` assumes the length of a SHA-1 > "P pack-<hex>.pack\n" line (under-counting the 77-byte lines in SHA-256 > repositories), and `cnt * 53 + 2` uses unchecked arithmetic. > > Rather than fixing the size calculation with `the_hash_algo->hexsz` and > `st_mult()` / `st_add()`, drop the pre-sizing altogether. > `get_info_packs()` is a one-shot CGI handler for dumb HTTP > `/objects/info/packs` requests that formats a single response and exits. > A typical repository only has a few dozen packs (a few kilobytes of > output), and even with thousands of packs `strbuf`'s geometric growth > only performs a couple dozen reallocations. That cost is negligible next > to CGI startup, scanning `objects/pack` to populate the pack list, and > writing the HTTP response, whereas computing `cnt` up front requires an > extra pass over the pack list.
Yeah, that's a much more convincing message. The other angle that this is missing a bit is that giving a size hint ensures that we allocate exactly as many bytes as we need. But that shouldn't matter in the general case, and if you have to allocate gigabytes of memory to store the list of packfiles... well, then you really have bigger problems to worry about.
So I think this version is good enough, thanks!
Patrick