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

Commit 5dfc838

Browse files
committed
Add legion and shipyard packed benchmarks
1 parent acea20d commit 5dfc838

File tree

10 files changed

+416
-0
lines changed

10 files changed

+416
-0
lines changed

benches/benchmarks.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ fn bench_simple_iter(c: &mut Criterion) {
2727
let mut bench = legion::simple_iter::Benchmark::new();
2828
b.iter(move || bench.run());
2929
});
30+
group.bench_function("legion (packed)", |b| {
31+
let mut bench = legion_packed::simple_iter::Benchmark::new();
32+
b.iter(move || bench.run());
33+
});
3034
group.bench_function("legion_0.2.4", |b| {
3135
let mut bench = legion_2_4::simple_iter::Benchmark::new();
3236
b.iter(move || bench.run());
@@ -39,6 +43,10 @@ fn bench_simple_iter(c: &mut Criterion) {
3943
let mut bench = shipyard::simple_iter::Benchmark::new();
4044
b.iter(move || bench.run());
4145
});
46+
group.bench_function("shipyard (packed)", |b| {
47+
let mut bench = shipyard_packed::simple_iter::Benchmark::new();
48+
b.iter(move || bench.run());
49+
});
4250
}
4351

4452
fn bench_frag_iter_bc(c: &mut Criterion) {
@@ -67,6 +75,10 @@ fn bench_schedule(c: &mut Criterion) {
6775
let mut bench = legion::schedule::Benchmark::new();
6876
b.iter(move || bench.run());
6977
});
78+
group.bench_function("legion (packed)", |b| {
79+
let mut bench = legion_packed::schedule::Benchmark::new();
80+
b.iter(move || bench.run());
81+
});
7082
group.bench_function("legion_0.2.4", |b| {
7183
let mut bench = legion_2_4::schedule::Benchmark::new();
7284
b.iter(move || bench.run());
@@ -79,6 +91,10 @@ fn bench_schedule(c: &mut Criterion) {
7991
let mut bench = shipyard::schedule::Benchmark::new();
8092
b.iter(move || bench.run());
8193
});
94+
group.bench_function("shipyard (packed)", |b| {
95+
let mut bench = shipyard_packed::schedule::Benchmark::new();
96+
b.iter(move || bench.run());
97+
});
8298
}
8399

84100
fn bench_heavy_compute(c: &mut Criterion) {
@@ -87,6 +103,10 @@ fn bench_heavy_compute(c: &mut Criterion) {
87103
let mut bench = legion::heavy_compute::Benchmark::new();
88104
b.iter(move || bench.run());
89105
});
106+
group.bench_function("legion (packed)", |b| {
107+
let mut bench = legion_packed::heavy_compute::Benchmark::new();
108+
b.iter(move || bench.run());
109+
});
90110
group.bench_function("legion_0.2.4", |b| {
91111
let mut bench = legion_2_4::heavy_compute::Benchmark::new();
92112
b.iter(move || bench.run());
@@ -99,6 +119,10 @@ fn bench_heavy_compute(c: &mut Criterion) {
99119
let mut bench = shipyard::heavy_compute::Benchmark::new();
100120
b.iter(move || bench.run());
101121
});
122+
group.bench_function("shipyard (packed)", |b| {
123+
let mut bench = shipyard_packed::heavy_compute::Benchmark::new();
124+
b.iter(move || bench.run());
125+
});
102126
}
103127

