Skip to content

Commit

Permalink
Optimize Iterator::count for event iterators (#7582)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
JoJoJet committed Feb 13, 2023
1 parent 1bd3908 commit 10d0336
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 10d0336

Please sign in to comment.