From a57498795516e4d7fdc50a0c5212a6e13ea2c0fc Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 20 Jan 2023 13:58:43 +0900 Subject: [PATCH] index: remove redundant slicing from commit_id-prefix search function If commit_id[..prefix_len] < prefix, commit_id < prefix is obviously true. If commit_id[..prefix_len] == prefix, commit_id < prefix returns false. So slicing isn't needed. This makes commit_id_byte_prefix_to_pos() basically the same as segment_commit_id_to_pos(), and these two functions can be merged. --- lib/src/index.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/src/index.rs b/lib/src/index.rs index a0c1bc61f..cbd1859f4 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -1621,18 +1621,15 @@ impl ReadonlyIndex { } let mut low = 0; let mut high = self.num_local_commits - 1; - let prefix_len = prefix.as_bytes().len(); // 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(); - let entry_prefix = &entry_commit_id.as_bytes()[0..prefix_len]; if high == low { return Some(IndexPosition(mid)); } - if entry_prefix < prefix.as_bytes() { + let entry = self.lookup_entry(mid); + if entry.commit_id().as_bytes() < prefix.as_bytes() { low = mid + 1; } else { high = mid;