bevy/crates/bevy_ecs/src/query
James Liu 2148518758
Override QueryIter::fold to port Query::for_each perf gains to select Iterator combinators (#6773)
# Objective
After #6547, `Query::for_each` has been capable of automatic
vectorization on certain queries, which is seeing a notable (>50% CPU
time improvements) for iteration. However, `Query::for_each` isn't
idiomatic Rust, and lacks the flexibility of iterator combinators.

Ideally, `Query::iter` and friends should be able to achieve the same
results. However, this does seem to blocked upstream
(rust-lang/rust#104914) by Rust's loop optimizations.

## Solution
This is an intermediate solution and refactor. This moves the
`Query::for_each` implementation onto the `Iterator::fold`
implementation for `QueryIter` instead. This should result in the same
automatic vectorization optimization on all `Iterator` functions that
internally use fold, including `Iterator::for_each`, `Iterator::count`,
etc.

With this, it should close the gap between the two completely.
Internally, this PR changes `Query::for_each` to use
`query.iter().for_each(..)` instead of the duplicated implementation.

Separately, the duplicate implementations of internal iteration (i.e.
`Query::par_for_each`) now use portions of the current `Query::for_each`
implementation factored out into their own functions.

This also massively cleans up our internal fragmentation of internal
iteration options, deduplicating the iteration code used in `for_each`
and `par_iter().for_each()`.

---

## Changelog
Changed: `Query::for_each`, `Query::for_each_mut`, `Query::for_each`,
and `Query::for_each_mut` have been moved to `QueryIter`'s
`Iterator::for_each` implementation, and still retains their performance
improvements over normal iteration. These APIs are deprecated in 0.13
and will be removed in 0.14.

---------

Co-authored-by: JoJoJet <21144246+JoJoJet@users.noreply.github.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2023-12-01 09:09:55 +00:00
..
access.rs Allow disjoint mutable world access via EntityMut (#9419) 2023-08-28 23:30:59 +00:00
error.rs Derive Error for more error types (#10240) 2023-10-28 22:20:37 +00:00
fetch.rs Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
filter.rs Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
iter.rs Override QueryIter::fold to port Query::for_each perf gains to select Iterator combinators (#6773) 2023-12-01 09:09:55 +00:00
mod.rs Override QueryIter::fold to port Query::for_each perf gains to select Iterator combinators (#6773) 2023-12-01 09:09:55 +00:00
par_iter.rs Override QueryIter::fold to port Query::for_each perf gains to select Iterator combinators (#6773) 2023-12-01 09:09:55 +00:00
state.rs Override QueryIter::fold to port Query::for_each perf gains to select Iterator combinators (#6773) 2023-12-01 09:09:55 +00:00
world_query.rs Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00