Skip to content

Commit

Permalink
index: pass bytes prefix to binary search function
Browse files Browse the repository at this point in the history
This helps extract common binary search helper to be used by change id index.
  • Loading branch information
yuja committed Feb 14, 2024
1 parent 2a92fe5 commit a82a0a6
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/src/default_index/readonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,13 @@ impl ReadonlyIndexSegment {
/// If the `prefix` matches exactly, returns `Ok` with the lookup position.
/// Otherwise, returns `Err` containing the position where the id could be
/// inserted.
fn commit_id_byte_prefix_to_lookup_pos(&self, prefix: &CommitId) -> Result<u32, u32> {
fn commit_id_byte_prefix_to_lookup_pos(&self, prefix: &[u8]) -> Result<u32, u32> {
let mut low = 0;
let mut high = self.num_local_commits;
while low < high {
let mid = (low + high) / 2;
let entry = self.commit_lookup_entry(mid);
let cmp = entry.commit_id_bytes().cmp(prefix.as_bytes());
let cmp = entry.commit_id_bytes().cmp(prefix);
// According to Rust std lib, this produces cmov instructions.
// https://github.com/rust-lang/rust/blob/1.76.0/library/core/src/slice/mod.rs#L2845-L2855
low = if cmp == Ordering::Less { mid + 1 } else { low };
Expand Down Expand Up @@ -409,7 +409,9 @@ impl IndexSegment for ReadonlyIndexSegment {
}

fn commit_id_to_pos(&self, commit_id: &CommitId) -> Option<LocalPosition> {
let lookup_pos = self.commit_id_byte_prefix_to_lookup_pos(commit_id).ok()?;
let lookup_pos = self
.commit_id_byte_prefix_to_lookup_pos(commit_id.as_bytes())
.ok()?;
let entry = self.commit_lookup_entry(lookup_pos);
Some(entry.local_pos())
}
Expand All @@ -419,7 +421,7 @@ impl IndexSegment for ReadonlyIndexSegment {
commit_id: &CommitId,
) -> (Option<CommitId>, Option<CommitId>) {
let (prev_lookup_pos, next_lookup_pos) =
match self.commit_id_byte_prefix_to_lookup_pos(commit_id) {
match self.commit_id_byte_prefix_to_lookup_pos(commit_id.as_bytes()) {
Ok(pos) => (pos.checked_sub(1), (pos + 1..self.num_local_commits).next()),
Err(pos) => (pos.checked_sub(1), (pos..self.num_local_commits).next()),
};
Expand All @@ -429,9 +431,8 @@ impl IndexSegment for ReadonlyIndexSegment {
}

fn resolve_commit_id_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
let min_bytes_prefix = CommitId::from_bytes(prefix.min_prefix_bytes());
let lookup_pos = self
.commit_id_byte_prefix_to_lookup_pos(&min_bytes_prefix)
.commit_id_byte_prefix_to_lookup_pos(prefix.min_prefix_bytes())
.unwrap_or_else(|pos| pos);
let mut matches = (lookup_pos..self.num_local_commits)
.map(|pos| self.commit_lookup_entry(pos).commit_id())
Expand Down

0 comments on commit a82a0a6

Please sign in to comment.