Skip to content
This repository was archived by the owner on Nov 27, 2022. It is now read-only.

Commit a05bfd6

Browse files
committed
Add hecs benchmarks
1 parent 5dfc838 commit a05bfd6

File tree

8 files changed

+196
-0
lines changed

8 files changed

+196
-0
lines changed

benches/benchmarks.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ fn bench_simple_insert(c: &mut Criterion) {
1515
let mut bench = bevy::simple_insert::Benchmark::new();
1616
b.iter(move || bench.run());
1717
});
18+
group.bench_function("hecs", |b| {
19+
let mut bench = hecs::simple_insert::Benchmark::new();
20+
b.iter(move || bench.run());
21+
});
1822
group.bench_function("shipyard", |b| {
1923
let mut bench = shipyard::simple_insert::Benchmark::new();
2024
b.iter(move || bench.run());
@@ -39,6 +43,10 @@ fn bench_simple_iter(c: &mut Criterion) {
3943
let mut bench = bevy::simple_iter::Benchmark::new();
4044
b.iter(move || bench.run());
4145
});
46+
group.bench_function("hecs", |b| {
47+
let mut bench = hecs::simple_iter::Benchmark::new();
48+
b.iter(move || bench.run());
49+
});
4250
group.bench_function("shipyard", |b| {
4351
let mut bench = shipyard::simple_iter::Benchmark::new();
4452
b.iter(move || bench.run());
@@ -63,6 +71,10 @@ fn bench_frag_iter_bc(c: &mut Criterion) {
6371
let mut bench = bevy::frag_iter::Benchmark::new();
6472
b.iter(move || bench.run());
6573
});
74+
group.bench_function("hecs", |b| {
75+
let mut bench = hecs::frag_iter::Benchmark::new();
76+
b.iter(move || bench.run());
77+
});
6678
group.bench_function("shipyard", |b| {
6779
let mut bench = shipyard::frag_iter::Benchmark::new();
6880
b.iter(move || bench.run());
@@ -115,6 +127,10 @@ fn bench_heavy_compute(c: &mut Criterion) {
115127
let mut bench = bevy::heavy_compute::Benchmark::new();
116128
b.iter(move || bench.run());
117129
});
130+
group.bench_function("hecs", |b| {
131+
let mut bench = hecs::heavy_compute::Benchmark::new();
132+
b.iter(move || bench.run());
133+
});
118134
group.bench_function("shipyard", |b| {
119135
let mut bench = shipyard::heavy_compute::Benchmark::new();
120136
b.iter(move || bench.run());
@@ -135,6 +151,10 @@ fn bench_add_remove(c: &mut Criterion) {
135151
let mut bench = legion_2_4::add_remove::Benchmark::new();
136152
b.iter(move || bench.run());
137153
});
154+
group.bench_function("hecs", |b| {
155+
let mut bench = hecs::add_remove::Benchmark::new();
156+
b.iter(move || bench.run());
157+
});
138158
group.bench_function("shipyard", |b| {
139159
let mut bench = shipyard::add_remove::Benchmark::new();
140160
b.iter(move || bench.run());

src/hecs/add_remove.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use hecs::*;
2+
3+
struct A(f32);
4+
struct B(f32);
5+
6+
pub struct Benchmark(World, Vec<Entity>);
7+
8+
impl Benchmark {
9+
pub fn new() -> Self {
10+
let mut world = World::default();
11+
12+
let entities = world
13+
.spawn_batch((0..10000).map(|_| (A(0.0),)))
14+
.collect::<Vec<_>>();
15+
16+
Self(world, entities)
17+
}
18+
19+
pub fn run(&mut self) {
20+
for entity in &self.1 {
21+
self.0.insert_one(*entity, B(0.0)).unwrap();
22+
}
23+
24+
for entity in &self.1 {
25+
self.0.remove_one::<B>(*entity).unwrap();
26+
}
27+
}
28+
}

src/hecs/frag_iter.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use hecs::*;
2+
3+
macro_rules! create_entities {
4+
($world:ident; $( $variants:ident ),*) => {
5+
$(
6+
struct $variants(f32);
7+
$world.spawn_batch((0..20).map(|_| ($variants(0.0), Data(1.0))));
8+
)*
9+
};
10+
}
11+
12+
struct Data(f32);
13+
14+
pub struct Benchmark(World);
15+
16+
impl Benchmark {
17+
pub fn new() -> Self {
18+
let mut world = World::default();
19+
20+
create_entities!(world; A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);
21+
22+
Self(world)
23+
}
24+
25+
pub fn run(&mut self) {
26+
for (_, mut data) in self.0.query::<&mut Data>().iter() {
27+
data.0 *= 2.0;
28+
}
29+
}
30+
}

src/hecs/heavy_compute.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use cgmath::*;
2+
use hecs::*;
3+
4+
#[derive(Copy, Clone)]
5+
struct Position(Vector3<f32>);
6+
7+
#[derive(Copy, Clone)]
8+
struct Rotation(Vector3<f32>);
9+
10+
#[derive(Copy, Clone)]
11+
struct Velocity(Vector3<f32>);
12+
13+
pub struct Benchmark(World);
14+
15+
impl Benchmark {
16+
pub fn new() -> Self {
17+
let mut world = World::default();
18+
19+
world.spawn_batch((0..1000).map(|_| {
20+
(
21+
Matrix4::<f32>::from_angle_x(Rad(1.2)),
22+
Position(Vector3::unit_x()),
23+
Rotation(Vector3::unit_x()),
24+
Velocity(Vector3::unit_x()),
25+
)
26+
}));
27+
28+
Self(world)
29+
}
30+
31+
pub fn run(&mut self) {
32+
for (_, (mut pos, mat)) in self.0.query::<(&mut Position, &mut Matrix4<f32>)>().iter() {
33+
for _ in 0..100 {
34+
*mat = mat.invert().unwrap();
35+
}
36+
37+
pos.0 = mat.transform_vector(pos.0);
38+
}
39+
}
40+
}

src/hecs/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod add_remove;
2+
pub mod frag_iter;
3+
pub mod heavy_compute;
4+
pub mod simple_insert;
5+
pub mod simple_iter;

src/hecs/simple_insert.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use cgmath::*;
2+
use hecs::*;
3+
4+
#[derive(Copy, Clone)]
5+
struct Transform(Matrix4<f32>);
6+
7+
#[derive(Copy, Clone)]
8+
struct Position(Vector3<f32>);
9+
10+
#[derive(Copy, Clone)]
11+
struct Rotation(Vector3<f32>);
12+
13+
#[derive(Copy, Clone)]
14+
struct Velocity(Vector3<f32>);
15+
16+
pub struct Benchmark;
17+
18+
impl Benchmark {
19+
pub fn new() -> Self {
20+
Self
21+
}
22+
23+
pub fn run(&mut self) {
24+
let mut world = World::new();
25+
world.spawn_batch((0..10_000).map(|_| {
26+
(
27+
Transform(Matrix4::from_scale(1.0)),
28+
Position(Vector3::unit_x()),
29+
Rotation(Vector3::unit_x()),
30+
Velocity(Vector3::unit_x()),
31+
)
32+
}));
33+
}
34+
}

src/hecs/simple_iter.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use cgmath::*;
2+
use hecs::*;
3+
4+
#[derive(Copy, Clone)]
5+
struct Transform(Matrix4<f32>);
6+
7+
#[derive(Copy, Clone)]
8+
struct Position(Vector3<f32>);
9+
10+
#[derive(Copy, Clone)]
11+
struct Rotation(Vector3<f32>);
12+
13+
#[derive(Copy, Clone)]
14+
struct Velocity(Vector3<f32>);
15+
16+
pub struct Benchmark(World);
17+
18+
impl Benchmark {
19+
pub fn new() -> Self {
20+
let mut world = World::new();
21+
world.spawn_batch((0..10_000).map(|_| {
22+
(
23+
Transform(Matrix4::from_scale(1.0)),
24+
Position(Vector3::unit_x()),
25+
Rotation(Vector3::unit_x()),
26+
Velocity(Vector3::unit_x()),
27+
)
28+
}));
29+
30+
Self(world)
31+
}
32+
33+
pub fn run(&mut self) {
34+
for (_, (velocity, position)) in self.0.query::<(&Velocity, &mut Position)>().iter() {
35+
position.0 += velocity.0;
36+
}
37+
}
38+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(clippy::new_without_default)]
22

33
pub mod bevy;
4+
pub mod hecs;
45
pub mod legion;
56
pub mod legion_2_4;
67
pub mod legion_packed;

0 commit comments

Comments
 (0)