Debian Codegraph @

third_party/rust/zerovec/benches/zeromap.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 std::collections::HashMap;
6
7 use criterion::{black_box, criterion_group, criterion_main, Criterion};
8
9 use zerovec::maps::ZeroMapKV;
10 use zerovec::vecs::{Index32, VarZeroSlice, VarZeroVec};
11 use zerovec::{ZeroHashMap, ZeroMap};
12
13 const DATA: [(&str, &str); 16] = [
14 ("ar", "Arabic"),
15 ("bn", "Bangla"),
16 ("ccp", "Chakma"),
17 ("chr", "Cherokee"),
18 ("el", "Greek"),
19 ("en", "English"),
20 ("eo", "Esperanto"),
21 ("es", "Spanish"),
22 ("fr", "French"),
23 ("iu", "Inuktitut"),
24 ("ja", "Japanese"),
25 ("ru", "Russian"),
26 ("sr", "Serbian"),
27 ("th", "Thai"),
28 ("tr", "Turkish"),
29 ("zh", "Chinese"),
30 ];
31
32 const POSTCARD: [u8; 282] = [
33 102, 16, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 7, 0, 0, 0, 10, 0, 0, 0, 12, 0, 0, 0, 14,
34 0, 0, 0, 16, 0, 0, 0, 18, 0, 0, 0, 20, 0, 0, 0, 22, 0, 0, 0, 24, 0, 0, 0, 26, 0, 0, 0, 28, 0,
35 0, 0, 30, 0, 0, 0, 32, 0, 0, 0, 97, 114, 98, 110, 99, 99, 112, 99, 104, 114, 101, 108, 101,
36 110, 101, 111, 101, 115, 102, 114, 105, 117, 106, 97, 114, 117, 115, 114, 116, 104, 116, 114,
37 122, 104, 177, 1, 16, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 12, 0, 0, 0, 18, 0, 0, 0, 26, 0, 0, 0,
38 31, 0, 0, 0, 38, 0, 0, 0, 47, 0, 0, 0, 54, 0, 0, 0, 60, 0, 0, 0, 69, 0, 0, 0, 77, 0, 0, 0, 84,
39 0, 0, 0, 91, 0, 0, 0, 95, 0, 0, 0, 102, 0, 0, 0, 65, 114, 97, 98, 105, 99, 66, 97, 110, 103,
40 108, 97, 67, 104, 97, 107, 109, 97, 67, 104, 101, 114, 111, 107, 101, 101, 71, 114, 101, 101,
41 107, 69, 110, 103, 108, 105, 115, 104, 69, 115, 112, 101, 114, 97, 110, 116, 111, 83, 112, 97,
42 110, 105, 115, 104, 70, 114, 101, 110, 99, 104, 73, 110, 117, 107, 116, 105, 116, 117, 116, 74,
43 97, 112, 97, 110, 101, 115, 101, 82, 117, 115, 115, 105, 97, 110, 83, 101, 114, 98, 105, 97,
44 110, 84, 104, 97, 105, 84, 117, 114, 107, 105, 115, 104, 67, 104, 105, 110, 101, 115, 101,
45 ];
46
47 const POSTCARD_HASHMAP: [u8; 176] = [
48 16, 2, 114, 117, 7, 82, 117, 115, 115, 105, 97, 110, 3, 99, 99, 112, 6, 67, 104, 97, 107, 109,
49 97, 3, 99, 104, 114, 8, 67, 104, 101, 114, 111, 107, 101, 101, 2, 116, 114, 7, 84, 117, 114,
50 107, 105, 115, 104, 2, 116, 104, 4, 84, 104, 97, 105, 2, 106, 97, 8, 74, 97, 112, 97, 110, 101,
51 115, 101, 2, 101, 115, 7, 83, 112, 97, 110, 105, 115, 104, 2, 101, 111, 9, 69, 115, 112, 101,
52 114, 97, 110, 116, 111, 2, 122, 104, 7, 67, 104, 105, 110, 101, 115, 101, 2, 115, 114, 7, 83,
53 101, 114, 98, 105, 97, 110, 2, 101, 110, 7, 69, 110, 103, 108, 105, 115, 104, 2, 105, 117, 9,
54 73, 110, 117, 107, 116, 105, 116, 117, 116, 2, 102, 114, 6, 70, 114, 101, 110, 99, 104, 2, 98,
55 110, 6, 66, 97, 110, 103, 108, 97, 2, 101, 108, 5, 71, 114, 101, 101, 107, 2, 97, 114, 6, 65,
56 114, 97, 98, 105, 99,
57 ];
58
59 const POSTCARD_ZEROHASHMAP: [u8; 412] = [
60 128, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
62 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
64 0, 0, 0, 102, 16, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 6, 0, 0, 0, 8, 0, 0, 0, 10, 0,
65 0, 0, 13, 0, 0, 0, 15, 0, 0, 0, 17, 0, 0, 0, 19, 0, 0, 0, 21, 0, 0, 0, 24, 0, 0, 0, 26, 0, 0,
66 0, 28, 0, 0, 0, 30, 0, 0, 0, 32, 0, 0, 0, 115, 114, 101, 111, 116, 114, 97, 114, 105, 117, 99,
67 99, 112, 102, 114, 101, 115, 106, 97, 122, 104, 99, 104, 114, 98, 110, 101, 110, 101, 108, 114,
68 117, 116, 104, 177, 1, 16, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 16, 0, 0, 0, 23, 0, 0, 0, 29, 0, 0,
69 0, 38, 0, 0, 0, 44, 0, 0, 0, 50, 0, 0, 0, 57, 0, 0, 0, 65, 0, 0, 0, 72, 0, 0, 0, 80, 0, 0, 0,
70 86, 0, 0, 0, 93, 0, 0, 0, 98, 0, 0, 0, 105, 0, 0, 0, 83, 101, 114, 98, 105, 97, 110, 69, 115,
71 112, 101, 114, 97, 110, 116, 111, 84, 117, 114, 107, 105, 115, 104, 65, 114, 97, 98, 105, 99,
72 73, 110, 117, 107, 116, 105, 116, 117, 116, 67, 104, 97, 107, 109, 97, 70, 114, 101, 110, 99,
73 104, 83, 112, 97, 110, 105, 115, 104, 74, 97, 112, 97, 110, 101, 115, 101, 67, 104, 105, 110,
74 101, 115, 101, 67, 104, 101, 114, 111, 107, 101, 101, 66, 97, 110, 103, 108, 97, 69, 110, 103,
75 108, 105, 115, 104, 71, 114, 101, 101, 107, 82, 117, 115, 115, 105, 97, 110, 84, 104, 97, 105,
76 ];
77
78 /// Run this function to print new data to the console.
79 /// Requires the optional `serde` Cargo feature.
80 #[allow(dead_code)]
81 fn generate_zeromap() {
82 let map = build_zeromap(false);
83 let buf = postcard::to_stdvec(&map).unwrap();
84 println!("{buf:?}");
85 }
86
87 /// Run this function to print new data to the console.
88 /// Requires the optional `serde` Cargo feature.
89 #[allow(dead_code)]
90 fn generate_hashmap() {
91 let map = build_hashmap(false);
92 let buf = postcard::to_stdvec(&map).unwrap();
93 println!("{buf:?}");
94 }
95
96 /// Run this function to print new data to the console.
97 /// Requires the optional `serde` Cargo feature.
98 #[allow(dead_code)]
99 fn generate_zerohashmap() {
100 let map = build_zerohashmap(false);
101 let buf = postcard::to_stdvec(&map).unwrap();
102 println!("{buf:?}");
103 }
104
105 fn overview_bench(c: &mut Criterion) {
106 bench_zeromap(c);
107 bench_hashmap(c);
108 bench_zerohashmap(c);
109 }
110
111 fn bench_zeromap(c: &mut Criterion) {
112 // Uncomment the following line to re-generate the const data.
113 // generate_hashmap();
114
115 bench_deserialize(c);
116 #[cfg(feature = "bench")]
117 bench_deserialize_large(c);
118 bench_lookup(c);
119 #[cfg(feature = "bench")]
120 bench_lookup_large(c);
121 }
122
123 fn build_zeromap(large: bool) -> ZeroMap<'static, Index32Str, Index32Str> {
124 // TODO(#2826): This should use ZeroMap::from_iter, however that currently takes
125 // *minutes*, whereas this code runs in milliseconds
126 let mut keys = Vec::new();
127 let mut values = Vec::new();
128 let mut data = DATA.to_vec();
129 data.sort();
130 for &(key, value) in data.iter() {
131 if large {
132 for n in 0..8192 {
133 keys.push(format!("{key}{n:04}"));
134 values.push(indexify(value));
135 }
136 } else {
137 keys.push(key.to_owned());
138 values.push(indexify(value));
139 }
140 }
141
142 let keys = keys.iter().map(|s| indexify(s)).collect::<Vec<_>>();
143 // keys are sorted by construction
144 unsafe { ZeroMap::from_parts_unchecked(VarZeroVec::from(&keys), VarZeroVec::from(&values)) }
145 }
146
147 fn bench_deserialize(c: &mut Criterion) {
148 c.bench_function("zeromap/deserialize/small", |b| {
149 b.iter(|| {
150 let map: ZeroMap<Index32Str, Index32Str> =
151 postcard::from_bytes(black_box(&POSTCARD)).unwrap();
152 assert_eq!(map.get(indexify("iu")).map(|x| &x.0), Some("Inuktitut"));
153 })
154 });
155 }
156
157 #[cfg(feature = "bench")]
158 fn bench_deserialize_large(c: &mut Criterion) {
159 let buf = large_zeromap_postcard_bytes();
160 c.bench_function("zeromap/deserialize/large", |b| {
161 b.iter(|| {
162 let map: ZeroMap<Index32Str, Index32Str> =
163 postcard::from_bytes(black_box(&buf)).unwrap();
164 assert_eq!(map.get(indexify("iu3333")).map(|x| &x.0), Some("Inuktitut"));
165 })
166 });
167 }
168
169 fn bench_lookup(c: &mut Criterion) {
170 let map: ZeroMap<Index32Str, Index32Str> = postcard::from_bytes(black_box(&POSTCARD)).unwrap();
171 c.bench_function("zeromap/lookup/small", |b| {
172 b.iter(|| {
173 assert_eq!(
174 map.get(black_box(indexify("iu"))).map(|x| &x.0),
175 Some("Inuktitut")
176 );
177 assert_eq!(map.get(black_box(indexify("zz"))).map(|x| &x.0), None);
178 });
179 });
180 }
181
182 #[cfg(feature = "bench")]
183 fn bench_lookup_large(c: &mut Criterion) {
184 let buf = large_zeromap_postcard_bytes();
185 let map: ZeroMap<Index32Str, Index32Str> = postcard::from_bytes(&buf).unwrap();
186 c.bench_function("zeromap/lookup/large", |b| {
187 b.iter(|| {
188 assert_eq!(
189 map.get(black_box(indexify("iu3333"))).map(|x| &x.0),
190 Some("Inuktitut")
191 );
192 assert_eq!(map.get(black_box(indexify("zz"))).map(|x| &x.0), None);
193 });
194 });
195 }
196
197 #[cfg(feature = "bench")]
198 fn large_zeromap_postcard_bytes() -> Vec<u8> {
199 postcard::to_stdvec(&build_zeromap(true)).unwrap()
200 }
201
202 fn bench_hashmap(c: &mut Criterion) {
203 // Uncomment the following line to re-generate the const data.
204 // generate_hashmap();
205
206 bench_deserialize_hashmap(c);
207 #[cfg(feature = "bench")]
208 bench_deserialize_large_hashmap(c);
209 bench_lookup_hashmap(c);
210 #[cfg(feature = "bench")]
211 bench_lookup_large_hashmap(c);
212 }
213
214 fn build_hashmap(large: bool) -> HashMap<String, String> {
215 let mut map: HashMap<String, String> = HashMap::new();
216 for &(key, value) in DATA.iter() {
217 if large {
218 for n in 0..8192 {
219 map.insert(format!("{key}{n}"), value.to_owned());
220 }
221 } else {
222 map.insert(key.to_owned(), value.to_owned());
223 }
224 }
225 map
226 }
227
228 fn bench_deserialize_hashmap(c: &mut Criterion) {
229 c.bench_function("zeromap/deserialize/small/hashmap", |b| {
230 b.iter(|| {
231 let map: HashMap<String, String> =
232 postcard::from_bytes(black_box(&POSTCARD_HASHMAP)).unwrap();
233 assert_eq!(map.get("iu"), Some(&"Inuktitut".to_owned()));
234 })
235 });
236 }
237
238 #[cfg(feature = "bench")]
239 fn bench_deserialize_large_hashmap(c: &mut Criterion) {
240 let buf = large_hashmap_postcard_bytes();
241 c.bench_function("zeromap/deserialize/large/hashmap", |b| {
242 b.iter(|| {
243 let map: HashMap<String, String> = postcard::from_bytes(black_box(&buf)).unwrap();
244 assert_eq!(map.get("iu3333"), Some(&"Inuktitut".to_owned()));
245 })
246 });
247 }
248
249 fn bench_lookup_hashmap(c: &mut Criterion) {
250 let map: HashMap<String, String> = postcard::from_bytes(black_box(&POSTCARD_HASHMAP)).unwrap();
251 c.bench_function("zeromap/lookup/small/hashmap", |b| {
252 b.iter(|| {
253 assert_eq!(map.get(black_box("iu")), Some(&"Inuktitut".to_owned()));
254 assert_eq!(map.get(black_box("zz")), None);
255 });
256 });
257 }
258
259 #[cfg(feature = "bench")]
260 fn bench_lookup_large_hashmap(c: &mut Criterion) {
261 let buf = large_hashmap_postcard_bytes();
262 let map: HashMap<String, String> = postcard::from_bytes(&buf).unwrap();
263 c.bench_function("zeromap/lookup/large/hashmap", |b| {
264 b.iter(|| {
265 assert_eq!(map.get(black_box("iu3333")), Some(&"Inuktitut".to_owned()));
266 assert_eq!(map.get(black_box("zz")), None);
267 });
268 });
269 }
270
271 #[cfg(feature = "bench")]
272 fn large_hashmap_postcard_bytes() -> Vec<u8> {
273 postcard::to_stdvec(&build_hashmap(true)).unwrap()
274 }
275
276 fn bench_zerohashmap(c: &mut Criterion) {
277 // Uncomment the following line to re-generate the const data.
278 // generate_zerohashmap();
279
280 bench_deserialize_zerohashmap(c);
281 #[cfg(feature = "bench")]
282 bench_deserialize_large_zerohashmap(c);
283 bench_zerohashmap_lookup(c);
284 #[cfg(feature = "bench")]
285 bench_zerohashmap_lookup_large(c);
286 }
287
288 fn build_zerohashmap(large: bool) -> ZeroHashMap<'static, Index32Str, Index32Str> {
289 let mut kv = Vec::new();
290
291 for (key, value) in DATA.iter() {
292 if large {
293 for n in 0..512 {
294 kv.push((format!("{key}{n}"), indexify(value)));
295 }
296 } else {
297 kv.push((key.to_string(), indexify(value)));
298 }
299 }
300
301 ZeroHashMap::from_iter(kv.iter().map(|kv| (indexify(&kv.0), kv.1)))
302 }
303
304 fn bench_deserialize_zerohashmap(c: &mut Criterion) {
305 c.bench_function("zerohashmap/deserialize/small", |b| {
306 b.iter(|| {
307 let map: ZeroHashMap<Index32Str, Index32Str> =
308 postcard::from_bytes(black_box(&POSTCARD_ZEROHASHMAP)).unwrap();
309 assert_eq!(map.get(indexify("iu")).map(|x| &x.0), Some("Inuktitut"));
310 })
311 });
312 }
313
314 fn bench_deserialize_large_zerohashmap(c: &mut Criterion) {
315 let buf = large_zerohashmap_postcard_bytes();
316 c.bench_function("zerohashmap/deserialize/large", |b| {
317 b.iter(|| {
318 let map: ZeroHashMap<Index32Str, Index32Str> =
319 postcard::from_bytes(black_box(&buf)).unwrap();
320 assert_eq!(map.get(indexify("iu333")).map(|x| &x.0), Some("Inuktitut"));
321 })
322 });
323 }
324
325 fn bench_zerohashmap_lookup(c: &mut Criterion) {
326 let zero_hashmap: ZeroHashMap<Index32Str, Index32Str> =
327 postcard::from_bytes(black_box(&POSTCARD_ZEROHASHMAP)).unwrap();
328
329 c.bench_function("zerohashmap/lookup/small", |b| {
330 b.iter(|| {
331 assert_eq!(
332 zero_hashmap.get(black_box(indexify("iu"))).map(|x| &x.0),
333 Some("Inuktitut")
334 );
335 assert_eq!(
336 zero_hashmap.get(black_box(indexify("zz"))).map(|x| &x.0),
337 None
338 );
339 });
340 });
341 }
342
343 #[cfg(feature = "bench")]
344 fn bench_zerohashmap_lookup_large(c: &mut Criterion) {
345 let buf = large_zerohashmap_postcard_bytes();
346 let zero_hashmap: ZeroHashMap<Index32Str, Index32Str> = postcard::from_bytes(&buf).unwrap();
347
348 c.bench_function("zerohashmap/lookup/large", |b| {
349 b.iter(|| {
350 assert_eq!(
351 zero_hashmap.get(black_box(indexify("iu333"))).map(|x| &x.0),
352 Some("Inuktitut")
353 );
354 assert_eq!(
355 zero_hashmap.get(black_box(indexify("zz"))).map(|x| &x.0),
356 None
357 );
358 });
359 });
360 }
361
362 #[cfg(feature = "bench")]
363 fn large_zerohashmap_postcard_bytes() -> Vec<u8> {
364 postcard::to_stdvec(&build_zerohashmap(true)).unwrap()
365 }
366
367 criterion_group!(benches, overview_bench);
368 criterion_main!(benches);
369
370 /// This type lets us use a u32-index-format VarZeroVec with the ZeroMap.
371 ///
372 /// Eventually we will have a FormatSelector type that lets us do `ZeroMap<FormatSelector<K, Index32>, V>`
373 /// (https://github.com/unicode-org/icu4x/issues/2312)
374 ///
375 /// , isn't actually important; it's just more convenient to use make_varule to get the
376 /// full suite of traits instead of `#[derive(VarULE)]`. (With `#[derive(VarULE)]` we would have to manually
377 /// define a Serialize implementation, and that would be gnarly)
378 /// https://github.com/unicode-org/icu4x/issues/2310 tracks being able to do this with derive(ULE)
379 #[zerovec::make_varule(Index32Str)]
380 #[zerovec::skip_derive(ZeroMapKV)]
381 #[derive(Eq, PartialEq, Ord, PartialOrd, serde::Serialize, serde::Deserialize)]
382 #[zerovec::derive(Serialize, Deserialize, Hash)]
383 pub(crate) struct Index32StrBorrowed<'a>(#[serde(borrow)] pub &'a str);
384
385 impl<'a> ZeroMapKV<'a> for Index32Str {
386 type Container = VarZeroVec<'a, Index32Str, Index32>;
387 type Slice = VarZeroSlice<Index32Str, Index32>;
388 type GetType = Index32Str;
389 type OwnedType = Box<Index32Str>;
390 }
391
392 #[inline]
393 fn indexify(s: &str) -> &Index32Str {
394 unsafe { &*(s as *const str as *const Index32Str) }
395 }