summaryrefslogtreecommitdiffstats
path: root/vendor/valuable/benches/structable.rs
blob: 3d00cd754e57fb0c3e394ed53b4bbd616c41de86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use valuable::*;

use criterion::{black_box, criterion_group, criterion_main, Criterion};

#[derive(Default)]
struct HelloWorld {
    one: usize,
    two: usize,
    three: usize,
    four: usize,
    five: usize,
    six: usize,
}

static FIELDS: &[NamedField<'static>] = &[
    NamedField::new("one"),
    NamedField::new("two"),
    NamedField::new("three"),
    NamedField::new("four"),
    NamedField::new("five"),
    NamedField::new("six"),
];

impl Structable for HelloWorld {
    fn definition(&self) -> StructDef<'_> {
        StructDef::new_static("HelloWorld", Fields::Named(FIELDS))
    }
}

impl Valuable for HelloWorld {
    fn as_value(&self) -> Value<'_> {
        Value::Structable(self)
    }

    fn visit(&self, v: &mut dyn Visit) {
        v.visit_named_fields(&NamedValues::new(
            FIELDS,
            &[
                Value::Usize(self.one),
                Value::Usize(self.two),
                Value::Usize(self.three),
                Value::Usize(self.four),
                Value::Usize(self.five),
                Value::Usize(self.six),
            ],
        ));
    }
}

fn criterion_benchmark(c: &mut Criterion) {
    const NUM: usize = 50;

    let hello_world = black_box(HelloWorld::default());
    let structable = &hello_world as &dyn Structable;
    let f = match structable.definition() {
        StructDef::Static {
            fields: Fields::Named(fields),
            ..
        } => &fields[5],
        _ => unreachable!(),
    };

    struct Sum(usize, &'static NamedField<'static>);

    impl Visit for Sum {
        fn visit_named_fields(&mut self, record: &NamedValues<'_>) {
            self.0 += match record.get(self.1) {
                Some(Value::Usize(v)) => v,
                _ => return,
            }
        }

        fn visit_value(&mut self, _: Value<'_>) {
            unimplemented!()
        }
    }

    c.bench_function("struct", |b| {
        b.iter(|| {
            let mut num = 0;
            for _ in 0..NUM {
                let hello_world = black_box(HelloWorld::default());
                num += hello_world.six;
            }

            black_box(num);
        })
    });

    c.bench_function("valuable", |b| {
        b.iter(|| {
            let mut v = Sum(black_box(0), f);

            for _ in 0..NUM {
                v.visit_named_fields(&NamedValues::new(
                    FIELDS,
                    &[
                        Value::Usize(0),
                        Value::Usize(0),
                        Value::Usize(0),
                        Value::Usize(0),
                        Value::Usize(0),
                        Value::Usize(0),
                    ],
                ));
                /*
                v.visit_struct(&Record::new(
                    &definition,
                    &[
                        Value::Usize(hello_world.one),
                        Value::Usize(hello_world.two),
                        Value::Usize(hello_world.three),
                        Value::Usize(hello_world.four),
                        Value::Usize(hello_world.five),
                        Value::Usize(hello_world.six),
                    ]
                ));
                */
                // hello_world.visit(&mut v);
            }

            black_box(v.0);
        })
    });
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);