Skip to content

Commit 6367ad4

Browse files
authored
Add benchmarks for spawning and inserting bundles (#19762)
# Objective - Splitted off from #19491 - Add some benchmarks for spawning and inserting components. Right now these are pretty short, but it's expected that they will be extended when different kinds of dynamic bundles will be implemented.
1 parent 83afcb5 commit 6367ad4

File tree

6 files changed

+174
-0
lines changed

6 files changed

+174
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use benches::bench;
2+
use bevy_ecs::{component::Component, world::World};
3+
use criterion::Criterion;
4+
5+
const ENTITY_COUNT: usize = 2_000;
6+
7+
#[derive(Component)]
8+
struct C<const N: usize>(usize);
9+
10+
pub fn insert_many(criterion: &mut Criterion) {
11+
let mut group = criterion.benchmark_group(bench!("insert_many"));
12+
13+
group.bench_function("all", |bencher| {
14+
let mut world = World::new();
15+
bencher.iter(|| {
16+
for _ in 0..ENTITY_COUNT {
17+
world
18+
.spawn_empty()
19+
.insert(C::<0>(1))
20+
.insert(C::<1>(1))
21+
.insert(C::<2>(1))
22+
.insert(C::<3>(1))
23+
.insert(C::<4>(1))
24+
.insert(C::<5>(1))
25+
.insert(C::<6>(1))
26+
.insert(C::<7>(1))
27+
.insert(C::<8>(1))
28+
.insert(C::<9>(1))
29+
.insert(C::<10>(1))
30+
.insert(C::<11>(1))
31+
.insert(C::<12>(1))
32+
.insert(C::<13>(1))
33+
.insert(C::<14>(1));
34+
}
35+
world.clear_entities();
36+
});
37+
});
38+
39+
group.bench_function("only_last", |bencher| {
40+
let mut world = World::new();
41+
bencher.iter(|| {
42+
for _ in 0..ENTITY_COUNT {
43+
world
44+
.spawn((
45+
C::<0>(1),
46+
C::<1>(1),
47+
C::<2>(1),
48+
C::<3>(1),
49+
C::<4>(1),
50+
C::<5>(1),
51+
C::<6>(1),
52+
C::<7>(1),
53+
C::<8>(1),
54+
C::<9>(1),
55+
C::<10>(1),
56+
C::<11>(1),
57+
C::<12>(1),
58+
C::<13>(1),
59+
))
60+
.insert(C::<14>(1));
61+
}
62+
world.clear_entities();
63+
});
64+
});
65+
66+
group.finish();
67+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use criterion::criterion_group;
2+
3+
mod insert_many;
4+
mod spawn_many;
5+
mod spawn_many_zst;
6+
mod spawn_one_zst;
7+
8+
criterion_group!(
9+
benches,
10+
spawn_one_zst::spawn_one_zst,
11+
spawn_many_zst::spawn_many_zst,
12+
spawn_many::spawn_many,
13+
insert_many::insert_many,
14+
);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use benches::bench;
2+
use bevy_ecs::{component::Component, world::World};
3+
use criterion::Criterion;
4+
5+
const ENTITY_COUNT: usize = 2_000;
6+
7+
#[derive(Component)]
8+
struct C<const N: usize>(usize);
9+
10+
pub fn spawn_many(criterion: &mut Criterion) {
11+
let mut group = criterion.benchmark_group(bench!("spawn_many"));
12+
13+
group.bench_function("static", |bencher| {
14+
let mut world = World::new();
15+
bencher.iter(|| {
16+
for _ in 0..ENTITY_COUNT {
17+
world.spawn((
18+
C::<0>(1),
19+
C::<1>(1),
20+
C::<2>(1),
21+
C::<3>(1),
22+
C::<4>(1),
23+
C::<5>(1),
24+
C::<6>(1),
25+
C::<7>(1),
26+
C::<8>(1),
27+
C::<9>(1),
28+
C::<10>(1),
29+
C::<11>(1),
30+
C::<12>(1),
31+
C::<13>(1),
32+
C::<14>(1),
33+
));
34+
}
35+
world.clear_entities();
36+
});
37+
});
38+
39+
group.finish();
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use benches::bench;
2+
use bevy_ecs::{component::Component, world::World};
3+
use criterion::Criterion;
4+
5+
const ENTITY_COUNT: usize = 2_000;
6+
7+
#[derive(Component)]
8+
struct C<const N: usize>;
9+
10+
pub fn spawn_many_zst(criterion: &mut Criterion) {
11+
let mut group = criterion.benchmark_group(bench!("spawn_many_zst"));
12+
13+
group.bench_function("static", |bencher| {
14+
let mut world = World::new();
15+
bencher.iter(|| {
16+
for _ in 0..ENTITY_COUNT {
17+
world.spawn((
18+
C::<0>, C::<1>, C::<2>, C::<3>, C::<4>, C::<5>, C::<6>, C::<7>, C::<8>, C::<9>,
19+
C::<10>, C::<11>, C::<12>, C::<13>, C::<14>,
20+
));
21+
}
22+
world.clear_entities();
23+
});
24+
});
25+
26+
group.finish();
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use benches::bench;
2+
use bevy_ecs::{component::Component, world::World};
3+
use criterion::Criterion;
4+
5+
const ENTITY_COUNT: usize = 10_000;
6+
7+
#[derive(Component)]
8+
struct A;
9+
10+
pub fn spawn_one_zst(criterion: &mut Criterion) {
11+
let mut group = criterion.benchmark_group(bench!("spawn_one_zst"));
12+
13+
group.bench_function("static", |bencher| {
14+
let mut world = World::new();
15+
bencher.iter(|| {
16+
for _ in 0..ENTITY_COUNT {
17+
world.spawn(A);
18+
}
19+
world.clear_entities();
20+
});
21+
});
22+
23+
group.finish();
24+
}

benches/benches/bevy_ecs/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use criterion::criterion_main;
77

8+
mod bundles;
89
mod change_detection;
910
mod components;
1011
mod empty_archetypes;
@@ -18,6 +19,7 @@ mod scheduling;
1819
mod world;
1920

2021
criterion_main!(
22+
bundles::benches,
2123
change_detection::benches,
2224
components::benches,
2325
empty_archetypes::benches,

0 commit comments

Comments
 (0)