Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
65 changes: 65 additions & 0 deletions benches/benches/bevy_ecs/bundles/insert_many.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use benches::bench;
use bevy_ecs::{component::Component, world::World};
use criterion::Criterion;

const ENTITY_COUNT: usize = 2_000;

#[derive(Component)]
struct C<const N: usize>(usize);

pub fn insert_many(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group(bench!("insert_many"));

group.bench_function("all", |bencher| {
bencher.iter(|| {
let mut world = World::new();
for _ in 0..ENTITY_COUNT {
world
.spawn_empty()
.insert(C::<0>(1))
.insert(C::<1>(1))
.insert(C::<2>(1))
.insert(C::<3>(1))
.insert(C::<4>(1))
.insert(C::<5>(1))
.insert(C::<6>(1))
.insert(C::<7>(1))
.insert(C::<8>(1))
.insert(C::<9>(1))
.insert(C::<10>(1))
.insert(C::<11>(1))
.insert(C::<12>(1))
.insert(C::<13>(1))
.insert(C::<14>(1));
}
});
});

group.bench_function("only_last", |bencher| {
bencher.iter(|| {
let mut world = World::new();
for _ in 0..ENTITY_COUNT {
world
.spawn((
C::<0>(1),
C::<1>(1),
C::<2>(1),
C::<3>(1),
C::<4>(1),
C::<5>(1),
C::<6>(1),
C::<7>(1),
C::<8>(1),
C::<9>(1),
C::<10>(1),
C::<11>(1),
C::<12>(1),
C::<13>(1),
))
.insert(C::<14>(1));
}
});
});

group.finish();
}
14 changes: 14 additions & 0 deletions benches/benches/bevy_ecs/bundles/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use criterion::criterion_group;

mod insert_many;
mod spawn_many;
mod spawn_many_zst;
mod spawn_one_zst;

criterion_group!(
benches,
spawn_one_zst::spawn_one_zst,
spawn_many_zst::spawn_many_zst,
spawn_many::spawn_many,
insert_many::insert_many,
);
41 changes: 41 additions & 0 deletions benches/benches/bevy_ecs/bundles/spawn_many.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use core::hint::black_box;

use benches::bench;
use bevy_ecs::{component::Component, world::World};
use criterion::Criterion;

const ENTITY_COUNT: usize = 2_000;

#[derive(Component)]
struct C<const N: usize>(usize);

pub fn spawn_many(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group(bench!("spawn_many"));

group.bench_function("static", |bencher| {
bencher.iter(|| {
let mut world = World::new();
for _ in 0..ENTITY_COUNT {
world.spawn(black_box((
C::<0>(1),
C::<1>(1),
C::<2>(1),
C::<3>(1),
C::<4>(1),
C::<5>(1),
C::<6>(1),
C::<7>(1),
C::<8>(1),
C::<9>(1),
C::<10>(1),
C::<11>(1),
C::<12>(1),
C::<13>(1),
C::<14>(1),
)));
}
});
});

group.finish();
}
28 changes: 28 additions & 0 deletions benches/benches/bevy_ecs/bundles/spawn_many_zst.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use core::hint::black_box;

use benches::bench;
use bevy_ecs::{component::Component, world::World};
use criterion::Criterion;

const ENTITY_COUNT: usize = 2_000;

#[derive(Component)]
struct C<const N: usize>;

pub fn spawn_many_zst(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group(bench!("spawn_many_zst"));

group.bench_function("static", |bencher| {
bencher.iter(|| {
let mut world = World::new();
for _ in 0..ENTITY_COUNT {
world.spawn(black_box((
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the black_box being used here on the bundle before spawning it. You'll definitely need the blackbox somewhere, but I'm not sure here is where it should be...

I'm not sure if it would work exactly, or what precisely you're trying to bench, but putting the black box over the bundle makes rustc unsure where the bundle comes from. That makes sense for dynamic bundles, but for static bundles, I don't mind letting the compiler see that information since that's going to be more realistic. Have you tried passing a &mut World and Entity to the blackbox instead? That might be more realistic for static bundles. That said, I'm not expert, and it really does depend on what you're testing. As it is, this is kinda more benching spawning an fn() -> impl StaticBundle. So up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I put them for consistency with the dynamic ones, because for them I would expect the compiler to not know whether there is a Some or a None. In this case it doesn't really matter since all the components are ZST, but in any case I removed it.

Have you tried passing a &mut World and Entity to the blackbox instead? That might be more realistic for static bundles.

Since I moved the World outside the iter I believe this is not automatically done by criterion when calling the inner closure (since it black_boxes it, which has the effect of black_boxing all the captured variables including the World).

C::<0>, C::<1>, C::<2>, C::<3>, C::<4>, C::<5>, C::<6>, C::<7>, C::<8>, C::<9>,
C::<10>, C::<11>, C::<12>, C::<13>, C::<14>,
)));
}
});
});

group.finish();
}
25 changes: 25 additions & 0 deletions benches/benches/bevy_ecs/bundles/spawn_one_zst.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use core::hint::black_box;

use benches::bench;
use bevy_ecs::{component::Component, world::World};
use criterion::Criterion;

const ENTITY_COUNT: usize = 10_000;

#[derive(Component)]
struct A;

pub fn spawn_one_zst(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group(bench!("spawn_one_zst"));

group.bench_function("static", |bencher| {
bencher.iter(|| {
let mut world = World::new();
for _ in 0..ENTITY_COUNT {
world.spawn(black_box(A));
}
});
});

group.finish();
}
2 changes: 2 additions & 0 deletions benches/benches/bevy_ecs/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use criterion::criterion_main;

mod bundles;
mod change_detection;
mod components;
mod empty_archetypes;
Expand All @@ -18,6 +19,7 @@ mod scheduling;
mod world;

criterion_main!(
bundles::benches,
change_detection::benches,
components::benches,
empty_archetypes::benches,
Expand Down