15940
|
1 |
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
|
2 |
use hwphysics::{common::GearId, data::GearDataManager};
|
|
3 |
|
|
4 |
#[derive(Clone, Copy, Default)]
|
|
5 |
struct P {
|
|
6 |
position: u64,
|
|
7 |
}
|
|
8 |
|
|
9 |
#[derive(Clone, Copy, Default)]
|
|
10 |
struct V {
|
|
11 |
velocity: u64,
|
|
12 |
}
|
|
13 |
|
|
14 |
#[derive(Clone, Copy, Default)]
|
|
15 |
struct Pv {
|
|
16 |
position: u64,
|
|
17 |
velocity: u64,
|
|
18 |
}
|
|
19 |
|
|
20 |
const SIZE: usize = 4 * 1024;
|
|
21 |
|
|
22 |
pub fn array_run(c: &mut Criterion) {
|
|
23 |
let mut items = [Pv::default(); SIZE];
|
|
24 |
|
|
25 |
c.bench_function("array run", |b| {
|
|
26 |
b.iter(|| {
|
|
27 |
for item in &mut items {
|
|
28 |
item.velocity += black_box(item.position);
|
|
29 |
}
|
|
30 |
})
|
|
31 |
});
|
|
32 |
}
|
|
33 |
|
|
34 |
pub fn component_run(c: &mut Criterion) {
|
|
35 |
let mut manager = GearDataManager::new();
|
15943
|
36 |
|
|
37 |
manager.register::<P>();
|
|
38 |
manager.register::<V>();
|
|
39 |
|
15940
|
40 |
for i in 1..=SIZE {
|
|
41 |
let gear_id = GearId::new(i as u16).unwrap();
|
15943
|
42 |
manager.add(gear_id, &P::default());
|
|
43 |
manager.add(gear_id, &V::default());
|
15940
|
44 |
}
|
|
45 |
|
|
46 |
c.bench_function("component run", |b| {
|
|
47 |
b.iter(|| {
|
|
48 |
manager
|
|
49 |
.iter()
|
15943
|
50 |
.run(|(p, v): (&mut P, &mut V)| v.velocity += black_box(p.position));
|
15940
|
51 |
})
|
|
52 |
});
|
|
53 |
}
|
|
54 |
|
15944
|
55 |
pub fn component_multirun(c: &mut Criterion) {
|
|
56 |
for n in (16..=64).step_by(16) {
|
|
57 |
let mut manager = GearDataManager::new();
|
|
58 |
|
|
59 |
manager.register::<P>();
|
|
60 |
manager.register::<V>();
|
|
61 |
|
|
62 |
for i in 1..=(SIZE / n) {
|
|
63 |
let gear_id = GearId::new(i as u16).unwrap();
|
|
64 |
manager.add(gear_id, &P::default());
|
|
65 |
manager.add(gear_id, &V::default());
|
|
66 |
}
|
|
67 |
|
|
68 |
c.bench_function(&format!("component run {}", n), |b| {
|
|
69 |
b.iter(|| {
|
|
70 |
for i in 0..n {
|
|
71 |
manager
|
|
72 |
.iter()
|
|
73 |
.run(|(p, v): (&mut P, &mut V)| v.velocity += black_box(p.position));
|
|
74 |
}
|
|
75 |
})
|
|
76 |
});
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
15940
|
80 |
pub fn component_add_remove(c: &mut Criterion) {
|
|
81 |
let mut manager = GearDataManager::new();
|
|
82 |
let mut gears1 = vec![];
|
|
83 |
let mut gears2 = vec![];
|
|
84 |
|
|
85 |
manager.register::<P>();
|
|
86 |
manager.register::<V>();
|
|
87 |
|
|
88 |
for i in 1..=SIZE {
|
|
89 |
let gear_id = GearId::new(i as u16).unwrap();
|
|
90 |
manager.add(gear_id, &P::default());
|
|
91 |
if i % 2 == 0 {
|
|
92 |
manager.add(gear_id, &V::default());
|
|
93 |
gears1.push(gear_id);
|
|
94 |
} else {
|
|
95 |
gears2.push(gear_id);
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
|
99 |
c.bench_function("component add/remove", |b| {
|
|
100 |
b.iter(|| {
|
|
101 |
for id in &gears2 {
|
|
102 |
manager.add(*id, &V::default());
|
|
103 |
}
|
|
104 |
for id in &gears1 {
|
|
105 |
manager.remove::<V>(*id);
|
|
106 |
}
|
|
107 |
std::mem::swap(&mut gears1, &mut gears2);
|
|
108 |
})
|
|
109 |
});
|
|
110 |
}
|
|
111 |
|
15945
|
112 |
criterion_group!(
|
|
113 |
benches,
|
|
114 |
array_run,
|
|
115 |
component_run,
|
|
116 |
component_multirun,
|
|
117 |
component_add_remove
|
|
118 |
);
|
15940
|
119 |
criterion_main!(benches);
|