Skip to content

Commit 6355a76

Browse files
rscharfegitster
authored andcommitted
sha1_file: avoid comparison if no packed hash matches the first byte
find_pack_entry_one() uses the fan-out table of pack indexes to find out which entries match the first byte of the searched hash and does a binary search on this subset of the main index table. If there are no matching entries then lo and hi will have the same value. The binary search still starts and compares the hash of the following entry (which has a non-matching first byte, so won't cause any trouble), or whatever comes after the sorted list of entries. The probability of that stray comparison matching by mistake is low, but let's not take any chances and check when entering the binary search loop if we're actually done already. Signed-off-by: Rene Scharfe <[email protected]> Reviewed-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 840ed14 commit 6355a76

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

sha1_file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,7 +2526,7 @@ off_t find_pack_entry_one(const unsigned char *sha1,
25262526
return nth_packed_object_offset(p, pos);
25272527
}
25282528

2529-
do {
2529+
while (lo < hi) {
25302530
unsigned mi = (lo + hi) / 2;
25312531
int cmp = hashcmp(index + mi * stride, sha1);
25322532

@@ -2539,7 +2539,7 @@ off_t find_pack_entry_one(const unsigned char *sha1,
25392539
hi = mi;
25402540
else
25412541
lo = mi+1;
2542-
} while (lo < hi);
2542+
}
25432543
return 0;
25442544
}
25452545

0 commit comments

Comments
 (0)