]> git.ipfire.org Git - thirdparty/git.git/commit
use __builtin_add_overflow() in st_add() with Clang
authorRené Scharfe <l.s.r@web.de>
Mon, 18 May 2026 20:25:02 +0000 (22:25 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 19 May 2026 00:52:22 +0000 (09:52 +0900)
commit29d9fdcf1098672b2146c60eea5202e840615772
tree5b6e341dc9f32510c50121da53d08ef673820228
parentc13d0f7bd4e696d5db3345e00f763df23347b6ad
use __builtin_add_overflow() in st_add() with Clang

Clang and GCC optimize away comparisons of overflow checks by checking
the carry flag on x64.  GCC does the same on ARM64, but Clang currently
(version 22.1) doesn't.

It does this optimization for overflow checks that use its builtin
function __builtin_add_overflow(), though.  Provide a non-generic
lookalike for size_t that does the same checks as before as a fallback
and use the original with Clang.  Use it on all platforms for simplicity.

On an Apple M1 I get a nice speedup for a command that builds lots of
strings using a strbuf, which exercises the st_add3() in strbuf_grow()
for every line of output:

Benchmark 1: ./git_main cat-file --batch-all-objects --batch-check='%(objectname)'
  Time (mean ± σ):     120.4 ms ±   0.2 ms    [User: 113.8 ms, System: 6.0 ms]
  Range (min … max):   120.1 ms … 121.1 ms    24 runs

Benchmark 2: ./git cat-file --batch-all-objects --batch-check='%(objectname)'
  Time (mean ± σ):     115.5 ms ±   0.1 ms    [User: 108.6 ms, System: 5.8 ms]
  Range (min … max):   115.2 ms … 115.8 ms    25 runs

Summary
  ./git cat-file --batch-all-objects --batch-check='%(objectname)' ran
    1.04 ± 0.00 times faster than ./git_main cat-file --batch-all-objects --batch-check='%(objectname)'

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-compat-util.h