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

Override QueryIter::fold to port Query::for_each perf gains to select Iterator combinators #6773

Merged
merged 30 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a2d6b13
Move for_each implementation onto Iterator::fold
james7132 Nov 26, 2022
7d17204
Fix starting fold from the middle of iteration
james7132 Nov 27, 2022
2c5af86
Simplify for_each cases and add safety docs
james7132 Nov 27, 2022
16b94dc
Undo test changes to empty.rs
james7132 Nov 27, 2022
fe681cb
Formatting
james7132 Nov 27, 2022
da27171
Merge branch 'main' into query-iter-fold
james7132 Dec 4, 2022
e0ea6c8
Merge branch 'main' into query-iter-fold
james7132 Jan 7, 2023
a11827f
Fix build
james7132 Jan 7, 2023
2abc327
Fix CI
james7132 Jan 7, 2023
abadb6f
Merge branch 'main' into query-iter-fold
james7132 Feb 17, 2023
1883adc
Revert QueryIterationCursor::fetch
james7132 Feb 17, 2023
c57481a
Deprecate the functions
james7132 Feb 18, 2023
65028bf
Apply suggestions from code review
james7132 Feb 19, 2023
2f997d0
Formatting
james7132 Feb 19, 2023
4bde3f4
Address deprecation
james7132 Feb 19, 2023
bca5e61
Update the safety comments.
james7132 Feb 19, 2023
df3d3bb
Address JoJoJet's comment
james7132 Feb 19, 2023
ca81c1c
make Query::for_each safer
james7132 Feb 19, 2023
dab9734
Remove mentions of QueryState::for_each_unchecked_manual
james7132 Feb 19, 2023
d166843
Merge branch 'main' into query-iter-fold
james7132 Mar 5, 2023
0d7d7da
Merge branch 'main' into query-iter-fold
james7132 Nov 26, 2023
d13e9a6
Update deprecation versions
james7132 Nov 26, 2023
144bb14
Fix CI and rename fold_*/for_each_*
james7132 Nov 26, 2023
624c3c5
Fix CI
james7132 Nov 26, 2023
94bc428
Fix UX tests
james7132 Nov 26, 2023
381bfbb
Fix deprecation notice for iter_mut().for_each
james7132 Nov 26, 2023
6307015
Merge branch 'main' into query-iter-fold
alice-i-cecile Nov 28, 2023
541f4c5
Inline for_each_in*
james7132 Dec 1, 2023
997ab70
Fix CI
james7132 Dec 1, 2023
3a9c92c
Remove the for_each_* functions when multithreading is not required.
james7132 Dec 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_frag_foreach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'w> Benchmark<'w> {
}

