Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from

Conversation

JoJoJet
Copy link
Member

@JoJoJet JoJoJet commented Feb 9, 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.

@JoJoJet JoJoJet 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
Copy link
Member

mockersf commented Feb 9, 2023

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.

@JoJoJet
Copy link
Member Author

JoJoJet commented Feb 9, 2023

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

What do you mean by this?

@mockersf
Copy link
Member

mockersf commented Feb 9, 2023

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

@JoJoJet
Copy link
Member Author

JoJoJet commented Feb 9, 2023

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
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
Copy link
Member

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

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.
@JoJoJet JoJoJet 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.

None yet

3 participants