mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 12:43:34 +00:00
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:
parent
1bd390806f
commit
10d0336287
1 changed files with 9 additions and 0 deletions
|
@ -378,6 +378,10 @@ impl<'a, E: Event> Iterator for ManualEventIterator<'a, E> {
|
|||
self.iter.next().map(|(event, _)| event)
|
||||
}
|
||||
|
||||
fn count(self) -> usize {
|
||||
self.iter.count()
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
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>) {
|
||||
self.chain.size_hint()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue