Skip to content

Commit

Permalink
feat: benchmark despawning an entity
Browse files Browse the repository at this point in the history
  • Loading branch information
BD103 committed Jun 14, 2024
1 parent f080be0 commit ea034da
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
37 changes: 36 additions & 1 deletion benches/ecs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ pub fn world_spawn(c: &mut Criterion) {
});
}

/// Benchmarks despawning an entity in a [`World`].
pub fn world_despawn(c: &mut Criterion) {
c.bench_function("world_despawn", |b| {
let mut world = World::new();

let unsafe_world_cell = world.as_unsafe_world_cell();

b.iter_batched(
|| {
// SAFETY: The `&mut World` returned by this is always dropped before the routine
// accesses the world.
let world = unsafe { unsafe_world_cell.world_mut() };

world.spawn_batch(repeat((A(0), B(0))).take(64))
},
|input| {
// SAFETY: The `&mut World` return by this is always dropped before the setup
// acceses the world.
let world = unsafe { unsafe_world_cell.world_mut() };

for entity in input {
world.despawn(entity);
}
},
BatchSize::SmallInput,
);
});
}

/// Benchmarks iterating over all matching entities within a [`World`].
pub fn world_query_iter(c: &mut Criterion) {
c.bench_function("world_query_iter", |b| {
Expand Down Expand Up @@ -58,4 +87,10 @@ pub fn world_get_entity(c: &mut Criterion) {
});
}

criterion_group!(group, world_spawn, world_query_iter, world_get_entity);
criterion_group!(
group,
world_spawn,
world_despawn,
world_query_iter,
world_get_entity
);
4 changes: 2 additions & 2 deletions benches/ecs/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use bevy::ecs::prelude::*;
///
/// This is intended to not match with [`B`]'s size, to force the ECS to deal with padding.
#[derive(Component, Clone)]
pub struct A(pub u32);
pub struct A(#[allow(dead_code)] pub u32);

/// A 16-bit wide component.
///
/// This is intended to not match with [`A`]'s size, to force the ECS to deal with padding.
#[derive(Component, Clone)]
pub struct B(pub u16);
pub struct B(#[allow(dead_code)] pub u16);

0 comments on commit ea034da

Please sign in to comment.