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

[PATCH 3/4] ls-files: use strbuf_add_uint()

From
René Scharfe <l.s.r@web.de>
Date
May 12, 2026, 11:56 UTC
Message-ID
<20260512115603.80780-4-l.s.r@web.de>
In-Reply-To
<20260512115603.80780-1-l.s.r@web.de>

Speed up printing of objectsize values by using the specialized function strbuf_add_uint() as well as strbuf_insert() for padding instead of the general-purpose function strbuf_addf(). Here are the numbers I get when listing files in the Linux kernel repo:

Benchmark 1: ./git_main -C ../linux ls-files --format='%(objectsize)'
  Time (mean ± σ):     257.3 ms ±   0.4 ms    [User: 197.4 ms, System: 56.7 ms]
  Range (min … max):   256.7 ms … 258.1 ms    11 runs
Benchmark 2: ./git -C ../linux ls-files --format='%(objectsize)'
  Time (mean ± σ):     253.4 ms ±   0.3 ms    [User: 193.6 ms, System: 56.6 ms]
  Range (min … max):   253.0 ms … 253.8 ms    11 runs
Benchmark 3: ./git_main -C ../linux ls-files --format='%(objectsize:padded)'
  Time (mean ± σ):     257.9 ms ±   0.3 ms    [User: 198.0 ms, System: 56.6 ms]
  Range (min … max):   257.3 ms … 258.5 ms    11 runs
Benchmark 4: ./git -C ../linux ls-files --format='%(objectsize:padded)'
  Time (mean ± σ):     254.6 ms ±   1.0 ms    [User: 194.6 ms, System: 56.7 ms]
  Range (min … max):   253.7 ms … 256.8 ms    11 runs
Summary
  ./git -C ../linux ls-files --format='%(objectsize)' ran
    1.00 ± 0.00 times faster than ./git -C ../linux ls-files --format='%(objectsize:padded)'
    1.02 ± 0.00 times faster than ./git_main -C ../linux ls-files --format='%(objectsize)'
    1.02 ± 0.00 times faster than ./git_main -C ../linux ls-files --format='%(objectsize:padded)'
Signed-off-by: René Scharfe <l.s.r@web.de>
---
 builtin/ls-files.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index b148607f7a..c142ad4156 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -250,20 +250,23 @@ static void expand_objectsize(struct repository *repo, struct strbuf *line,
 			      const struct object_id *oid,
 			      const enum object_type type, unsigned int padded)
 {
+	static const char padding[] = "       ";
+	size_t min_len = padded ? strlen(padding) : 0;
+	size_t orig_len = line->len;
+	size_t len;
+
 	if (type == OBJ_BLOB) {
 		unsigned long size;
 		if (odb_read_object_info(repo->objects, oid, &size) < 0)
 			die(_("could not get object info about '%s'"),
 			    oid_to_hex(oid));
-		if (padded)
-			strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size);
-		else
-			strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size);
-	} else if (padded) {
-		strbuf_addf(line, "%7s", "-");
+		strbuf_add_uint(line, size);
 	} else {
 		strbuf_addstr(line, "-");
 	}
+	len = line->len - orig_len;
+	if (len < min_len)
+		strbuf_insert(line, orig_len, padding, min_len - len);
 }
 
 static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
-- 
2.54.0
Previous: René ScharfeNext: Jeff King
Message 15 of 18 in “strbuf: add and use strbuf_add_uint()”
  1. 0/4 strbuf: add and use strbuf_add_uint()René Scharfe, May 12, 2026
  2. 2/4 cat-file: use strbuf_add_uint()René Scharfe, May 12, 2026
  3. Jeff KingMay 12, 2026
  4. 1/4 strbuf: add strbuf_add_uint()René Scharfe, May 12, 2026
  5. Jeff KingMay 12, 2026
  6. René ScharfeMay 12, 2026
  7. Jeff KingMay 13, 2026
  8. Jeff KingMay 13, 2026
  9. Jeff KingMay 13, 2026
  10. René ScharfeMay 14, 2026
  11. Junio C HamanoMay 14, 2026
  12. Jeff KingMay 15, 2026
  13. René ScharfeMay 13, 2026
  14. 4/4 ls-tree: use strbuf_add_uint()René Scharfe, May 12, 2026
  15. 3/4 ls-files: use strbuf_add_uint()René Scharfe, May 12, 2026
  16. Jeff KingMay 12, 2026
  17. René ScharfeMay 12, 2026
  18. Jeff KingMay 13, 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.