add component manipulation benchmark
authoralfadur
Sun, 26 Mar 2023 02:31:04 +0300
changeset 15970 ea370bfda705
parent 15969 4a8e6f9d5133
child 15971 8035f7452b48
add component manipulation benchmark
rust/hwphysics/benches/ecs_bench.rs
--- a/rust/hwphysics/benches/ecs_bench.rs	Sat Mar 25 04:14:07 2023 +0300
+++ b/rust/hwphysics/benches/ecs_bench.rs	Sun Mar 26 02:31:04 2023 +0300
@@ -1,36 +1,84 @@
-use criterion::{black_box, criterion_group, criterion_main, Criterion};
-use hwphysics::{
-    common::GearId,
-    data::{GearDataManager}
-};
-
-#[derive(Clone, Copy, Default)]
-struct V {
-    position: u64,
-    velocity: u64
-}
-
-pub fn benchmark(c: &mut Criterion) {
-    const SIZE: usize = 4 * 1024;
-    let mut items = [V::default(); SIZE];
-
-    c.bench_function("array run", |b| b.iter(|| {
-        for item in &mut items {
-            item.velocity += black_box(item.position);
-        }
-    }));
-
-    let mut manager = GearDataManager::new();
-    manager.register::<V>();
-    for i in 1..=items.len() {
-        let gear_id = GearId::new(i as u16).unwrap();
-        manager.add(gear_id, &items[i - 1]);
-    }
-
-    c.bench_function("component run", |b| b.iter(|| {
-        manager.iter().run(|(item,): (&mut V,)| item.velocity += black_box(item.position) );
-    }));
-}
-
-criterion_group!(benches, benchmark);
-criterion_main!(benches);
\ No newline at end of file
+use criterion::{black_box, criterion_group, criterion_main, Criterion};
+use hwphysics::{common::GearId, data::GearDataManager};
+
+#[derive(Clone, Copy, Default)]
+struct P {
+    position: u64,
+}
+
+#[derive(Clone, Copy, Default)]
+struct V {
+    velocity: u64,
+}
+
+#[derive(Clone, Copy, Default)]
+struct Pv {
+    position: u64,
+    velocity: u64,
+}
+
+const SIZE: usize = 4 * 1024;
+
+pub fn array_run(c: &mut Criterion) {
+    let mut items = [Pv::default(); SIZE];
+
+    c.bench_function("array run", |b| {
+        b.iter(|| {
+            for item in &mut items {
+                item.velocity += black_box(item.position);
+            }
+        })
+    });
+}
+
+pub fn component_run(c: &mut Criterion) {
+    let mut manager = GearDataManager::new();
+    manager.register::<Pv>();
+    for i in 1..=SIZE {
+        let gear_id = GearId::new(i as u16).unwrap();
+        manager.add(gear_id, &Pv::default());
+    }
+
+    c.bench_function("component run", |b| {
+        b.iter(|| {
+            manager
+                .iter()
+                .run(|(item,): (&mut Pv,)| item.velocity += black_box(item.position));
+        })
+    });
+}
+
+pub fn component_add_remove(c: &mut Criterion) {
+    let mut manager = GearDataManager::new();
+    let mut gears1 = vec![];
+    let mut gears2 = vec![];
+
+    manager.register::<P>();
+    manager.register::<V>();
+
+    for i in 1..=SIZE {
+        let gear_id = GearId::new(i as u16).unwrap();
+        manager.add(gear_id, &P::default());
+        if i % 2 == 0 {
+            manager.add(gear_id, &V::default());
+            gears1.push(gear_id);
+        } else {
+            gears2.push(gear_id);
+        }
+    }
+
+    c.bench_function("component add/remove", |b| {
+        b.iter(|| {
+            for id in &gears2 {
+                manager.add(*id, &V::default());
+            }
+            for id in &gears1 {
+                manager.remove::<V>(*id);
+            }
+            std::mem::swap(&mut gears1, &mut gears2);
+        })
+    });
+}
+
+criterion_group!(benches, array_run, component_run, component_add_remove);
+criterion_main!(benches);