Use git_oid_raw_cmp instead of custom loop in git_oid_is_zero

Since git_oid_raw_cmp uses memcmp, it's far easier to optimise more aggressively.
Given that it's static const and used inlined, it seems reasonable to assume that git_oid_zero will be optimised away in release. 
This is at least the case with x86-64 and GCC 15.2.1.
This commit is contained in:
Alf Henrik Sauge
2026-01-06 20:20:46 +01:00
parent 3ac4c0adb1
commit 73b8b4eb74

View File

@@ -22,6 +22,8 @@ const git_oid git_oid__empty_tree_sha1 =
{ 0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60,
0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04 });
static const unsigned char git_oid_zero[GIT_OID_MAX_SIZE] = {0};
static int oid_error_invalid(const char *msg)
{
git_error_set(GIT_ERROR_INVALID, "unable to parse OID - %s", msg);
@@ -292,7 +294,7 @@ int git_oid_streq(const git_oid *oid_a, const char *str)
int git_oid_is_zero(const git_oid *oid_a)
{
const unsigned char *a = oid_a->id;
size_t size = git_oid_size(git_oid_type(oid_a)), i;
size_t size = git_oid_size(git_oid_type(oid_a));
#ifdef GIT_EXPERIMENTAL_SHA256
if (!oid_a->type)
@@ -301,10 +303,7 @@ int git_oid_is_zero(const git_oid *oid_a)
return 0;
#endif
for (i = 0; i < size; ++i, ++a)
if (*a != 0)
return 0;
return 1;
return git_oid_raw_cmp(a, git_oid_zero, size) == 0 ? 1 : 0;
}
#ifndef GIT_DEPRECATE_HARD