From 956a2d5f837186e1a8b642acd34ca76ea74e5e57 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 26 Jan 2023 15:29:36 +0900 Subject: [PATCH] index: remove redundant prefix tests from resolve_prefix functions The "min" prefix guarantees that the first entry matches the hex prefix if any. Spotted by @ilyagr. --- lib/src/index.rs | 14 ++++++-------- lib/src/repo.rs | 5 ++--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/src/index.rs b/lib/src/index.rs index cc0180514..b3061222d 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -1333,7 +1333,7 @@ impl IndexSegment for ReadonlyIndex { } fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { - let (bytes_prefix, min_bytes_prefix) = prefix.bytes_prefixes(); + let (_, min_bytes_prefix) = prefix.bytes_prefixes(); let lookup_pos = self .commit_id_byte_prefix_to_lookup_pos(&min_bytes_prefix) .unwrap_or(self.num_local_commits); @@ -1341,14 +1341,13 @@ impl IndexSegment for ReadonlyIndex { for i in lookup_pos..self.num_local_commits { let entry = self.lookup_entry(i); let id = entry.commit_id(); - if !id.as_bytes().starts_with(bytes_prefix.as_bytes()) { - break; - } if prefix.matches(&id) { if first_match.is_some() { return PrefixResolution::AmbiguousMatch; } first_match = Some(id) + } else { + break; } } match first_match { @@ -1437,20 +1436,19 @@ impl IndexSegment for MutableIndex { } fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { - let (bytes_prefix, min_bytes_prefix) = prefix.bytes_prefixes(); + let (_, min_bytes_prefix) = prefix.bytes_prefixes(); let potential_range = self .lookup .range((Bound::Included(&min_bytes_prefix), Bound::Unbounded)); let mut first_match = None; for (id, _pos) in potential_range { - if !id.as_bytes().starts_with(bytes_prefix.as_bytes()) { - break; - } if prefix.matches(id) { if first_match.is_some() { return PrefixResolution::AmbiguousMatch; } first_match = Some(id) + } else { + break; } } match first_match { diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 4cb83353b..23e2b2dd4 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -1306,12 +1306,11 @@ where &'a self, prefix: &'b HexPrefix, ) -> impl Iterator + 'b { - let (bytes_prefix, min_bytes_prefix) = prefix.bytes_prefixes::(); + let (_, min_bytes_prefix) = prefix.bytes_prefixes::(); let pos = self.0.partition_point(|(k, _)| k < &min_bytes_prefix); self.0[pos..] .iter() - .take_while(move |(k, _)| k.as_bytes().starts_with(bytes_prefix.as_bytes())) - .filter(|(k, _)| prefix.matches(k)) + .take_while(|(k, _)| prefix.matches(k)) .map(|(k, v)| (k, v)) }