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

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
Previous: Yusuke Hayashi via B4 RelayNext: Junio C Hamano
Message 5 of 6 in “http-backend: drop strbuf pre-sizing from get_info_packs()”
  1. http-backend: drop strbuf pre-sizing from get_info_packs()Yusuke Hayashi via B4 Relay, Nov 13, 2026
  2. Patrick SteinhardtNov 13, 2026
  3. Yusuke HayashiNov 15, 2026
  4. http-backend: drop strbuf pre-sizing from get_info_packs()Yusuke Hayashi via B4 Relay, Nov 15, 2026
  5. Patrick SteinhardtNov 16, 2026
  6. Junio C HamanoNov 16, 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.