cat-file: use strbuf_add_uint()

Speed up printing of objectsize atoms by using the specialized function strbuf_add_uint() instead of the general-purpose function strbuf_addf(): Benchmark 1: ./git_main cat-file --batch-all-objects --batch-check='%(objectsize)' Time (mean ± σ): 751.7 ms ± 1.5 ms [User: 733.5 ms, System: 17.1 ms] Range (min … max): 750.5 ms … 755.0 ms 10 runs Benchmark 2: ./git cat-file --batch-all-objects --batch-check='%(objectsize)' Time (mean ± σ): 720.4 ms ± 0.4 ms [User: 701.9 ms, System: 16.7 ms] Range (min … max): 719.7 ms … 721.2 ms 10 runs Summary ./git cat-file --batch-all-objects --batch-check='%(objectsize)' ran 1.04 ± 0.00 times faster than ./git_main cat-file --batch-all-objects --batch-check='%(objectsize)' Benchmark 1: ./git_main cat-file --batch-all-objects --batch-check='%(objectsize:disk)' Time (mean ± σ): 404.6 ms ± 0.9 ms [User: 397.8 ms, System: 5.7 ms] Range (min … max): 403.3 ms … 405.9 ms 10 runs Benchmark 2: ./git cat-file --batch-all-objects --batch-check='%(objectsize:disk)' Time (mean ± σ): 378.3 ms ± 0.9 ms [User: 371.2 ms, System: 5.9 ms] Range (min … max): 376.8 ms … 380.2 ms 10 runs Summary ./git cat-file --batch-all-objects --batch-check='%(objectsize:disk)' ran 1.07 ± 0.00 times faster than ./git_main cat-file --batch-all-objects --batch-check='%(objectsize:disk)' 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 8feb5702163a32384d098e2c9ad3987928f8c447
1 file changed +2 -2
builtin/cat-file.c
+2 -2
@@ -330,12 +330,12 @@ static int expand_atom(struct strbuf *sb, const char *atom, int len,
330 if (data->mark_query)
331 data->info.sizep = &data->size;
332 else
333 - strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
333 + strbuf_add_uint(sb, data->size);
334 } else if (is_atom("objectsize:disk", atom, len)) {
335 if (data->mark_query)
336 data->info.disk_sizep = &data->disk_size;
337 else
338 - strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
338 + strbuf_add_uint(sb, data->disk_size);
339 } else if (is_atom("rest", atom, len)) {
340 if (data->mark_query)
341 data->split_on_whitespace = 1;