From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout.web.de (mout.web.de [212.227.17.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D6723C0A0B for ; Tue, 12 May 2026 20:44:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.17.11 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778618670; cv=none; b=UIyey4jdsQb6W4vwzf1MYyldi72+Q6oAOY+IMnutGJrcm2x98ieg3JN2BYuduoG05JNH0szDV2tSKc/PIpQXaA7mHFT8Qff0PRC/mz5Sdn/h8qGr0MqQJpnhhPfPD/nlfcelPukCKwF241me2tdMVSGtxDYy4M1hVo5jSacwdfg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778618670; c=relaxed/simple; bh=LyF1gEFdllD4CG5Nhl6iVGzFehAL8j5C+djh0pteUug=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=LKw+whSwufgQjHhmE0odq0VSZingyUTh8gHVh7xp6v+AXZJ0nPd9N4zKrFWGWHSpTbDPGO7L5QbA5FYfheR9lj9pIjnVyRAqhaUyylKJqGVysuMfYHpvnx6bH8p5MkoqGd16cq5N03ldLvbE5nSViWVOM9fXhK/d5FFCIX/mvlU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de; spf=pass smtp.mailfrom=web.de; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b=I+xFIMNa; arc=none smtp.client-ip=212.227.17.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b="I+xFIMNa" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1778618662; x=1779223462; i=l.s.r@web.de; bh=yUdZodF65P24nBQwoWusnhUyR37DS4x0A9woHtgsL3A=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:To:Cc: References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding:cc:content-transfer-encoding: content-type:date:from:message-id:mime-version:reply-to:subject: to; b=I+xFIMNa/hSQ8ahymbF6ZFlCBktk0hU8mVp3afyq5Ta2viAfyID2LxKIRgR+IAjO M2CxdSvJsvVyISQ2fqwrn40xvsMO79jYjyEixvRbmLTiPR99usKApTtYkqmpG5nwo GZVtsH4fnoWHI47EUlcOVlpccEzVuESku+C0Jz4kQYsR3Ox7XtQiH2aBEEWc0/YQF oysP3vFf6PcHHXl8FJ9+rZg9VLlJNQxEmUb2Js+5zoEjhHtumL3K0gvifNz3ps8s/ MpMibEJBobpCuXxVUCazcz9sdZojkoS9VkIYP1PQpEJnfuy+Dzcgel2YEDk8JmW8Q pf7norMY2UcNMYY/9g== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from client.hidden.invalid by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MsrhK-1xFq4E1wG8-013Qua; Tue, 12 May 2026 22:44:22 +0200 Message-ID: <2f45a33b-5945-431d-97a5-7d61e271cfba@web.de> Date: Tue, 12 May 2026 22:44:21 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 3/4] ls-files: use strbuf_add_uint() To: Jeff King Cc: git@vger.kernel.org References: <20260512115603.80780-1-l.s.r@web.de> <20260512115603.80780-4-l.s.r@web.de> <20260512190105.GE70851@coredump.intra.peff.net> Content-Language: en-US From: =?UTF-8?Q?Ren=C3=A9_Scharfe?= In-Reply-To: <20260512190105.GE70851@coredump.intra.peff.net> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:qP0+vd+dbzxtydm1I9186xqrLVlk9owV9ISw0OXy5DZuGsPLqH8 lL0W8B4+BHWhP39666U8u/UJKq6MIgUBC3wJUTVSpMans2fyFaKHsBZ5P9rz7W7/Z761OQS 5l7Rzqn58nj5IQX/uE06Qsi2V6h8T3NqGJG3oZ4dVQ83alWdcn6e+qadYT0HqQZ4uXowo7q Srp8LQhZFFFgOviPtKmtQ== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:9m8JJbFMNM4=;o7Lio3ycbVEw8iAUEHOJIeRzYO1 qmpq4Kbhv/vA80Hiy4HwqmsObdShrSaDIpeLvrGb/0pHVyTvXj5kF17TmgFxFKENm1uA5OrJB WqVYEvVu0C6LR0g4yvUgFpgxRAH2vnbJ+9MM2wKfx8sDSGfygCbWwt6sUrg5g1WBCHXrFp7dt w7Pzag8n8MJqXu5LOXXVYqZMjsQG4FR5t3OFYQ86Poc4Orv51Ohhsn6TCtOIxfbiC+SbWnAqQ nHhwbGHgXtOqXphyPvWQSce0g1kFfLK7GZuKpW9ylUeY9kyW69Qiv7rnG+ZmDmc3C1i3fWazA hcrk8aC3SSvuVBEsiDEt8k66pemSMLFGWyoXPvofRTj1X2oVA9+rP6Ys1qPMMMs9iiqIE6a7x JX3U5ebFAjmQhCLB6UJJKP1SvilWhWiUKeF+r7HmhearUYj3Q0YZ8tbjQh8UPBgnZ2m56QavJ Tryi8mCzTpgF4bOSna2YWVjazV+COAkPCZq7baER7gth+9n80XICckuEpVX/e1W/svrIH487w qL2NYF3KdFqDc5ruMU81EVRTFOh/rYB8sG8Zcxhz96CldXV2xCjVjTFAqfa3z+C1uJNvuCWYy IN7Emyn93CLjFSVLb3xvjBg5fmP+aIHCNaE2uNl4ptU7o6E2Xhdkurhhyhim8tWkL7SejeoHS 5KV0BP5nEnpfK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HX/J2Tg8Npi39BZPKSjSUmNMdLScGXCID bFcX6MBTE/8yhRgdM+EvYtHJ7jo/6B9m0hqWQq8B9p8S49x+ouwl54f7qdYpWyrFqPOHgdkcK AoAdrs+2nYYPXEnJ6sEBIlc4BOqtzHnbgCpbhwQFAX8OcXbnpW+BeyaGYRKXzUpeMBZnGF2XI Zs4PEUYEtiQD0cdcn0tDBgz0yHo0k/FtgIRBLTd7xtsv0cJD4AaLLKp+PI0AHJZlOBoIZJJSR mwlnCDYATjAq9VZlGF8ijtwNdv0FeBG2iP3xNthIWfwNkTQss77ce0Fz5NI6jsoJRSLXTNNXH XBTWRHGOrpi+mp/18ygDpnbhQlxY0dr9Idyo5wGKKZxPAOZIVJWoVaY3qQLK9gVAg860nkZOd JVOEoxi5ZlJKYPPYFq+oskMm/Z9ehc9uKi311gvxCuQ+CwGaHeFDC7pWrH5UTJYkdZ3bpH0US BBfoOEjXJeWb/ioSgrKKwrrl/KqXsFU9LkwRmC43RQFf1sDTMNeJqfVBTxgE9x6kiCKTPMepT RotVw5TQmZ7QMJMYzbiWoC7hAfNIgKtLN7lN6F5fIo3bR5992umCgwvuO2reDLuu3AVssVzfR q1zjw6dv8Pa/dZoZAX2x+ziGbzFkjcbcB8pdF2eG79QwhiPXxA4CJIdH8 On 5/12/26 9:01 PM, Jeff King wrote: > On Tue, May 12, 2026 at 01:56:02PM +0200, Ren=C3=A9 Scharfe wrote: >=20 >> Speed up printing of objectsize values by using the specialized functio= n >> strbuf_add_uint() as well as strbuf_insert() for padding instead of the >> general-purpose function strbuf_addf(). Here are the numbers I get whe= n >> listing files in the Linux kernel repo: >> >> Benchmark 1: ./git_main -C ../linux ls-files --format=3D'%(objectsize)' >> Time (mean =C2=B1 =CF=83): 257.3 ms =C2=B1 0.4 ms [User: 197= .4 ms, System: 56.7 ms] >> Range (min =E2=80=A6 max): 256.7 ms =E2=80=A6 258.1 ms 11 runs >> >> Benchmark 2: ./git -C ../linux ls-files --format=3D'%(objectsize)' >> Time (mean =C2=B1 =CF=83): 253.4 ms =C2=B1 0.3 ms [User: 193= .6 ms, System: 56.6 ms] >> Range (min =E2=80=A6 max): 253.0 ms =E2=80=A6 253.8 ms 11 runs >=20 > OK, so here the improvement is less impressive than the previous commit. > And the code is... >=20 >> { >> + static const char padding[] =3D " "; >> + size_t min_len =3D padded ? strlen(padding) : 0; >> + size_t orig_len =3D line->len; >> + size_t len; >> + >> if (type =3D=3D 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 =3D line->len - orig_len; >> + if (len < min_len) >> + strbuf_insert(line, orig_len, padding, min_len - len); >> } >=20 > ...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. > I wonder how bad it would be to handle the padding ahead of time. > Obviously strbuf_add_uint() knows the size of the result right before it > calls memcpy(), and it could insert the padding then. But adding a > padding length parameter (let alone the space vs "0" decision) to that > function feels kind of gross. >=20 > In the earlier patch I raised the notion of pre-computing the output > length. If we had a helper to do that, it would be pretty easy to do: >=20 > /* noop if third parameter is negative */ > strbuf_pad(line, ' ', 7 - decimal_digits(size)); > strbuf_add_uint(line, size); I started with a struct numbuf for holding a number string and its length, which helped avoid the memmove(3) call. It's simple and doesn't require a lot of code, but introducing that concept felt a bit much for just two users. > You could also imagine a world where we had some stateful formatting > system, and you could say: >=20 > strbuf_pad_next(line, ' ', 7); > strbuf_add_uint(line, size); >=20 > but somebody has to store that state between the calls, and I don't love > the idea of bloating strbuf with it. So probably you have some > "formatter" struct, and it operates on a strbuf. And now we have all of > the OO boilerplate hassles like initializing and tearing down our > formatter object. ;) So probably not worth it for this triviality. Terrifying! > Having it all in one string ("%7d") is nice and concise. Yes, except the original code includes the 7 twice (again for the "-" fallback). > 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 extensible printf-like formatter would be nice indeed. I wondered about how something like that could be used to write structured output like tar and zip archive entries in a terse way. The thought faded, I guess, when I found no compelling reason for that compactness that would justify the required complexity of the mechanism. Ren=C3=A9