Skip to content
This repository was 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 all 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
18 changes: 9 additions & 9 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.9", features = ["column-serialize", "row-serialize"] }
legion = "0.4"
planck_ecs = { version = "1.1.0", features = ["parallel"] }
rayon = "1.3"
ron = "0.6"
rayon = "1.5"
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/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pub mod heavy_compute;
pub mod simple_insert;
pub mod simple_iter;
pub mod serialize_binary;
pub mod serialize_text;
pub mod serialize_text;
20 changes: 10 additions & 10 deletions src/hecs/serialize_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ impl SerializeContext for SerContext {
fn serialize_component_ids<S: SerializeTuple>(
&mut self,
archetype: &Archetype,
out: &mut S,
) -> Result<(), S::Error> {
mut out: S,
) -> Result<S::Ok, S::Error> {
if archetype.has::<Transform>() {
out.serialize_element(&ComponentId::Transform)?;
}
Expand All @@ -66,19 +66,19 @@ impl SerializeContext for SerContext {
if archetype.has::<Velocity>() {
out.serialize_element(&ComponentId::Velocity)?;
}
Ok(())
out.end()
}

fn serialize_components<S: SerializeTuple>(
&mut self,
archetype: &Archetype,
out: &mut S,
) -> Result<(), S::Error> {
try_serialize::<Transform, _>(archetype, out)?;
try_serialize::<Position, _>(archetype, out)?;
try_serialize::<Rotation, _>(archetype, out)?;
try_serialize::<Velocity, _>(archetype, out)?;
Ok(())
mut out: S,
) -> Result<S::Ok, S::Error> {
try_serialize::<Transform, _>(archetype, &mut out)?;
try_serialize::<Position, _>(archetype, &mut out)?;
try_serialize::<Rotation, _>(archetype, &mut out)?;
try_serialize::<Velocity, _>(archetype, &mut out)?;
out.end()
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/hecs/serialize_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ impl SerializeContext for SerContext {
fn serialize_entity<S: SerializeMap>(
&mut self,
entity: EntityRef<'_>,
map: &mut S,
) -> Result<(), S::Error> {
try_serialize::<Transform, _, _>(&entity, &ComponentId::Transform, map)?;
try_serialize::<Position, _, _>(&entity, &ComponentId::Position, map)?;
try_serialize::<Rotation, _, _>(&entity, &ComponentId::Rotation, map)?;
try_serialize::<Velocity, _, _>(&entity, &ComponentId::Velocity, map)?;
Ok(())
mut map: S,
) -> Result<S::Ok, S::Error> {
try_serialize::<Transform, _, _>(&entity, &ComponentId::Transform, &mut map)?;
try_serialize::<Position, _, _>(&entity, &ComponentId::Position, &mut map)?;
try_serialize::<Rotation, _, _>(&entity, &ComponentId::Rotation, &mut map)?;
try_serialize::<Velocity, _, _>(&entity, &ComponentId::Velocity, &mut map)?;
map.end()
}
}

Expand Down Expand Up @@ -104,7 +104,7 @@ impl Benchmark {
serialize(
&world,
&mut SerContext,
&mut ron::Serializer::new(&mut encoded, None, false).unwrap(),
&mut ron::Serializer::new(&mut encoded, None).unwrap(),
)
.unwrap();
deserialize(
Expand Down
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();
});
}
}
Loading