pub fn run(&mut self) {
self.1.for_each_mut(&mut self.0, |mut data| {
self.1.iter_mut(&mut self.0).for_each(|mut data| {
data.0 *= 2.0;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<'w> Benchmark<'w> {
}

pub fn run(&mut self) {
self.1.for_each_mut(&mut self.0, |mut data| {
self.1.iter_mut(&mut self.0).for_each(|mut data| {
data.0 *= 2.0;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'w> Benchmark<'w> {
}

pub fn run(&mut self) {
self.1.for_each_mut(&mut self.0, |mut data| {
self.1.iter_mut(&mut self.0).for_each(|mut data| {
data.0 .0 *= 2.0;
data.1 .0 *= 2.0;
data.2 .0 *= 2.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'w> Benchmark<'w> {
}

pub fn run(&mut self) {
self.1.for_each_mut(&mut self.0, |mut data| {
self.1.iter_mut(&mut self.0).for_each(|mut data| {
data.0 .0 *= 2.0;
data.1 .0 *= 2.0;
data.2 .0 *= 2.0;
Expand Down
3 changes: 2 additions & 1 deletion benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ impl<'w> Benchmark<'w> {

pub fn run(&mut self) {
self.1
.for_each_mut(&mut self.0, |(velocity, mut position)| {
.iter_mut(&mut self.0)
.for_each(|(velocity, mut position)| {
position.0 += velocity.0;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ impl<'w> Benchmark<'w> {

pub fn run(&mut self) {
self.1
.for_each_mut(&mut self.0, |(velocity, mut position)| {
.iter_mut(&mut self.0)
.for_each(|(velocity, mut position)| {
position.0 += velocity.0;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'w> Benchmark<'w> {
}

pub fn run(&mut self) {
self.1.for_each_mut(&mut self.0, |mut item| {
self.1.iter_mut(&mut self.0).for_each(|mut item| {
item.1 .0 += item.0 .0;
item.3 .0 += item.2 .0;
item.5 .0 += item.4 .0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<'w> Benchmark<'w> {
}

pub fn run(&mut self) {
self.1.for_each_mut(&mut self.0, |mut item| {
self.1.iter_mut(&mut self.0).for_each(|mut item| {
item.1 .0 += item.0 .0;
item.3 .0 += item.2 .0;
item.5 .0 += item.4 .0;
Expand Down
14 changes: 7 additions & 7 deletions benches/benches/bevy_ecs/scheduling/running_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ pub fn empty_systems(criterion: &mut Criterion) {

pub fn busy_systems(criterion: &mut Criterion) {
fn ab(mut q: Query<(&mut A, &mut B)>) {
q.for_each_mut(|(mut a, mut b)| {
q.iter_mut().for_each(|(mut a, mut b)| {
std::mem::swap(&mut a.0, &mut b.0);
});
}
fn cd(mut q: Query<(&mut C, &mut D)>) {
q.for_each_mut(|(mut c, mut d)| {
q.iter_mut().for_each(|(mut c, mut d)| {
std::mem::swap(&mut c.0, &mut d.0);
});
}
fn ce(mut q: Query<(&mut C, &mut E)>) {
q.for_each_mut(|(mut c, mut e)| {
q.iter_mut().for_each(|(mut c, mut e)| {
std::mem::swap(&mut c.0, &mut e.0);
});
}
Expand Down Expand Up @@ -103,20 +103,20 @@ pub fn busy_systems(criterion: &mut Criterion) {

pub fn contrived(criterion: &mut Criterion) {
fn s_0(mut q_0: Query<(&mut A, &mut B)>) {
q_0.for_each_mut(|(mut c_0, mut c_1)| {
q_0.iter_mut().for_each(|(mut c_0, mut c_1)| {
std::mem::swap(&mut c_0.0, &mut c_1.0);
});
}
fn s_1(mut q_0: Query<(&mut A, &mut C)>, mut q_1: Query<(&mut B, &mut D)>) {
q_0.for_each_mut(|(mut c_0, mut c_1)| {
q_0.iter_mut().for_each(|(mut c_0, mut c_1)| {
std::mem::swap(&mut c_0.0, &mut c_1.0);
});
q_1.for_each_mut(|(mut c_0, mut c_1)| {
q_1.iter_mut().for_each(|(mut c_0, mut c_1)| {
std::mem::swap(&mut c_0.0, &mut c_1.0);
});
}
fn s_2(mut q_0: Query<(&mut C, &mut D)>) {
q_0.for_each_mut(|(mut c_0, mut c_1)| {
q_0.iter_mut().for_each(|(mut c_0, mut c_1)| {
std::mem::swap(&mut c_0.0, &mut c_1.0);
});
}
Expand Down
6 changes: 3 additions & 3 deletions benches/benches/bevy_ecs/scheduling/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ pub fn schedule(c: &mut Criterion) {
struct E(f32);

fn ab(mut query: Query<(&mut A, &mut B)>) {
query.for_each_mut(|(mut a, mut b)| {
query.iter_mut().for_each(|(mut a, mut b)| {
std::mem::swap(&mut a.0, &mut b.0);
});
}

fn cd(mut query: Query<(&mut C, &mut D)>) {
query.for_each_mut(|(mut c, mut d)| {
query.iter_mut().for_each(|(mut c, mut d)| {
std::mem::swap(&mut c.0, &mut d.0);
});
}

fn ce(mut query: Query<(&mut C, &mut E)>) {
query.for_each_mut(|(mut c, mut e)| {
query.iter_mut().for_each(|(mut c, mut e)| {
std::mem::swap(&mut c.0, &mut e.0);
});
}
Expand Down
4 changes: 2 additions & 2 deletions benches/benches/bevy_ecs/world/world_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pub fn world_query_for_each(criterion: &mut Criterion) {

bencher.iter(|| {
let mut count = 0;
query.for_each(&world, |comp| {
query.iter(&world).for_each(|comp| {
black_box(comp);
count += 1;
black_box(count);
Expand All @@ -244,7 +244,7 @@ pub fn world_query_for_each(criterion: &mut Criterion) {

bencher.iter(|| {
let mut count = 0;
query.for_each(&world, |comp| {
query.iter(&world).for_each(|comp| {
black_box(comp);
count += 1;
black_box(count);
Expand Down
16 changes: 10 additions & 6 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ mod tests {
let mut results = Vec::new();
world
.query::<(Entity, &A, &TableStored)>()
.for_each(&world, |(e, &i, &s)| results.push((e, i, s)));
.iter(&world)
.for_each(|(e, &i, &s)| results.push((e, i, s)));
assert_eq!(
results,
&[
Expand Down Expand Up @@ -391,7 +392,8 @@ mod tests {
let mut results = Vec::new();
world
.query::<(Entity, &A)>()
.for_each(&world, |(e, &i)| results.push((e, i)));
.iter(&world)
.for_each(|(e, &i)| results.push((e, i)));
assert_eq!(results, &[(e, A(123)), (f, A(456))]);
}

Expand Down Expand Up @@ -482,7 +484,8 @@ mod tests {
let mut results = Vec::new();
world
.query_filtered::<&A, With<B>>()
.for_each(&world, |i| results.push(*i));
.iter(&world)
.for_each(|i| results.push(*i));
assert_eq!(results, vec![A(123)]);
}

Expand All @@ -509,7 +512,8 @@ mod tests {
let mut results = Vec::new();
world
.query_filtered::<&A, With<SparseStored>>()
.for_each(&world, |i| results.push(*i));
.iter(&world)
.for_each(|i| results.push(*i));
assert_eq!(results, vec![A(123)]);
}

Expand Down Expand Up @@ -1408,8 +1412,8 @@ mod tests {
let mut world_a = World::new();
let world_b = World::new();
let mut query = world_a.query::<&A>();
query.for_each(&world_a, |_| {});
query.for_each(&world_b, |_| {});
query.iter(&world_a).for_each(|_| {});
query.iter(&world_b).for_each(|_| {});
}

#[test]
Expand Down
Loading