ok/jj
1
0
Fork 0
forked from mirrors/jj

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.
This commit is contained in:
Yuya Nishihara 2023-01-26 15:29:36 +09:00
parent b4c837fd4a
commit 956a2d5f83
2 changed files with 8 additions and 11 deletions

View file

@ -1333,7 +1333,7 @@ impl IndexSegment for ReadonlyIndex {
}
fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
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<CommitId> {
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 {

View file

@ -1306,12 +1306,11 @@ where
&'a self,
prefix: &'b HexPrefix,
) -> impl Iterator<Item = (&'a K, &'a V)> + 'b {
let (bytes_prefix, min_bytes_prefix) = prefix.bytes_prefixes::<K>();
let (_, min_bytes_prefix) = prefix.bytes_prefixes::<K>();
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))
}