From: "René Scharfe" <l.s.r@web.de>
To: Git List <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>,
Jonathan Tan <jonathantanmy@google.com>
Subject: [PATCH] sha1_file: avoid comparison if no packed hash matches the first byte
Date: Wed, 9 Aug 2017 00:07:30 +0200 [thread overview]
Message-ID: <0c1f898c-46c4-033d-001b-114b17d7d36f@web.de> (raw)
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>
---
sha1_file.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/sha1_file.c b/sha1_file.c
index b60ae15f70..11ee69a99d 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2799,7 +2799,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);
@@ -2812,7 +2812,7 @@ off_t find_pack_entry_one(const unsigned char *sha1,
hi = mi;
else
lo = mi+1;
- } while (lo < hi);
+ }
return 0;
}
--
2.14.0
next reply other threads:[~2017-08-08 22:07 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-08 22:07 René Scharfe [this message]
2017-08-08 22:41 ` [PATCH] sha1_file: avoid comparison if no packed hash matches the first byte Jonathan Nieder
2017-08-08 22:43 ` Junio C Hamano
2017-08-08 22:52 ` Jeff King
2017-08-08 22:58 ` Jeff King
2017-08-09 5:36 ` Junio C Hamano
2017-08-09 9:20 ` Jeff King
2017-08-09 10:11 ` Jeff King
2017-08-09 10:14 ` [PATCH 1/2] sha1_file: drop experimental GIT_USE_LOOKUP search Jeff King
2017-08-09 18:12 ` Junio C Hamano
2017-08-09 21:09 ` Jeff King
2017-08-09 10:16 ` [PATCH 2/2] hashcmp: use memcmp instead of open-coded loop Jeff King
2017-08-09 14:55 ` René Scharfe
2017-08-09 15:06 ` Jeff King
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0c1f898c-46c4-033d-001b-114b17d7d36f@web.de \
--to=l.s.r@web.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jonathantanmy@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).