summaryrefslogtreecommitdiffstats
path: root/vendor/tinystr/benches/overview.rs
blob: 4911832ec4968dcc8c8602e2938bee385a64331a (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

mod common;
use common::*;

use criterion::black_box;
use criterion::criterion_group;
use criterion::criterion_main;
use criterion::Criterion;

use tinystr::TinyAsciiStr;
use tinystr_old::TinyStr16;
use tinystr_old::TinyStr4;
use tinystr_old::TinyStr8;

fn overview(c: &mut Criterion) {
    let mut g = c.benchmark_group("overview");

    g.bench_function("construct/TinyAsciiStr", |b| {
        b.iter(|| {
            for s in STRINGS_4 {
                let _: TinyAsciiStr<4> = black_box(s).parse().unwrap();
                let _: TinyAsciiStr<8> = black_box(s).parse().unwrap();
                let _: TinyAsciiStr<16> = black_box(s).parse().unwrap();
            }
            for s in STRINGS_8 {
                let _: TinyAsciiStr<8> = black_box(s).parse().unwrap();
                let _: TinyAsciiStr<16> = black_box(s).parse().unwrap();
            }
            for s in STRINGS_16 {
                let _: TinyAsciiStr<16> = black_box(s).parse().unwrap();
            }
        });
    });

    g.bench_function("construct/TinyStr", |b| {
        b.iter(|| {
            for s in STRINGS_4 {
                let _: TinyStr4 = black_box(s).parse().unwrap();
                let _: TinyStr8 = black_box(s).parse().unwrap();
                let _: TinyStr16 = black_box(s).parse().unwrap();
            }
            for s in STRINGS_8 {
                let _: TinyStr8 = black_box(s).parse().unwrap();
                let _: TinyStr16 = black_box(s).parse().unwrap();
            }
            for s in STRINGS_16 {
                let _: TinyStr16 = black_box(s).parse().unwrap();
            }
        });
    });

    let parsed_ascii_4: Vec<TinyAsciiStr<4>> = STRINGS_4
        .iter()
        .map(|s| s.parse::<TinyAsciiStr<4>>().unwrap())
        .collect();
    let parsed_ascii_8: Vec<TinyAsciiStr<8>> = STRINGS_4
        .iter()
        .chain(STRINGS_8)
        .map(|s| s.parse::<TinyAsciiStr<8>>().unwrap())
        .collect();
    let parsed_ascii_16: Vec<TinyAsciiStr<16>> = STRINGS_4
        .iter()
        .chain(STRINGS_8)
        .chain(STRINGS_16)
        .map(|s| s.parse::<TinyAsciiStr<16>>().unwrap())
        .collect();

    let parsed_tiny_4: Vec<TinyStr4> = STRINGS_4
        .iter()
        .map(|s| s.parse::<TinyStr4>().unwrap())
        .collect();
    let parsed_tiny_8: Vec<TinyStr8> = STRINGS_4
        .iter()
        .chain(STRINGS_8)
        .map(|s| s.parse::<TinyStr8>().unwrap())
        .collect();
    let parsed_tiny_16: Vec<TinyStr16> = STRINGS_4
        .iter()
        .chain(STRINGS_8)
        .chain(STRINGS_16)
        .map(|s| s.parse::<TinyStr16>().unwrap())
        .collect();

    g.bench_function("read/TinyAsciiStr", |b| {
        b.iter(|| {
            let mut collector: usize = 0;
            for t in black_box(&parsed_ascii_4) {
                let s: &str = t;
                collector += s.bytes().map(usize::from).sum::<usize>();
            }
            for t in black_box(&parsed_ascii_8) {
                let s: &str = t;
                collector += s.bytes().map(usize::from).sum::<usize>();
            }
            for t in black_box(&parsed_ascii_16) {
                let s: &str = t;
                collector += s.bytes().map(usize::from).sum::<usize>();
            }
            collector
        });
    });

    g.bench_function("read/TinyStr", |b| {
        b.iter(|| {
            let mut collector: usize = 0;
            for t in black_box(&parsed_tiny_4) {
                let s: &str = t;
                collector += s.bytes().map(usize::from).sum::<usize>();
            }
            for t in black_box(&parsed_tiny_8) {
                let s: &str = t;
                collector += s.bytes().map(usize::from).sum::<usize>();
            }
            for t in black_box(&parsed_tiny_16) {
                let s: &str = t;
                collector += s.bytes().map(usize::from).sum::<usize>();
            }
            collector
        });
    });

    g.bench_function("compare/TinyAsciiStr", |b| {
        b.iter(|| {
            let mut collector: usize = 0;
            for ts in black_box(&parsed_ascii_4).windows(2) {
                let o = ts[0].cmp(&ts[1]);
                collector ^= o as usize;
            }
            for ts in black_box(&parsed_ascii_8).windows(2) {
                let o = ts[0].cmp(&ts[1]);
                collector ^= o as usize;
            }
            for ts in black_box(&parsed_ascii_16).windows(2) {
                let o = ts[0].cmp(&ts[1]);
                collector ^= o as usize;
            }
            collector
        });
    });

    g.bench_function("compare/TinyStr", |b| {
        b.iter(|| {
            let mut collector: usize = 0;
            for ts in black_box(&parsed_tiny_4).windows(2) {
                let o = ts[0].cmp(&ts[1]);
                collector ^= o as usize;
            }
            for ts in black_box(&parsed_tiny_8).windows(2) {
                let o = ts[0].cmp(&ts[1]);
                collector ^= o as usize;
            }
            for ts in black_box(&parsed_tiny_16).windows(2) {
                let o = ts[0].cmp(&ts[1]);
                collector ^= o as usize;
            }
            collector
        });
    });
}

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