|
1 | | -use legion::*; |
2 | | -use storage::PackOptions; |
3 | | - |
| 1 | +use specs::prelude::*; |
| 2 | +use specs_derive::*; |
| 3 | +#[derive(Component)] |
| 4 | +#[storage(VecStorage)] |
4 | 5 | struct A(f32); |
| 6 | +#[derive(Component)] |
| 7 | +#[storage(VecStorage)] |
5 | 8 | struct B(f32); |
| 9 | +#[derive(Component)] |
| 10 | +#[storage(VecStorage)] |
6 | 11 | struct C(f32); |
| 12 | +#[derive(Component)] |
| 13 | +#[storage(VecStorage)] |
7 | 14 | struct D(f32); |
| 15 | +#[derive(Component)] |
| 16 | +#[storage(VecStorage)] |
8 | 17 | struct E(f32); |
9 | 18 |
|
10 | | -#[system(for_each)] |
11 | | -fn ab(a: &mut A, b: &mut B) { |
12 | | - std::mem::swap(&mut a.0, &mut b.0); |
13 | | -} |
| 19 | +struct ABSystem; |
14 | 20 |
|
15 | | -#[system(for_each)] |
16 | | -fn cd(c: &mut C, d: &mut D) { |
17 | | - std::mem::swap(&mut c.0, &mut d.0); |
18 | | -} |
| 21 | +impl<'a> System<'a> for ABSystem { |
| 22 | + type SystemData = (WriteStorage<'a, A>, WriteStorage<'a, B>); |
19 | 23 |
|
20 | | -#[system(for_each)] |
21 | | -fn ce(c: &mut C, e: &mut E) { |
22 | | - std::mem::swap(&mut c.0, &mut e.0); |
| 24 | + fn run(&mut self, (mut a_store, mut b_store): Self::SystemData) { |
| 25 | + for (a, b) in (&mut a_store, &mut b_store).join() { |
| 26 | + std::mem::swap(&mut a.0, &mut b.0); |
| 27 | + } |
| 28 | + } |
23 | 29 | } |
24 | 30 |
|
25 | | -pub struct Benchmark(World, Resources, Schedule); |
| 31 | +struct CDSystem; |
26 | 32 |
|
27 | | -impl Benchmark { |
28 | | - pub fn new() -> Self { |
29 | | - let mut world = World::default(); |
| 33 | +impl<'a> System<'a> for CDSystem { |
| 34 | + type SystemData = (WriteStorage<'a, C>, WriteStorage<'a, D>); |
30 | 35 |
|
31 | | - world.extend((0..10000).map(|_| (A(0.0), B(0.0)))); |
| 36 | + fn run(&mut self, (mut c_store, mut d_store): Self::SystemData) { |
| 37 | + for (c, d) in (&mut c_store, &mut d_store).join() { |
| 38 | + std::mem::swap(&mut c.0, &mut d.0); |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | +struct CESystem; |
32 | 43 |
|
33 | | - world.extend((0..10000).map(|_| (A(0.0), B(0.0), C(0.0)))); |
| 44 | +impl<'a> System<'a> for CESystem { |
| 45 | + type SystemData = (WriteStorage<'a, C>, WriteStorage<'a, E>); |
34 | 46 |
|
35 | | - world.extend((0..10000).map(|_| (A(0.0), B(0.0), C(0.0), D(0.0)))); |
| 47 | + fn run(&mut self, (mut c_store, mut e_store): Self::SystemData) { |
| 48 | + for (c, e) in (&mut c_store, &mut e_store).join() { |
| 49 | + std::mem::swap(&mut c.0, &mut e.0); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
36 | 53 |
|
37 | | - world.extend((0..10000).map(|_| (A(0.0), B(0.0), C(0.0), E(0.0)))); |
| 54 | +pub struct Benchmark<'a>(World, Dispatcher<'a, 'a>); |
38 | 55 |
|
39 | | - world.pack(PackOptions::force()); |
| 56 | +impl Benchmark<'_> { |
| 57 | + pub fn new() -> Self { |
| 58 | + let mut world = World::new(); |
| 59 | + world.register::<A>(); |
| 60 | + world.register::<B>(); |
| 61 | + world.register::<C>(); |
| 62 | + world.register::<D>(); |
| 63 | + world.register::<E>(); |
| 64 | + (0..10000).for_each(|_| { |
| 65 | + world.create_entity().with(A(0.0)).build(); |
| 66 | + }); |
| 67 | + (0..10000).for_each(|_| { |
| 68 | + world.create_entity().with(A(0.0)).with(B(0.0)).build(); |
| 69 | + }); |
| 70 | + (0..10000).for_each(|_| { |
| 71 | + world |
| 72 | + .create_entity() |
| 73 | + .with(A(0.0)) |
| 74 | + .with(B(0.0)) |
| 75 | + .with(C(0.0)) |
| 76 | + .build(); |
| 77 | + }); |
| 78 | + (0..10000).for_each(|_| { |
| 79 | + world |
| 80 | + .create_entity() |
| 81 | + .with(A(0.0)) |
| 82 | + .with(B(0.0)) |
| 83 | + .with(C(0.0)) |
| 84 | + .with(D(0.0)) |
| 85 | + .build(); |
| 86 | + }); |
| 87 | + (0..10000).for_each(|_| { |
| 88 | + world |
| 89 | + .create_entity() |
| 90 | + .with(A(0.0)) |
| 91 | + .with(B(0.0)) |
| 92 | + .with(C(0.0)) |
| 93 | + .with(E(0.0)) |
| 94 | + .build(); |
| 95 | + }); |
40 | 96 |
|
41 | | - let schedule = Schedule::builder() |
42 | | - .add_system(ab_system()) |
43 | | - .add_system(cd_system()) |
44 | | - .add_system(ce_system()) |
| 97 | + let dispatcher = DispatcherBuilder::new() |
| 98 | + .with(ABSystem, "ab", &[]) |
| 99 | + .with(CDSystem, "cd", &[]) |
| 100 | + .with(CESystem, "ce", &[]) |
45 | 101 | .build(); |
46 | 102 |
|
47 | | - Self(world, Resources::default(), schedule) |
| 103 | + Self(world, dispatcher) |
48 | 104 | } |
49 | 105 |
|
50 | 106 | pub fn run(&mut self) { |
51 | | - self.2.execute(&mut self.0, &mut self.1); |
| 107 | + self.1.dispatch_par(&self.0) |
52 | 108 | } |
53 | 109 | } |
0 commit comments