Debian Codegraph @

third_party/rust/zerovec/benches/vzv.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use criterion::{black_box, criterion_group, criterion_main, Criterion};
6 use rand::SeedableRng;
7 use rand_distr::{Alphanumeric, Distribution, Uniform};
8 use rand_pcg::Lcg64Xsh32;
9 use std::ops::RangeInclusive;
10
11 use zerovec::VarZeroVec;
12
13 /// Generates an array of random alphanumeric strings.
14 ///
15 /// - length = range of lengths for the strings (chosen uniformly at random)
16 /// - count = number of strings to generate
17 /// - seed = seed for the PRNG
18 ///
19 /// Returns a tuple including the vector and a u64 that can be used to seed the next PRNG.
20 fn random_alphanums(lengths: RangeInclusive<usize>, count: usize, seed: u64) -> (Vec<String>, u64) {
21 // Lcg64Xsh32 is a small, fast PRNG for reproducible benchmarks.
22 let mut rng1 = Lcg64Xsh32::seed_from_u64(seed);
23 let mut rng2 = Lcg64Xsh32::seed_from_u64(rand::Rng::gen(&mut rng1));
24 let alpha_dist = Alphanumeric;
25 let len_dist = Uniform::from(lengths);
26 let string_vec = len_dist
27 .sample_iter(&mut rng1)
28 .take(count)
29 .map(|len| {
30 (&alpha_dist)
31 .sample_iter(&mut rng2)
32 .take(len)
33 .map(char::from)
34 .collect::<String>()
35 })
36 .collect();
37 (string_vec, rand::Rng::gen(&mut rng1))
38 }
39
40 fn overview_bench(c: &mut Criterion) {
41 // Same as vzv/char_count/vzv but with different inputs
42 let seed = 42;
43 let (string_vec, _) = random_alphanums(2..=10, 100, seed);
44 let bytes: Vec<u8> = VarZeroVec::<str>::from(&string_vec).into_bytes();
45 let vzv = VarZeroVec::<str>::parse_byte_slice(black_box(bytes.as_slice())).unwrap();
46
47 c.bench_function("vzv/overview", |b| {
48 b.iter(|| {
49 black_box(&vzv)
50 .iter()
51 .fold(0, |sum, string| sum + string.chars().count())
52 });
53 });
54
55 #[cfg(feature = "bench")]
56 {
57 char_count_benches(c);
58 binary_search_benches(c);
59 vzv_precompute_bench(c);
60 }
61
62 #[cfg(all(feature = "bench", feature = "serde"))]
63 {
64 serde_benches(c);
65 }
66 }
67
68 #[cfg(feature = "bench")]
69 fn char_count_benches(c: &mut Criterion) {
70 let seed = 2021;
71 let (string_vec, _) = random_alphanums(2..=20, 100, seed);
72 let bytes: Vec<u8> = VarZeroVec::<str>::from(&string_vec).into_bytes();
73 let vzv = VarZeroVec::<str>::parse_byte_slice(black_box(bytes.as_slice())).unwrap();
74
75 // *** Count chars in vec of 100 strings ***
76 c.bench_function("vzv/char_count/slice", |b| {
77 b.iter(|| {
78 black_box(&string_vec)
79 .iter()
80 .fold(0, |sum, string| sum + string.chars().count())
81 });
82 });
83
84 // *** Count chars in vec of 100 strings ***
85 c.bench_function("vzv/char_count/vzv", |b| {
86 b.iter(|| {
87 black_box(&vzv)
88 .iter()
89 .fold(0, |sum, string| sum + string.chars().count())
90 });
91 });
92 }
93
94 #[cfg(feature = "bench")]
95 fn binary_search_benches(c: &mut Criterion) {
96 let seed = 2021;
97 let (string_vec, seed) = random_alphanums(2..=20, 500, seed);
98 let (needles, _) = random_alphanums(2..=20, 10, seed);
99 let bytes: Vec<u8> = VarZeroVec::<str>::from(&string_vec).into_bytes();
100 let vzv = VarZeroVec::<str>::parse_byte_slice(black_box(bytes.as_slice())).unwrap();
101 let single_needle = "lmnop".to_owned();
102
103 // *** Binary search vec of 500 strings 10 times ***
104 c.bench_function("vzv/binary_search/slice", |b| {
105 b.iter(|| {
106 black_box(&needles)
107 .iter()
108 .map(|needle| black_box(&string_vec).binary_search(needle))
109 .filter(|r| r.is_ok())
110 .count()
111 });
112 });
113
114 // *** Binary search vec of 500 strings 10 times ***
115 c.bench_function("vzv/binary_search/vzv", |b| {
116 b.iter(|| {
117 black_box(&needles)
118 .iter()
119 .map(|needle| black_box(&vzv).binary_search(needle))
120 .filter(|r| r.is_ok())
121 .count()
122 });
123 });
124
125 c.bench_function("vzv/binary_search/single/slice", |b| {
126 b.iter(|| black_box(&string_vec).binary_search(black_box(&single_needle)));
127 });
128
129 c.bench_function("vzv/binary_search/single/vzv", |b| {
130 b.iter(|| black_box(&vzv).binary_search(black_box(&single_needle)));
131 });
132 }
133
134 #[cfg(all(feature = "bench", feature = "serde"))]
135 fn serde_benches(c: &mut Criterion) {
136 let seed = 2021;
137 let (string_vec, _) = random_alphanums(2..=20, 100, seed);
138 let bincode_vec = bincode::serialize(&string_vec).unwrap();
139 let vzv: VarZeroVec<str> = VarZeroVec::from(&*string_vec);
140 let bincode_vzv = bincode::serialize(&vzv).unwrap();
141
142 // *** Deserialize vec of 100 strings ***
143 c.bench_function("vzv/deserialize/string/vec_owned", |b| {
144 b.iter(|| bincode::deserialize::<Vec<String>>(black_box(&bincode_vec)));
145 });
146
147 // *** Deserialize vec of 100 strings ***
148 c.bench_function("vzv/deserialize/string/vec_borrowed", |b| {
149 b.iter(|| bincode::deserialize::<Vec<&str>>(black_box(&bincode_vec)));
150 });
151
152 // *** Deserialize vec of 100 strings ***
153 c.bench_function("vzv/deserialize/string/vzv", |b| {
154 b.iter(|| bincode::deserialize::<VarZeroVec<str>>(black_box(&bincode_vzv)));
155 });
156 }
157
158 #[cfg(feature = "bench")]
159 // Testing differences between operating on slices with precomputed/non-precomputed indexing info
160 fn vzv_precompute_bench(c: &mut Criterion) {
161 let seed = 2021;
162 let (string_vec, seed) = random_alphanums(2..=20, 500, seed);
163 let (needles, _) = random_alphanums(2..=20, 10, seed);
164 let bytes: Vec<u8> = VarZeroVec::<str>::from(&string_vec).into_bytes();
165 let vzv = VarZeroVec::<str>::parse_byte_slice(black_box(bytes.as_slice())).unwrap();
166 let borrowed = vzv.as_components();
167 let slice = vzv.as_slice();
168 let single_needle = "lmnop";
169
170 c.bench_function("vzv_precompute/get/precomputed", |b| {
171 b.iter(|| black_box(&borrowed).get(100));
172 });
173
174 c.bench_function("vzv_precompute/get/slice", |b| {
175 b.iter(|| black_box(&slice).get(100));
176 });
177
178 c.bench_function("vzv_precompute/search/precomputed", |b| {
179 b.iter(|| black_box(&borrowed).binary_search(single_needle));
180 });
181
182 c.bench_function("vzv_precompute/search/slice", |b| {
183 b.iter(|| black_box(&slice).binary_search(single_needle));
184 });
185
186 c.bench_function("vzv_precompute/search_multi/precomputed", |b| {
187 b.iter(|| {
188 black_box(&needles)
189 .iter()
190 .map(|needle| black_box(&borrowed).binary_search(needle))
191 .filter(|r| r.is_ok())
192 .count()
193 });
194 });
195
196 c.bench_function("vzv_precompute/search_multi/slice", |b| {
197 b.iter(|| {
198 black_box(&needles)
199 .iter()
200 .map(|needle| black_box(&slice).binary_search(needle))
201 .filter(|r| r.is_ok())
202 .count()
203 });
204 });
205 }
206
207 criterion_group!(benches, overview_bench,);
208 criterion_main!(benches);