| 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)] |
| 42 |
fn generate() { |
| 43 |
let map = build_litemap(false); |
| 44 |
let buf = postcard::to_stdvec(&map).unwrap(); |
| 45 |
println!("{buf:?}"); |
| 46 |
} |
| 47 |
|
| 48 |
fn large_litemap_postcard_bytes() -> Vec<u8> { |
| 49 |
postcard::to_stdvec(&build_litemap(true)).unwrap() |
| 50 |
} |
| 51 |
|
| 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 |
#[cfg(feature = "generate")] |
| 62 |
generate_test_data(); |
| 63 |
} |
| 64 |
|
| 65 |
fn build_litemap(large: bool) -> LiteMap<String, String> { |
| 66 |
let mut map: LiteMap<String, String> = LiteMap::new(); |
| 67 |
for (key, value) in DATA.into_iter() { |
| 68 |
if large { |
| 69 |
for n in 0..8192 { |
| 70 |
map.insert(format!("{key}{n}"), value.to_owned()); |
| 71 |
} |
| 72 |
} else { |
| 73 |
map.insert(key.to_owned(), value.to_owned()); |
| 74 |
} |
| 75 |
} |
| 76 |
map |
| 77 |
} |
| 78 |
|
| 79 |
fn bench_deserialize(c: &mut Criterion) { |
| 80 |
c.bench_function("litemap/deserialize/small", |b| { |
| 81 |
b.iter(|| { |
| 82 |
let map: LiteMap<String, String> = postcard::from_bytes(black_box(&POSTCARD)).unwrap(); |
| 83 |
assert_eq!(map.get("iu"), Some(&"Inuktitut".to_owned())); |
| 84 |
}) |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
fn bench_deserialize_large(c: &mut Criterion) { |
| 89 |
let buf = large_litemap_postcard_bytes(); |
| 90 |
c.bench_function("litemap/deseralize/large", |b| { |
| 91 |
b.iter(|| { |
| 92 |
let map: LiteMap<String, String> = postcard::from_bytes(black_box(&buf)).unwrap(); |
| 93 |
assert_eq!(map.get("iu3333"), Some(&"Inuktitut".to_owned())); |
| 94 |
}); |
| 95 |
}); |
| 96 |
} |
| 97 |
|
| 98 |
fn bench_lookup(c: &mut Criterion) { |
| 99 |
let map: LiteMap<String, String> = postcard::from_bytes(&POSTCARD).unwrap(); |
| 100 |
c.bench_function("litemap/lookup/small", |b| { |
| 101 |
b.iter(|| { |
| 102 |
assert_eq!(map.get(black_box("iu")), Some(&"Inuktitut".to_owned())); |
| 103 |
assert_eq!(map.get(black_box("zz")), None); |
| 104 |
}); |
| 105 |
}); |
| 106 |
} |
| 107 |
|
| 108 |
fn bench_lookup_large(c: &mut Criterion) { |
| 109 |
let buf = large_litemap_postcard_bytes(); |
| 110 |
let map: LiteMap<String, String> = postcard::from_bytes(&buf).unwrap(); |
| 111 |
c.bench_function("litemap/lookup/large", |b| { |
| 112 |
b.iter(|| { |
| 113 |
assert_eq!(map.get(black_box("iu3333")), Some(&"Inuktitut".to_owned())); |
| 114 |
assert_eq!(map.get(black_box("zz")), None); |
| 115 |
}); |
| 116 |
}); |
| 117 |
} |
| 118 |
|
| 119 |
criterion_group!(benches, overview_bench); |
| 120 |
criterion_main!(benches); |