Skip to content

Commit

Permalink
index: rewrite CompositeIndex::entry_by_pos() by leveraging ancestors…
Browse files Browse the repository at this point in the history
… iterator

We no longer have "unsafe" in this function, so let's use the iterator API
instead of recursion. Apparently I haven't pushed this change before because
unsafe in .find_map() looked scary.
  • Loading branch information
yuja committed Nov 8, 2023
1 parent d27351b commit 084b99e
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/src/default_index_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,12 +780,12 @@ trait IndexSegment: Send + Sync {
pub struct CompositeIndex<'a>(&'a dyn IndexSegment);

impl<'a> CompositeIndex<'a> {
fn ancestor_files_without_local(&self) -> impl Iterator<Item = &Arc<ReadonlyIndexImpl>> {
fn ancestor_files_without_local(&self) -> impl Iterator<Item = &'a Arc<ReadonlyIndexImpl>> {
let parent_file = self.0.segment_parent_file();
iter::successors(parent_file, |file| file.segment_parent_file())
}

fn ancestor_index_segments(&self) -> impl Iterator<Item = &dyn IndexSegment> {
fn ancestor_index_segments(&self) -> impl Iterator<Item = &'a dyn IndexSegment> {
iter::once(self.0).chain(
self.ancestor_files_without_local()
.map(|file| file.as_ref() as &dyn IndexSegment),
Expand Down Expand Up @@ -835,13 +835,12 @@ impl<'a> CompositeIndex<'a> {
}

pub fn entry_by_pos(&self, pos: IndexPosition) -> IndexEntry<'a> {
let num_parent_commits = self.0.segment_num_parent_commits();
if pos.0 >= num_parent_commits {
self.0.segment_entry_by_pos(pos, pos.0 - num_parent_commits)
} else {
let parent_file = self.0.segment_parent_file().unwrap();
CompositeIndex(&**parent_file).entry_by_pos(pos)
}
self.ancestor_index_segments()
.find_map(|segment| {
u32::checked_sub(pos.0, segment.segment_num_parent_commits())
.map(|local_pos| segment.segment_entry_by_pos(pos, local_pos))
})
.unwrap()
}

pub fn commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition> {
Expand Down

0 comments on commit 084b99e

Please sign in to comment.