Skip to content

Commit

Permalink
Specialize count, last and nth PutBack iterator for performance
Browse files Browse the repository at this point in the history
They were missing and did hurt the MergeJoinBy performance improvement on the `nth` method because we couldn't borrow iter without exposing new interfaces or using something like the `take_mut` crate.
  • Loading branch information
Ten0 committed Sep 28, 2019
1 parent cf41bec commit cb71c58
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/adaptors/mod.rs
Expand Up @@ -243,6 +243,26 @@ impl<I> Iterator for PutBack<I>
size_hint::add_scalar(self.iter.size_hint(), self.top.is_some() as usize)
}

fn count(self) -> usize {
self.iter.count() + (self.top.is_some() as usize)
}

fn last(self) -> Option<Self::Item> {
self.iter.last().or(self.top)
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
if let Some(top) = self.top.take() {
if n == 0 {
Some(top)
} else {
self.iter.nth(n - 1)
}
} else {
self.iter.nth(n)
}
}

fn all<G>(&mut self, mut f: G) -> bool
where G: FnMut(Self::Item) -> bool
{
Expand Down

0 comments on commit cb71c58

Please sign in to comment.