104128
fn bench_add_remove(c: &mut Criterion) {

src/legion_packed/heavy_compute.rs

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

src/legion_packed/mod.rs

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

src/legion_packed/schedule.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use legion::*;
2+
use storage::PackOptions;
3+
4+
struct A(f32);
5+
struct B(f32);
6+
struct C(f32);
7+
struct D(f32);
8+
struct E(f32);
9+
10+
#[system(for_each)]
11+
fn ab(a: &mut A, b: &mut B) {
12+
std::mem::swap(&mut a.0, &mut b.0);
13+
}
14+
15+
#[system(for_each)]
16+
fn cd(c: &mut C, d: &mut D) {
17+
std::mem::swap(&mut c.0, &mut d.0);
18+
}
19+
20+
#[system(for_each)]
21+
fn ce(c: &mut C, e: &mut E) {
22+
std::mem::swap(&mut c.0, &mut e.0);
23+
}
24+
25+
pub struct Benchmark(World, Resources, Schedule);
26+
27+
impl Benchmark {
28+
pub fn new() -> Self {
29+
let options = WorldOptions {
30+
groups: vec![<(A, B)>::to_group(), <(C, D)>::to_group()],
31+
};
32+
33+
let mut world = World::new(options);
34+
35+
world.extend((0..10000).map(|_| (A(0.0), B(0.0))));
36+
37+
world.extend((0..10000).map(|_| (A(0.0), B(0.0), C(0.0))));
38+
39+
world.extend((0..10000).map(|_| (A(0.0), B(0.0), C(0.0), D(0.0))));
40+
41+
world.extend((0..10000).map(|_| (A(0.0), B(0.0), C(0.0), E(0.0))));
42+
43+
world.pack(PackOptions::force());
44+
45+
let schedule = Schedule::builder()
46+
.add_system(ab_system())
47+
.add_system(cd_system())
48+
.add_system(ce_system())
49+
.build();
50+
51+
Self(world, Resources::default(), schedule)
52+
}
53+
54+
pub fn run(&mut self) {
55+
self.2.execute(&mut self.0, &mut self.1);
56+
}
57+
}

src/legion_packed/simple_iter.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use cgmath::*;
2+
use legion::*;
3+
use query::Query;
4+
use storage::PackOptions;
5+
6+
#[derive(Copy, Clone)]
7+
struct Transform(Matrix4<f32>);
8+
9+
#[derive(Copy, Clone)]
10+
struct Position(Vector3<f32>);
11+
12+
#[derive(Copy, Clone)]
13+
struct Rotation(Vector3<f32>);
14+
15+
#[derive(Copy, Clone)]
16+
struct Velocity(Vector3<f32>);
17+
18+
pub struct Benchmark(World, Query<(Read<Velocity>, Write<Position>)>);
19+
20+
impl Benchmark {
21+
pub fn new() -> Self {
22+
let options = WorldOptions {
23+
groups: vec![<(Velocity, Position)>::to_group()],
24+
};
25+
26+
let mut world = World::new(options);
27+
28+
world.extend(
29+
(
30+
vec![Transform(Matrix4::from_scale(1.0)); 10000],
31+
vec![Position(Vector3::unit_x()); 10000],
32+
vec![Rotation(Vector3::unit_x()); 10000],
33+
vec![Velocity(Vector3::unit_x()); 10000],
34+
)
35+
.into_soa(),
36+
);
37+
world.pack(PackOptions::force());
38+
39+
let query = <(Read<Velocity>, Write<Position>)>::query();
40+
41+
Self(world, query)
42+
}
43+
44+
pub fn run(&mut self) {
45+
self.1.for_each_mut(&mut self.0, |(velocity, position)| {
46+
position.0 += velocity.0;
47+
});
48+
}
49+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
pub mod bevy;
44
pub mod legion;
55
pub mod legion_2_4;
6+
pub mod legion_packed;
67
pub mod shipyard;
8+
pub mod shipyard_packed;

src/shipyard_packed/heavy_compute.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use cgmath::*;
2+
use rayon::prelude::*;
3+
use shipyard::*;
4+
5+
#[derive(Copy, Clone)]
6+
struct Position(Vector3<f32>);
7+
8+
#[derive(Copy, Clone)]
9+
struct Rotation(Vector3<f32>);
10+
11+
#[derive(Copy, Clone)]
12+
struct Velocity(Vector3<f32>);
13+
14+
pub struct Benchmark(World);
15+
16+
impl Benchmark {
17+
pub fn new() -> Self {
18+
let world = World::default();
19+
20+
world.run(
21+
|mut entities: EntitiesViewMut,
22+
mut transforms: ViewMut<Matrix4<f32>>,
23+
mut positions: ViewMut<Position>,
24+
mut rotations: ViewMut<Rotation>,
25+
mut velocities: ViewMut<Velocity>| {
26+
(&mut positions, &mut transforms).tight_pack();
27+
28+
for _ in 0..1000 {
29+
entities.add_entity(
30+
(
31+
&mut transforms,
32+
&mut positions,
33+
&mut rotations,
34+
&mut velocities,
35+
),
36+
(
37+
Matrix4::<f32>::from_angle_x(Rad(1.2)),
38+
Position(Vector3::unit_x()),
39+
Rotation(Vector3::unit_x()),
40+
Velocity(Vector3::unit_x()),
41+
),
42+
);
43+
}
44+
},
45+
);
46+
47+
Self(world)
48+
}
49+
50+
pub fn run(&mut self) {
51+
self.0.run(
52+
|mut positions: ViewMut<Position>, mut transforms: ViewMut<Matrix4<f32>>| {
53+
(&mut positions, &mut transforms)
54+
.par_iter()
55+
.for_each(|(pos, mat)| {
56+
for _ in 0..100 {
57+
*mat = mat.invert().unwrap();
58+
}
59+
pos.0 = mat.transform_vector(pos.0);
60+
});
61+
},
62+
);
63+
}
64+
}

src/shipyard_packed/mod.rs

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

0 commit comments

Comments
 (0)