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

[PATCH v4] packed-refs: use `fwrite()` when passing refs verbatim

From
Karthik Nayak <karthik.188@gmail.com>
Date
Oct 7, 2026, 11:20 UTC
Message-ID
<20261007-kn-speedup-packed-refs-v4-1-79a411026596@gmail.com>
In-Reply-To
<20260930-kn-speedup-packed-refs-v1-1-111cd03d9b0e@gmail.com>

The `write_with_updates()` function uses a `struct ref_iterator` to iterate over all refs to write to the temporary packed-refs file. It receives the iterator from `packed_ref_iterator_begin()` which takes a snapshot of the 'packed-refs' file.

While writing to the new packed-refs file, writes are routed via `write_packed_entry()` which uses `fprintf()`. Even for references which haven't changed, we use the same mechanism. This is slow since we need to do string parsing and formatting. Instead, let's track the position of unchanged references in the snapshot iterator and directly write verbatim via `fwrite()`.

With this, any sanitization which was happening as a side effect of reformatting is now lost. But that was never the job of this section of the code, since the main intention is to simply rewrite the remaining refs post deletion of the selective few.

This removes the unnecessary formatting operation involved. We can see a consistent ~20% performance improvement when deleting from packed references.

Benchmark 1: update-ref: delete ref (refcount = 100000, revision = master)
  Time (mean ± σ):      28.7 ms ±   1.7 ms    [User: 22.5 ms, System: 5.9 ms]
  Range (min … max):    26.7 ms …  33.3 ms    46 runs
Benchmark 2: update-ref: delete ref (refcount = 100000, revision = b4/kn-speedup-packed-refs)
  Time (mean ± σ):      23.8 ms ±   1.2 ms    [User: 17.5 ms, System: 6.0 ms]
  Range (min … max):    22.1 ms …  27.7 ms    56 runs
Summary
  update-ref: delete ref (refcount = 100000, revision = b4/kn-speedup-packed-refs) ran
    1.21 ± 0.09 times faster than update-ref: delete ref (refcount = 100000, revision = master)
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
Changes in v4:
- Modify the commit message to state the issue with the previous
  approach.
- Modify the comment for `record_start` to remove ambiguity around its
  setting.
- Remove `write_packed_entry_raw()` and inline the call to `fwrite()`.
- Link to v3: https://patch.msgid.link/20261006-kn-speedup-packed-refs-v3-1-a1c76b1df9e0@gmail.com
Changes in v3:
- Fixed a typo in the commit message.
- Link to v2: https://patch.msgid.link/20261002-kn-speedup-packed-refs-v2-1-2ae75772ebc1@gmail.com
Changes in v2:
- Instead of using the existing function, introduce a new
  `write_packed_entry_raw()`.
- Modify the commit to also note that we lose sanitization.
- Link to v1: https://patch.msgid.link/20260930-kn-speedup-packed-refs-v1-1-111cd03d9b0e@gmail.com
---
 refs/packed-backend.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/refs/packed-backend.c b/refs/packed-backend.c
index a73fc6aca7..53511f2d91 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
@@ -879,6 +879,9 @@ struct packed_ref_iterator {
 	/* The current position in the snapshot's buffer: */
 	const char *pos;
 
+	/* The starting position of the current ref record. */
+	const char *record_start;
+
 	/* The end of the part of the buffer that will be iterated over: */
 	const char *eof;
 
@@ -933,6 +936,7 @@ static int next_record(struct packed_ref_iterator *iter)
 	if (iter->pos == iter->eof)
 		return ITER_DONE;
 
+	iter->record_start = iter->pos;
 	iter->base.ref.flags = REF_ISPACKED;
 	p = iter->pos;
 
@@ -1530,9 +1534,15 @@ static enum ref_transaction_error write_with_updates(struct packed_ref_store *re
 		}
 
 		if (cmp < 0) {
-			/* Pass the old reference through. */
-			if (write_packed_entry(out, iter->ref.name,
-					       iter->ref.oid, iter->ref.peeled_oid))
+			const struct packed_ref_iterator *packed_iter =
+				(const struct packed_ref_iterator *)iter;
+			size_t len = packed_iter->pos - packed_iter->record_start;
+
+			/*
+			 * Skip any formatting and directly write to the packed-refs file
+			 * when deleting references and writing the remaining refs verbatim.
+			 */
+			if (fwrite(packed_iter->record_start, len, 1, out) != 1)
 				goto write_error;
 
 			if ((ok = ref_iterator_advance(iter)) != ITER_OK) {

---
base-commit: a018953688f1b10bddf91bff8747068f5f4746a4
change-id: 20260930-kn-speedup-packed-refs-9868f5d0abe9


Thanks
- Karthik
Previous: Karthik NayakNext: Patrick Steinhardt
Message 14 of 16 in “packed-refs: use `fwrite()` when passing refs verbatim”
  1. packed-refs: use `fwrite()` when passing refs verbatimKarthik Nayak, Sep 30, 2026
  2. Toon ClaesOct 1, 2026
  3. Karthik NayakOct 2, 2026
  4. packed-refs: use `fwrite()` when passing refs verbatimKarthik Nayak, Oct 2, 2026
  5. Toon ClaesOct 5, 2026
  6. Karthik NayakOct 6, 2026
  7. packed-refs: use `fwrite()` when passing refs verbatimKarthik Nayak, Oct 6, 2026
  8. Patrick SteinhardtOct 6, 2026
  9. Karthik NayakOct 6, 2026
  10. Patrick SteinhardtOct 7, 2026
  11. Karthik NayakOct 7, 2026
  12. Junio C HamanoOct 6, 2026
  13. Karthik NayakOct 6, 2026
  14. packed-refs: use `fwrite()` when passing refs verbatimKarthik Nayak, Oct 7, 2026
  15. Patrick SteinhardtOct 7, 2026
  16. Toon ClaesOct 8, 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.