From 2b7021664d1d10d0bca1b19b4f7a400af8586ba7 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 20 Jan 2023 13:50:40 +0900 Subject: [PATCH] index: deduplicate binary search functions of commit_id lookup --- lib/src/index.rs | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/lib/src/index.rs b/lib/src/index.rs index fdfaae44c..d59b58f7f 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -1233,30 +1233,12 @@ impl IndexSegment for ReadonlyIndex { } fn segment_commit_id_to_pos(&self, commit_id: &CommitId) -> Option { - if self.num_local_commits == 0 { - // Avoid overflow when subtracting 1 below - return None; - } - let mut low = 0; - let mut high = self.num_local_commits - 1; - - // binary search for the commit id - loop { - let mid = (low + high) / 2; - let entry = self.lookup_entry(mid); - let entry_commit_id = entry.commit_id(); - if high == low { - return if &entry_commit_id == commit_id { - Some(entry.pos()) - } else { - None - }; - } - if commit_id > &entry_commit_id { - low = mid + 1; - } else { - high = mid; - } + let lookup_pos = self.commit_id_byte_prefix_to_lookup_pos(commit_id)?; + let entry = self.lookup_entry(lookup_pos); + if &entry.commit_id() == commit_id { + Some(entry.pos()) + } else { + None } }