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 Oct 31, 2019
1 parent d73460b commit 33a2465
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/adaptors/mod.rs
Expand Up @@ -243,6 +243,28 @@ 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> {
match self.top {
None => self.iter.nth(n),
ref mut some => {
if n == 0 {
some.take()
} else {
*some = None;
self.iter.nth(n - 1)
}
}
}
}

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

0 comments on commit 33a2465

Please sign in to comment.