Skip to content

Commit 0b00601

Browse files
peffgitster
authored andcommitted
hashcmp: use memcmp instead of open-coded loop
In 1a812f3 (hashcmp(): inline memcmp() by hand to optimize, 2011-04-28), it was reported that an open-coded loop outperformed memcmp() for comparing sha1s. Discussion[1] a few years later in 2013 showed that this depends on your libc's version of memcmp(). In particular, glibc 2.13 optimized their memcmp around 2011. Here are current timings with glibc 2.24 (best-of-five, on linux.git): [before this patch, open-coded] $ time git rev-list --objects --all real 0m35.357s user 0m35.016s sys 0m0.340s [after this patch, memcmp] real 0m32.930s user 0m32.630s sys 0m0.300s Now that we've had 6 years for that version of glibc to make its way onto people's machines, it's worth revisiting our benchmarks and switching to memcmp(). It may be that there are other non-glibc systems where memcmp() isn't as well optimized. But since our single data point in favor of open-coding was on a now-ancient glibc, we should probably assume the system memcmp is good unless proven otherwise. We may end up with a SLOW_MEMCMP Makefile knob, but we can hold off on that until we actually find such a system in practice. [1] https://public-inbox.org/git/[email protected]/ Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 95d6787 commit 0b00601

File tree

1 file changed

+1
-8
lines changed

1 file changed

+1
-8
lines changed

cache.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -962,14 +962,7 @@ extern const struct object_id null_oid;
962962

963963
static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
964964
{
965-
int i;
966-
967-
for (i = 0; i < GIT_SHA1_RAWSZ; i++, sha1++, sha2++) {
968-
if (*sha1 != *sha2)
969-
return *sha1 - *sha2;
970-
}
971-
972-
return 0;
965+
return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
973966
}
974967

975968
static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)

0 commit comments

Comments
 (0)