From 3d2d61d06365d9f5672760e51ee8f8f5bafc5e3d Mon Sep 17 00:00:00 2001 From: BD103 <59022059+BD103@users.noreply.github.com> Date: Thu, 1 Feb 2024 14:23:09 -0500 Subject: [PATCH] Use batch spawn in benchmarks (#11611) # Objective - The benchmarks for `bevy_ecs`' `iter_simple` group use `for` loops instead of `World::spawn_batch`. - There's a TODO comment that says to batch spawn them. ## Solution - Replace the `for` loops with `World::spawn_batch`. --- benches/benches/bevy_ecs/iteration/iter_simple.rs | 10 +++++----- .../benches/bevy_ecs/iteration/iter_simple_foreach.rs | 10 +++++----- .../iteration/iter_simple_foreach_sparse_set.rs | 10 +++++----- .../bevy_ecs/iteration/iter_simple_foreach_wide.rs | 10 +++++----- .../iteration/iter_simple_foreach_wide_sparse_set.rs | 10 +++++----- .../bevy_ecs/iteration/iter_simple_sparse_set.rs | 10 +++++----- .../benches/bevy_ecs/iteration/iter_simple_system.rs | 10 +++++----- benches/benches/bevy_ecs/iteration/iter_simple_wide.rs | 10 +++++----- .../bevy_ecs/iteration/iter_simple_wide_sparse_set.rs | 10 +++++----- 9 files changed, 45 insertions(+), 45 deletions(-) diff --git a/benches/benches/bevy_ecs/iteration/iter_simple.rs b/benches/benches/bevy_ecs/iteration/iter_simple.rs index bf01a049d4..79af49b44d 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple.rs @@ -19,15 +19,15 @@ impl<'w> Benchmark<'w> { pub fn new() -> Self { let mut world = World::new(); - // TODO: batch this - for _ in 0..10_000 { - world.spawn(( + world.spawn_batch( + std::iter::repeat(( Transform(Mat4::from_scale(Vec3::ONE)), Position(Vec3::X), Rotation(Vec3::X), Velocity(Vec3::X), - )); - } + )) + .take(10_000), + ); let query = world.query::<(&Velocity, &mut Position)>(); Self(world, query) diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs b/benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs index 6dae375ed7..a7975f9b6e 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs @@ -19,15 +19,15 @@ impl<'w> Benchmark<'w> { pub fn new() -> Self { let mut world = World::new(); - // TODO: batch this - for _ in 0..10_000 { - world.spawn(( + world.spawn_batch( + std::iter::repeat(( Transform(Mat4::from_scale(Vec3::ONE)), Position(Vec3::X), Rotation(Vec3::X), Velocity(Vec3::X), - )); - } + )) + .take(10_000), + ); let query = world.query::<(&Velocity, &mut Position)>(); Self(world, query) diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_foreach_sparse_set.rs b/benches/benches/bevy_ecs/iteration/iter_simple_foreach_sparse_set.rs index 8a9e7baaab..3864a519b0 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_foreach_sparse_set.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_foreach_sparse_set.rs @@ -21,15 +21,15 @@ impl<'w> Benchmark<'w> { pub fn new() -> Self { let mut world = World::new(); - // TODO: batch this - for _ in 0..10_000 { - world.spawn(( + world.spawn_batch( + std::iter::repeat(( Transform(Mat4::from_scale(Vec3::ONE)), Position(Vec3::X), Rotation(Vec3::X), Velocity(Vec3::X), - )); - } + )) + .take(10_000), + ); let query = world.query::<(&Velocity, &mut Position)>(); Self(world, query) diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide.rs b/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide.rs index 7c9e779619..2847064172 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide.rs @@ -33,9 +33,8 @@ impl<'w> Benchmark<'w> { pub fn new() -> Self { let mut world = World::new(); - // TODO: batch this - for _ in 0..10_000 { - world.spawn(( + world.spawn_batch( + std::iter::repeat(( Transform(Mat4::from_scale(Vec3::ONE)), Rotation(Vec3::X), Position::<0>(Vec3::X), @@ -48,8 +47,9 @@ impl<'w> Benchmark<'w> { Velocity::<3>(Vec3::X), Position::<4>(Vec3::X), Velocity::<4>(Vec3::X), - )); - } + )) + .take(10_000), + ); let query = world.query(); Self(world, query) diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide_sparse_set.rs b/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide_sparse_set.rs index 254b27a887..c25edcd9b8 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide_sparse_set.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide_sparse_set.rs @@ -35,9 +35,8 @@ impl<'w> Benchmark<'w> { pub fn new() -> Self { let mut world = World::new(); - // TODO: batch this - for _ in 0..10_000 { - world.spawn(( + world.spawn_batch( + std::iter::repeat(( Transform(Mat4::from_scale(Vec3::ONE)), Rotation(Vec3::X), Position::<0>(Vec3::X), @@ -50,8 +49,9 @@ impl<'w> Benchmark<'w> { Velocity::<3>(Vec3::X), Position::<4>(Vec3::X), Velocity::<4>(Vec3::X), - )); - } + )) + .take(10_000), + ); let query = world.query(); Self(world, query) diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_sparse_set.rs b/benches/benches/bevy_ecs/iteration/iter_simple_sparse_set.rs index f31554ee0d..d4394b5ade 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_sparse_set.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_sparse_set.rs @@ -21,15 +21,15 @@ impl<'w> Benchmark<'w> { pub fn new() -> Self { let mut world = World::new(); - // TODO: batch this - for _ in 0..10_000 { - world.spawn(( + world.spawn_batch( + std::iter::repeat(( Transform(Mat4::from_scale(Vec3::ONE)), Position(Vec3::X), Rotation(Vec3::X), Velocity(Vec3::X), - )); - } + )) + .take(10_000), + ); let query = world.query::<(&Velocity, &mut Position)>(); Self(world, query) diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_system.rs b/benches/benches/bevy_ecs/iteration/iter_simple_system.rs index f4d6066897..e583ffc558 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_system.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_system.rs @@ -19,15 +19,15 @@ impl Benchmark { pub fn new() -> Self { let mut world = World::new(); - // TODO: batch this - for _ in 0..10_000 { - world.spawn(( + world.spawn_batch( + std::iter::repeat(( Transform(Mat4::from_scale(Vec3::ONE)), Position(Vec3::X), Rotation(Vec3::X), Velocity(Vec3::X), - )); - } + )) + .take(10_000), + ); fn query_system(mut query: Query<(&Velocity, &mut Position)>) { for (velocity, mut position) in &mut query { diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_wide.rs b/benches/benches/bevy_ecs/iteration/iter_simple_wide.rs index 8995fb843f..193ca0c7fb 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_wide.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_wide.rs @@ -33,9 +33,8 @@ impl<'w> Benchmark<'w> { pub fn new() -> Self { let mut world = World::new(); - // TODO: batch this - for _ in 0..10_000 { - world.spawn(( + world.spawn_batch( + std::iter::repeat(( Transform(Mat4::from_scale(Vec3::ONE)), Rotation(Vec3::X), Position::<0>(Vec3::X), @@ -48,8 +47,9 @@ impl<'w> Benchmark<'w> { Velocity::<3>(Vec3::X), Position::<4>(Vec3::X), Velocity::<4>(Vec3::X), - )); - } + )) + .take(10_000), + ); let query = world.query(); Self(world, query) diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_wide_sparse_set.rs b/benches/benches/bevy_ecs/iteration/iter_simple_wide_sparse_set.rs index 5235fd9368..a4cea147ac 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_wide_sparse_set.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_wide_sparse_set.rs @@ -35,9 +35,8 @@ impl<'w> Benchmark<'w> { pub fn new() -> Self { let mut world = World::new(); - // TODO: batch this - for _ in 0..10_000 { - world.spawn(( + world.spawn_batch( + std::iter::repeat(( Transform(Mat4::from_scale(Vec3::ONE)), Rotation(Vec3::X), Position::<0>(Vec3::X), @@ -50,8 +49,9 @@ impl<'w> Benchmark<'w> { Velocity::<3>(Vec3::X), Position::<4>(Vec3::X), Velocity::<4>(Vec3::X), - )); - } + )) + .take(10_000), + ); let query = world.query(); Self(world, query)