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

Add Edict ECS #27

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ serde = { version = "1.0", features = ["derive"] }
shipyard = "0.5.0"
specs = {version = "0.16.1", features = ["serde"] }
specs-derive = "0.4.1"
edict = { version = "0.0.3" }

[dev-dependencies]
criterion = "0.3"
Expand Down
16 changes: 16 additions & 0 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ fn bench_simple_insert(c: &mut Criterion) {
let mut bench = specs::simple_insert::Benchmark::new();
b.iter(move || bench.run());
});
group.bench_function("edict", |b| {
let mut bench = edict::simple_insert::Benchmark::new();
b.iter(move || bench.run());
});
}

fn bench_simple_iter(c: &mut Criterion) {
Expand Down Expand Up @@ -59,6 +63,10 @@ fn bench_simple_iter(c: &mut Criterion) {
let mut bench = specs::simple_iter::Benchmark::new();
b.iter(move || bench.run());
});
group.bench_function("edict", |b| {
let mut bench = edict::simple_iter::Benchmark::new();
b.iter(move || bench.run());
});
}

fn bench_frag_iter_bc(c: &mut Criterion) {
Expand Down Expand Up @@ -87,6 +95,10 @@ fn bench_frag_iter_bc(c: &mut Criterion) {
let mut bench = specs::frag_iter::Benchmark::new();
b.iter(move || bench.run());
});
group.bench_function("edict", |b| {
let mut bench = edict::frag_iter::Benchmark::new();
b.iter(move || bench.run());
});
}

fn bench_schedule(c: &mut Criterion) {
Expand Down Expand Up @@ -171,6 +183,10 @@ fn bench_add_remove(c: &mut Criterion) {
let mut bench = bevy::add_remove::Benchmark::new();
b.iter(move || bench.run());
});
group.bench_function("edict", |b| {
let mut bench = edict::add_remove::Benchmark::new();
b.iter(move || bench.run());
});
}

fn bench_serialize_text(c: &mut Criterion) {
Expand Down
26 changes: 26 additions & 0 deletions src/edict/add_remove.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use edict::prelude::*;

struct A(f32);
struct B(f32);

pub struct Benchmark(World, Vec<Entity>);

impl Benchmark {
pub fn new() -> Self {
let mut world = World::new();

let entities = (0..10_000).map(|_| world.spawn((A(0.0),))).collect();

Self(world, entities)
}

pub fn run(&mut self) {
for entity in &self.1 {
self.0.insert(entity, B(0.0));
}

for entity in &self.1 {
let _ = self.0.remove::<B>(entity);
}
}
}
29 changes: 29 additions & 0 deletions src/edict/frag_iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use edict::prelude::*;

macro_rules! create_entities {
($world:ident; $entities:ident; $( $variants:ident ),*) => {
$(
struct $variants(f32);
$entities.extend($world.spawn_batch((0..20).map(|_| ($variants(0.0), Data(1.0)))));
)*
};
}

struct Data(f32);

pub struct Benchmark(World, Vec<Entity>);

impl Benchmark {
pub fn new() -> Self {
let mut world = World::default();
let mut entities = Vec::new();

create_entities!(world; entities; 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);

Self(world, entities)
}

pub fn run(&mut self) {
self.0.for_each_mut::<&mut Data, _>(|data| data.0 *= 2.0);
}
}
4 changes: 4 additions & 0 deletions src/edict/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod add_remove;
pub mod frag_iter;
pub mod simple_insert;
pub mod simple_iter;
39 changes: 39 additions & 0 deletions src/edict/simple_insert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use cgmath::*;
use edict::prelude::*;

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

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

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

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

pub struct Benchmark {
entities: Vec<Entity>,
}

impl Benchmark {
pub fn new() -> Self {
Benchmark {
entities: Vec::new(),
}
}

pub fn run(&mut self) {
let mut world = World::new();

self.entities.extend(world.spawn_batch((0..10_000).map(|_| {
(
Transform(Matrix4::from_scale(1.0)),
Position(Vector3::unit_x()),
Rotation(Vector3::unit_x()),
Velocity(Vector3::unit_x()),
)
})));
}
}
41 changes: 41 additions & 0 deletions src/edict/simple_iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use cgmath::*;
use edict::prelude::*;

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

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

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

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

pub struct Benchmark(World, Vec<Entity>);

impl Benchmark {
pub fn new() -> Self {
let mut world = World::new();
let entities = world
.spawn_batch((0..10_000).map(|_| {
(
Transform(Matrix4::from_scale(1.0)),
Position(Vector3::unit_x()),
Rotation(Vector3::unit_x()),
Velocity(Vector3::unit_x()),
)
}))
.collect();

Self(world, entities)
}

pub fn run(&mut self) {
self.0
.for_each_mut::<(&Velocity, &mut Position), _>(|(velocity, position)| {
position.0 += velocity.0;
})
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub mod legion_packed;
pub mod planck_ecs;
pub mod shipyard;
pub mod specs;
pub mod edict;