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

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

From
Jeff King <peff@peff.net>
Date
May 13, 2026, 16:46 UTC
Message-ID
<20260513164622.GC103037@coredump.intra.peff.net>
In-Reply-To
<2f45a33b-5945-431d-97a5-7d61e271cfba@web.de>
On Tue, May 12, 2026 at 10:44:21PM +0200, René Scharfe wrote:
Show 7 quoted lines
> > ...also less nice. We are formatting into the strbuf, and then maybe
> > memmove()-ing the result to accommodate padding. I wonder how much that
> > affects the timing. It's extra shuffling, but memmove() etc is often
> > surprisingly fast.
> 
> I gave my objectsize and objectsize:padded numbers; the difference was
> 1.2 ms, albeit with 1.0 ms noise in padded case.
Ah, right, that makes sense.
Applying the fast decimal-width from my earlier message, I came up with:
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index c142ad4156..e17e3517ff 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -251,22 +251,24 @@ static void expand_objectsize(struct repository *repo, struct strbuf *line,
 			      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;
+	static const unsigned padding_len = ARRAY_SIZE(padding) - 1;
 
 	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) {
+			unsigned digits = decimal_length_u64(size);
+			if (digits < padding_len)
+				strbuf_add(line, padding, padding_len - digits);
+		}
 		strbuf_add_uint(line, size);
 	} else {
+		if (padded)
+			strbuf_add(line, padding, padding_len - 1);
 		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,

but it was not meaningfully faster than your version.

> > I have often wondered how hard it would be to implement our own
> > vsnprintf(), and whether we could do better than the libc ones. It would
> > be nice to be able to add shorthands for common types (instead of the
> > unreadable PRIuMAX mess), as well as custom ones (e.g., hex oids).
> 
> C99 has %ju for uintmax_t and %zu for size_t.  Hmm, do we actually
> still need to avoid them?  CodingGuidelines says "the C library used
> by MinGW does not" support it.  82c36fa0a9 (submodule: hash the
> submodule name for the gitdir path, 2026-01-12) just added a %zu,
> and there are lots of them in compat/mimalloc/ in Git for Windows.

An accidental test-balloon, I guess. It's in v2.54.0, so maybe we will
see some reports, or maybe we can eventually use it as evidence that we
can relax a bit.

-Peff
Previous: René Scharfe
Message 18 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.