bevy/benches/benches/bevy_ecs/world/despawn_recursive.rs
kirawulff 716fc8b54b
Fix double-despawning in despawn_world and despawn_world_recursive benchmarks (#18448)
# Objective

- Fixes #18430


## Solution

- Moves world creation into per-iteration setup for both `despawn_world`
and `despawn_world_recursive`, meaning the world's entities don't aren't
despawned multiple times
- Doesn't affect despawn APIs

## Testing

- Tested manually by running `cargo bench -p benches --bench ecs --
despawn_world`
2025-03-22 03:26:34 +00:00

45 lines
1.5 KiB
Rust

use bevy_ecs::prelude::*;
use criterion::{BatchSize, Criterion};
use glam::*;
#[derive(Component)]
struct A(Mat4);
#[derive(Component)]
struct B(Vec4);
pub fn world_despawn_recursive(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("despawn_world_recursive");
group.warm_up_time(core::time::Duration::from_millis(500));
group.measurement_time(core::time::Duration::from_secs(4));
for entity_count in (0..5).map(|i| 10_u32.pow(i)) {
group.bench_function(format!("{}_entities", entity_count), |bencher| {
bencher.iter_batched_ref(
|| {
let mut world = World::default();
let parent_ents = (0..entity_count)
.map(|_| {
world
.spawn((A(Mat4::default()), B(Vec4::default())))
.with_children(|parent| {
parent.spawn((A(Mat4::default()), B(Vec4::default())));
})
.id()
})
.collect::<Vec<_>>();
(world, parent_ents)
},
|(world, parent_ents)| {
parent_ents.iter().for_each(|e| {
world.despawn(*e);
});
},
BatchSize::SmallInput,
);
});
}
group.finish();
}