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
7 use litemap::LiteMap;
8
9 const DATA: [(&str, &str); 16] = [
10 ("ar", "Arabic"),
11 ("bn", "Bangla"),
12 ("ccp", "Chakma"),
13 ("chr", "Cherokee"),
14 ("el", "Greek"),
15 ("en", "English"),
16 ("eo", "Esperanto"),
17 ("es", "Spanish"),
18 ("fr", "French"),
19 ("iu", "Inuktitut"),
20 ("ja", "Japanese"),
21 ("ru", "Russian"),
22 ("sr", "Serbian"),
23 ("th", "Thai"),
24 ("tr", "Turkish"),
25 ("zh", "Chinese"),
26 ];
27
28 const POSTCARD: [u8; 176] = [
29 16, 2, 97, 114, 6, 65, 114, 97, 98, 105, 99, 2, 98, 110, 6, 66, 97, 110, 103, 108, 97, 3, 99,
30 99, 112, 6, 67, 104, 97, 107, 109, 97, 3, 99, 104, 114, 8, 67, 104, 101, 114, 111, 107, 101,
31 101, 2, 101, 108, 5, 71, 114, 101, 101, 107, 2, 101, 110, 7, 69, 110, 103, 108, 105, 115, 104,
32 2, 101, 111, 9, 69, 115, 112, 101, 114, 97, 110, 116, 111, 2, 101, 115, 7, 83, 112, 97, 110,
33 105, 115, 104, 2, 102, 114, 6, 70, 114, 101, 110, 99, 104, 2, 105, 117, 9, 73, 110, 117, 107,
34 116, 105, 116, 117, 116, 2, 106, 97, 8, 74, 97, 112, 97, 110, 101, 115, 101, 2, 114, 117, 7,
35 82, 117, 115, 115, 105, 97, 110, 2, 115, 114, 7, 83, 101, 114, 98, 105, 97, 110, 2, 116, 104,
36 4, 84, 104, 97, 105, 2, 116, 114, 7, 84, 117, 114, 107, 105, 115, 104, 2, 122, 104, 7, 67, 104,
37 105, 110, 101, 115, 101,
38 ];
39
40 /// Run this function to print new data to the console.
41 #[allow(dead_code)]
generate()42 fn generate() {
43 let map = build_litemap(false);
44 let buf = postcard::to_stdvec(&map).unwrap();
45 println!("{buf:?}");
46 }
47
large_litemap_postcard_bytes() -> Vec<u8>48 fn large_litemap_postcard_bytes() -> Vec<u8> {
49 postcard::to_stdvec(&build_litemap(true)).unwrap()
50 }
51
overview_bench(c: &mut Criterion)52 fn overview_bench(c: &mut Criterion) {
53 // Uncomment the following line to re-generate the binary data.
54 // generate();
55
56 bench_deserialize(c);
57 bench_deserialize_large(c);
58 bench_lookup(c);
59 bench_lookup_large(c);
60 }
61
build_litemap(large: bool) -> LiteMap<String, String>62 fn build_litemap(large: bool) -> LiteMap<String, String> {
63 let mut map: LiteMap<String, String> = LiteMap::new();
64 for (key, value) in DATA.into_iter() {
65 if large {
66 for n in 0..8192 {
67 map.insert(format!("{key}{n}"), value.to_owned());
68 }
69 } else {
70 map.insert(key.to_owned(), value.to_owned());
71 }
72 }
73 map
74 }
75
bench_deserialize(c: &mut Criterion)76 fn bench_deserialize(c: &mut Criterion) {
77 c.bench_function("litemap/deserialize/small", |b| {
78 b.iter(|| {
79 let map: LiteMap<String, String> = postcard::from_bytes(black_box(&POSTCARD)).unwrap();
80 assert_eq!(map.get("iu"), Some(&"Inuktitut".to_owned()));
81 })
82 });
83 }
84
bench_deserialize_large(c: &mut Criterion)85 fn bench_deserialize_large(c: &mut Criterion) {
86 let buf = large_litemap_postcard_bytes();
87 c.bench_function("litemap/deseralize/large", |b| {
88 b.iter(|| {
89 let map: LiteMap<String, String> = postcard::from_bytes(black_box(&buf)).unwrap();
90 assert_eq!(map.get("iu3333"), Some(&"Inuktitut".to_owned()));
91 });
92 });
93 }
94
bench_lookup(c: &mut Criterion)95 fn bench_lookup(c: &mut Criterion) {
96 let map: LiteMap<String, String> = postcard::from_bytes(&POSTCARD).unwrap();
97 c.bench_function("litemap/lookup/small", |b| {
98 b.iter(|| {
99 assert_eq!(map.get(black_box("iu")), Some(&"Inuktitut".to_owned()));
100 assert_eq!(map.get(black_box("zz")), None);
101 });
102 });
103 }
104
bench_lookup_large(c: &mut Criterion)105 fn bench_lookup_large(c: &mut Criterion) {
106 let buf = large_litemap_postcard_bytes();
107 let map: LiteMap<String, String> = postcard::from_bytes(&buf).unwrap();
108 c.bench_function("litemap/lookup/large", |b| {
109 b.iter(|| {
110 assert_eq!(map.get(black_box("iu3333")), Some(&"Inuktitut".to_owned()));
111 assert_eq!(map.get(black_box("zz")), None);
112 });
113 });
114 }
115
116 criterion_group!(benches, overview_bench);
117 criterion_main!(benches);
118