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