Optimize Iterator::count for event iterators (#7582)

# Objective

Related to #7530.

`EventReader` iterators currently use the default impl for `.count()`, which unnecessarily loops over all unread events.

# Solution

Add specialized impls that mark the `EventReader` as consumed and return the number of unread events.
This commit is contained in:
JoJoJet 2023-02-13 18:20:21 +00:00
parent 1bd390806f
commit 10d0336287

View file

@ -378,6 +378,10 @@ impl<'a, E: Event> Iterator for ManualEventIterator<'a, E> {
self.iter.next().map(|(event, _)| event) self.iter.next().map(|(event, _)| event)
} }
fn count(self) -> usize {
self.iter.count()
}
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint() self.iter.size_hint()
} }
@ -445,6 +449,11 @@ impl<'a, E: Event> Iterator for ManualEventIteratorWithId<'a, E> {
} }
} }
fn count(self) -> usize {
self.reader.last_event_count += self.unread;
self.unread
}
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {
self.chain.size_hint() self.chain.size_hint()
} }