Skip to content

Commit

Permalink
feat: Make it possible to access the current commits buffer during co…
Browse files Browse the repository at this point in the history
…mmit ancestor iteration. (#470)

This is useful to avoid additional lookups of the same object for
reading additional data from it.

Currently one needs an object cache to avoid duplciate object extraction
work, with such a cache being slower than accessing the same buffer
again.
  • Loading branch information
Byron committed Sep 19, 2022
1 parent 8c2e5c6 commit 6b2af57
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions git-traverse/src/commit.rs
Expand Up @@ -46,6 +46,7 @@ impl Default for Sorting {

///
pub mod ancestors {
use std::borrow::Borrow;
use std::{borrow::BorrowMut, collections::VecDeque, iter::FromIterator};

use git_hash::{oid, ObjectId};
Expand Down Expand Up @@ -85,6 +86,7 @@ pub mod ancestors {
}
}

/// Builder
impl<Find, Predicate, StateMut> Ancestors<Find, Predicate, StateMut> {
/// Change our commit parent handling mode to the given one.
pub fn parents(mut self, mode: Parents) -> Self {
Expand All @@ -93,6 +95,7 @@ pub mod ancestors {
}
}

/// Builder
impl<Find, Predicate, StateMut, E> Ancestors<Find, Predicate, StateMut>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
Expand All @@ -119,6 +122,7 @@ pub mod ancestors {
}
}

/// Initialization
impl<Find, StateMut, E> Ancestors<Find, fn(&oid) -> bool, StateMut>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
Expand All @@ -140,6 +144,7 @@ pub mod ancestors {
}
}

/// Initialization
impl<Find, Predicate, StateMut, E> Ancestors<Find, Predicate, StateMut>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
Expand Down Expand Up @@ -186,6 +191,16 @@ pub mod ancestors {
}
}
}
/// Access
impl<Find, Predicate, StateMut> Ancestors<Find, Predicate, StateMut>
where
StateMut: Borrow<State>,
{
/// Return an iterator for accessing more of the current commits data.
pub fn commit_iter(&self) -> CommitRefIter<'_> {
CommitRefIter::from_bytes(&self.state.borrow().buf)
}
}

impl<Find, Predicate, StateMut, E> Iterator for Ancestors<Find, Predicate, StateMut>
where
Expand All @@ -208,6 +223,7 @@ pub mod ancestors {
}
}

/// Utilities
impl<Find, Predicate, StateMut, E> Ancestors<Find, Predicate, StateMut>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
Expand Down Expand Up @@ -272,6 +288,7 @@ pub mod ancestors {
}
}

/// Utilities
impl<Find, Predicate, StateMut, E> Ancestors<Find, Predicate, StateMut>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
Expand Down

0 comments on commit 6b2af57

Please sign in to comment.