Skip to content

Commit

Permalink
Optimize Iterator::count for event iterators (bevyengine#7582)
Browse files Browse the repository at this point in the history
# Objective

Related to bevyengine#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.
  • Loading branch information
JoJoJet authored and myreprise1 committed Feb 15, 2023
1 parent 90b61a9 commit b0b8e55
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/bevy_ecs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit b0b8e55

Please sign in to comment.