ls-files: use strbuf_add_uint()

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed May 12, 2026 at 13:56 UTC f001b4ab3942cbaff4a39662294ee7191e2dbee5
1 file changed +9 -6
builtin/ls-files.c
+9 -6
@@ -250,20 +250,23 @@ static void expand_objectsize(struct repository *repo, struct strbuf *line,
250 const struct object_id *oid,
251 const enum object_type type, unsigned int padded)
252 {
253 + static const char padding[] = " ";
254 + size_t min_len = padded ? strlen(padding) : 0;
255 + size_t orig_len = line->len;
256 + size_t len;
257 +
258 if (type == OBJ_BLOB) {
259 unsigned long size;
260 if (odb_read_object_info(repo->objects, oid, &size) < 0)
261 die(_("could not get object info about '%s'"),
262 oid_to_hex(oid));
258 - if (padded)
259 - strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size);
260 - else
261 - strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size);
262 - } else if (padded) {
263 - strbuf_addf(line, "%7s", "-");
263 + strbuf_add_uint(line, size);
264 } else {
265 strbuf_addstr(line, "-");
266 }
267 + len = line->len - orig_len;
268 + if (len < min_len)
269 + strbuf_insert(line, orig_len, padding, min_len - len);
270 }
271
272 static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,