Skip to content

[Merged by Bors] - Optimize Iterator::count for event iterators - #7582

Closed
joseph-gio wants to merge 1 commit into
bevyengine:mainfrom
joseph-gio:optimize-event-count
Closed

[Merged by Bors] - Optimize Iterator::count for event iterators#7582
joseph-gio wants to merge 1 commit into
bevyengine:mainfrom
joseph-gio:optimize-event-count

Conversation

@joseph-gio

@joseph-gio joseph-gio commented Feb 9, 2023

Copy link
Copy Markdown
Member

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.

@joseph-gio joseph-gio added A-ECS Entities, components, systems, and events C-Performance A change motivated by improving speed, memory usage or compile times labels Feb 9, 2023
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Feb 9, 2023
@mockersf

mockersf commented Feb 9, 2023

Copy link
Copy Markdown
Member

Those iterators also implement ExactSizedIterator which adds a len() method that is optimised. Implementing them that way would mean that the doc for those trait methods would be wrong.

@joseph-gio

Copy link
Copy Markdown
Member Author

Implementing them that way would mean that the doc for those trait methods would be wrong.

What do you mean by this?

@mockersf

mockersf commented Feb 9, 2023

Copy link
Copy Markdown
Member

the doc for the trait methods is https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.count which specifically mention calling all the next

@joseph-gio

joseph-gio commented Feb 9, 2023

Copy link
Copy Markdown
Member Author

Correct me if I'm wrong, but there is no observable difference between repeatedly calling next and the approach used in this PR. It's the same exact behavior, just more efficient.

@alice-i-cecile alice-i-cecile removed the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Feb 9, 2023
@mockersf

Copy link
Copy Markdown
Member

Correct me if I'm wrong, but there is no observable difference between repeatedly calling next and the approach used in this PR. It's the same exact behavior, just more efficient.

You're right. Let's hope no one rely on something being not the most efficient 😄

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Feb 10, 2023

@alice-i-cecile alice-i-cecile left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging as trivial now that we've discussed it.

bors r+

bors Bot pushed a commit that referenced this pull request Feb 13, 2023
# 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.
@bors bors Bot changed the title Optimize Iterator::count for event iterators [Merged by Bors] - Optimize Iterator::count for event iterators Feb 13, 2023
@bors bors Bot closed this Feb 13, 2023
myreprise1 pushed a commit to myreprise1/bevy that referenced this pull request Feb 14, 2023
# 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.
@joseph-gio
joseph-gio deleted the optimize-event-count branch February 14, 2023 00:53
myreprise1 pushed a commit to myreprise1/bevy that referenced this pull request Feb 15, 2023
# 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ECS Entities, components, systems, and events C-Performance A change motivated by improving speed, memory usage or compile times S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants