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

Update bevy, hecs, legion, specs and shipyard #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy_ecs = "0.5.0"
bevy_tasks = "0.5.0"
bevy_ecs = "0.8.0"
bevy_tasks = "0.8.0"
bincode = "1.3"
cgmath = { version = "0.17", features = ["serde"] }
hecs = { version = "0.5", features = ["column-serialize", "row-serialize"] }
legion = "0.3"
cgmath = { version = "0.18", features = ["serde"] }
hecs = { version = "0.7", features = ["column-serialize", "row-serialize"] }
zakarumych marked this conversation as resolved.
Show resolved Hide resolved
legion = "0.4"
planck_ecs = { version = "1.1.0", features = ["parallel"] }
rayon = "1.3"
ron = "0.6"
ron = "0.8"
serde = { version = "1.0", features = ["derive"] }
shipyard = "0.5.0"
specs = {version = "0.16.1", features = ["serde"] }
shipyard = "0.6.0"
specs = {version = "0.18", features = ["serde"] }
specs-derive = "0.4.1"

[dev-dependencies]
Expand Down
3 changes: 3 additions & 0 deletions src/bevy/add_remove.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use bevy_ecs::prelude::*;

#[derive(bevy_ecs::component::Component)]
struct A(f32);

#[derive(bevy_ecs::component::Component)]
struct B(f32);

pub struct Benchmark(World, Vec<Entity>);
Expand Down
3 changes: 3 additions & 0 deletions src/bevy/frag_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ use bevy_ecs::prelude::*;
macro_rules! create_entities {
($world:ident; $( $variants:ident ),*) => {
$(
#[derive(bevy_ecs::component::Component)]
struct $variants(f32);

$world.spawn_batch((0..20).map(|_| ($variants(0.0), Data(1.0))));
)*
};
}

#[derive(bevy_ecs::component::Component)]
struct Data(f32);

pub struct Benchmark(World);
Expand Down
21 changes: 11 additions & 10 deletions src/bevy/heavy_compute.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use bevy_ecs::prelude::*;
use bevy_tasks::TaskPool;
use cgmath::*;

#[derive(Copy, Clone)]
#[derive(Copy, Clone, bevy_ecs::component::Component)]
struct Position(Vector3<f32>);

#[derive(Copy, Clone)]
#[derive(Copy, Clone, bevy_ecs::component::Component)]
struct Rotation(Vector3<f32>);

#[derive(Copy, Clone)]
#[derive(Copy, Clone, bevy_ecs::component::Component)]
struct Velocity(Vector3<f32>);

#[derive(Copy, Clone, bevy_ecs::component::Component)]
struct Affine(Matrix4<f32>);

pub struct Benchmark(World);

impl Benchmark {
Expand All @@ -19,7 +21,7 @@ impl Benchmark {

world.spawn_batch((0..1000).map(|_| {
(
Matrix4::<f32>::from_angle_x(Rad(1.2)),
Affine(Matrix4::<f32>::from_angle_x(Rad(1.2))),
Position(Vector3::unit_x()),
Rotation(Vector3::unit_x()),
Velocity(Vector3::unit_x()),
Expand All @@ -30,15 +32,14 @@ impl Benchmark {
}

pub fn run(&mut self) {
let task_pool = TaskPool::new();
let mut query = self.0.query::<(&mut Position, &mut Matrix4<f32>)>();
let mut query = self.0.query::<(&mut Position, &mut Affine)>();

query.par_for_each_mut(&mut self.0, &task_pool, 64, |(mut pos, mut mat)| {
query.par_for_each_mut(&mut self.0, 64, |(mut pos, mut aff)| {
for _ in 0..100 {
*mat = mat.invert().unwrap();
aff.0 = aff.0.invert().unwrap();
}

pos.0 = mat.transform_vector(pos.0);
pos.0 = aff.0.transform_vector(pos.0);
});
}
}
15 changes: 12 additions & 3 deletions src/bevy/schedule.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
use bevy_ecs::{prelude::*, schedule::Schedule};

#[derive(bevy_ecs::component::Component)]
struct A(f32);

#[derive(bevy_ecs::component::Component)]
struct B(f32);

#[derive(bevy_ecs::component::Component)]
struct C(f32);

#[derive(bevy_ecs::component::Component)]
struct D(f32);

#[derive(bevy_ecs::component::Component)]
struct E(f32);

fn ab(mut query: Query<(&mut A, &mut B)>) {
Expand Down Expand Up @@ -40,9 +49,9 @@ impl Benchmark {

let mut schedule = Schedule::default();
schedule.add_stage("main", SystemStage::parallel());
schedule.add_system_to_stage("main", ab.system());
schedule.add_system_to_stage("main", cd.system());
schedule.add_system_to_stage("main", ce.system());
schedule.add_system_to_stage("main", ab);
schedule.add_system_to_stage("main", cd);
schedule.add_system_to_stage("main", ce);

Self(world, schedule)
}
Expand Down
8 changes: 4 additions & 4 deletions src/bevy/simple_insert.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use bevy_ecs::prelude::*;
use cgmath::*;

#[derive(Copy, Clone)]
#[derive(Copy, Clone, bevy_ecs::component::Component)]

Choose a reason for hiding this comment

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

For an insertion-focused test, I would suggest using Bevy's sparse set storage rather than the default table storage. Ideally there'd be benches for both, but that may be hard to display.

Copy link

@laundmo laundmo Sep 13, 2022

Choose a reason for hiding this comment

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

For using Sparse Set storage you would add a #[component(storage = "SparseSet")] below the Component derive, as it can be used per-component.

Copy link
Author

Choose a reason for hiding this comment

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

Having bench only for SparseSet based storage wouldn't be fair.
It's a tradeoff with significant iteration speed disadvantage.

Having both would be perfect.

Choose a reason for hiding this comment

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

Yep, I'm on board there. I'd love to see both storage types for iteration and insertion speed.

struct Transform(Matrix4<f32>);

#[derive(Copy, Clone)]
#[derive(Copy, Clone, bevy_ecs::component::Component)]
struct Position(Vector3<f32>);

#[derive(Copy, Clone)]
#[derive(Copy, Clone, bevy_ecs::component::Component)]
struct Rotation(Vector3<f32>);

#[derive(Copy, Clone)]
#[derive(Copy, Clone, bevy_ecs::component::Component)]
struct Velocity(Vector3<f32>);

pub struct Benchmark;
Expand Down
8 changes: 4 additions & 4 deletions src/bevy/simple_iter.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use bevy_ecs::prelude::*;
use cgmath::*;

#[derive(Copy, Clone)]
#[derive(Copy, Clone, bevy_ecs::component::Component)]
struct Transform(Matrix4<f32>);

#[derive(Copy, Clone)]
#[derive(Copy, Clone, bevy_ecs::component::Component)]
struct Position(Vector3<f32>);

#[derive(Copy, Clone)]
#[derive(Copy, Clone, bevy_ecs::component::Component)]
struct Rotation(Vector3<f32>);

#[derive(Copy, Clone)]
#[derive(Copy, Clone, bevy_ecs::component::Component)]
struct Velocity(Vector3<f32>);

pub struct Benchmark(World);
Expand Down
2 changes: 1 addition & 1 deletion src/hecs/frag_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Benchmark {
}

pub fn run(&mut self) {
for (_, mut data) in self.0.query_mut::<&mut Data>() {
for (_, mut data) in self.0.query::<&mut Data>().iter() {
zakarumych marked this conversation as resolved.
Show resolved Hide resolved
data.0 *= 2.0;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/hecs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ pub mod frag_iter;
pub mod heavy_compute;
pub mod simple_insert;
pub mod simple_iter;
pub mod serialize_binary;
pub mod serialize_text;
// pub mod serialize_binary;
zakarumych marked this conversation as resolved.
Show resolved Hide resolved
// pub mod serialize_text;
2 changes: 1 addition & 1 deletion src/hecs/simple_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Benchmark {
}

pub fn run(&mut self) {
for (_, (velocity, position)) in self.0.query_mut::<(&Velocity, &mut Position)>() {
for (_, (velocity, position)) in self.0.query::<(&Velocity, &mut Position)>().iter() {
zakarumych marked this conversation as resolved.
Show resolved Hide resolved
position.0 += velocity.0;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/legion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod add_remove;
pub mod frag_iter;
pub mod heavy_compute;
pub mod schedule;
pub mod serialize_binary;
pub mod serialize_text;
// pub mod serialize_binary;
// pub mod serialize_text;
pub mod simple_insert;
pub mod simple_iter;
7 changes: 5 additions & 2 deletions src/legion/serialize_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ impl Benchmark {

pub fn run(&mut self) {
let Self(world, registry) = self;
let serializable = &world.as_serializable(any(), &*registry);

let mut canon = legion::serialize::Canon::default();

let serializable = &world.as_serializable(any(), &*registry, &mut canon);

let encoded = bincode::serialize(serializable).unwrap();

Expand All @@ -65,7 +68,7 @@ impl Benchmark {
);

registry
.as_deserialize()
.as_deserialize(&mut canon)
.deserialize(&mut deserializer)
.unwrap();
}
Expand Down
6 changes: 4 additions & 2 deletions src/legion/serialize_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ impl Benchmark {

pub fn run(&mut self) {
let Self(world, registry) = self;
let serializable = &world.as_serializable(any(), &*registry);
let mut canon = legion::serialize::Canon::default();

let serializable = &world.as_serializable(any(), &*registry, &mut canon);

let serialized = ron::ser::to_string(serializable).unwrap();

let mut deserializer = ron::de::Deserializer::from_str(&serialized).unwrap();
registry
.as_deserialize()
.as_deserialize(&mut canon)
.deserialize(&mut deserializer)
.unwrap();
}
Expand Down
9 changes: 6 additions & 3 deletions src/shipyard/add_remove.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use shipyard::*;

#[derive(Component)]
struct A(f32);

#[derive(Component)]
struct B(f32);

pub struct Benchmark(World, Vec<EntityId>);
Expand All @@ -16,7 +19,7 @@ impl Benchmark {
entity_ids.push(entity);
}
entity_ids
}).unwrap();
});

Self(world, entities)
}
Expand All @@ -26,12 +29,12 @@ impl Benchmark {
for entity in &self.1 {
entities.add_component(*entity, &mut b, B(0.0));
}
}).unwrap();
});

self.0.run(|mut b: ViewMut<B>| {
for entity in &self.1 {
b.remove(*entity);
}
}).unwrap();
});
}
}
6 changes: 4 additions & 2 deletions src/shipyard/frag_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use shipyard::*;
macro_rules! create_entities {
($world:ident; $( $variants:ident ),*) => {
$(
#[derive(Component)]
struct $variants(f32);
$world.run(
| mut entities: EntitiesViewMut,
Expand All @@ -14,11 +15,12 @@ macro_rules! create_entities {
($variants(0.0), Data(1.0)),
);
}
}).unwrap();
});
)*
};
}

#[derive(Component)]
struct Data(f32);

pub struct Benchmark(World);
Expand All @@ -37,6 +39,6 @@ impl Benchmark {
(&mut data).iter().for_each(|mut data| {
data.0 *= 2.0;
})
}).unwrap();
});
}
}
34 changes: 19 additions & 15 deletions src/shipyard/heavy_compute.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use cgmath::*;
use cgmath::{Transform as _, *};
use rayon::prelude::*;
use shipyard::*;

#[derive(Copy, Clone)]
#[derive(Copy, Clone, Component)]
struct Position(Vector3<f32>);

#[derive(Copy, Clone)]
#[derive(Copy, Clone, Component)]
struct Rotation(Vector3<f32>);

#[derive(Copy, Clone)]
#[derive(Copy, Clone, Component)]
struct Velocity(Vector3<f32>);

#[derive(Copy, Clone, Component)]
struct Transform(Matrix4<f32>);

pub struct Benchmark(World);

impl Benchmark {
Expand All @@ -19,7 +22,7 @@ impl Benchmark {

world.run(
|mut entities: EntitiesViewMut,
mut transforms: ViewMut<Matrix4<f32>>,
mut transforms: ViewMut<Transform>,
mut positions: ViewMut<Position>,
mut rotations: ViewMut<Rotation>,
mut velocities: ViewMut<Velocity>| {
Expand All @@ -32,31 +35,32 @@ impl Benchmark {
&mut velocities,
),
(
Matrix4::<f32>::from_angle_x(Rad(1.2)),
Transform(Matrix4::<f32>::from_angle_x(Rad(1.2))),
Position(Vector3::unit_x()),
Rotation(Vector3::unit_x()),
Velocity(Vector3::unit_x()),
),
);
}
},
).unwrap();
);

Self(world)
}

pub fn run(&mut self) {
self.0.run(
|mut positions: ViewMut<Position>, mut transforms: ViewMut<Matrix4<f32>>| {
(&mut positions, &mut transforms)
.par_iter()
.for_each(|(mut pos, mut mat)| {
|mut positions: ViewMut<Position>, mut transforms: ViewMut<Transform>| {
(&mut positions, &mut transforms).par_iter().for_each(
|(mut pos, mut transform)| {
for _ in 0..100 {
*mat = mat.invert().unwrap();
transform.0 = transform.0.invert().unwrap();
}
pos.0 = mat.transform_vector(pos.0);
});

pos.0 = transform.0.transform_vector(pos.0);
},
);
},
).unwrap();
);
}
}
Loading