]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sha1_file: avoid comparison if no packed hash matches the first byte
authorRené Scharfe <l.s.r@web.de>
Tue, 8 Aug 2017 22:07:30 +0000 (00:07 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 9 Aug 2017 16:52:25 +0000 (09:52 -0700)
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 <l.s.r@web.de>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
sha1_file.c

index 727a9769fb7549e365e66f4ecb65f8e665898a96..3047ee04f1ba50967836ba318a0b96e96d5962b4 100644 (file)
@@ -2526,7 +2526,7 @@ off_t find_pack_entry_one(const unsigned char *sha1,
                return nth_packed_object_offset(p, pos);
        }
 
-       do {
+       while (lo < hi) {
                unsigned mi = (lo + hi) / 2;
                int cmp = hashcmp(index + mi * stride, sha1);
 
@@ -2539,7 +2539,7 @@ off_t find_pack_entry_one(const unsigned char *sha1,
                        hi = mi;
                else
                        lo = mi+1;
-       } while (lo < hi);
+       }
        return 0;
 }