| 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 |
use rand::seq::SliceRandom; |
| 9 |
|
| 10 |
const DATA: [(&str, &str); 16] = [ |
| 11 |
("ar", "Arabic"), |
| 12 |
("bn", "Bangla"), |
| 13 |
("ccp", "Chakma"), |
| 14 |
("chr", "Cherokee"), |
| 15 |
("el", "Greek"), |
| 16 |
("en", "English"), |
| 17 |
("eo", "Esperanto"), |
| 18 |
("es", "Spanish"), |
| 19 |
("fr", "French"), |
| 20 |
("iu", "Inuktitut"), |
| 21 |
("ja", "Japanese"), |
| 22 |
("ru", "Russian"), |
| 23 |
("sr", "Serbian"), |
| 24 |
("th", "Thai"), |
| 25 |
("tr", "Turkish"), |
| 26 |
("zh", "Chinese"), |
| 27 |
]; |
| 28 |
|
| 29 |
const POSTCARD: [u8; 176] = [ |
| 30 |
16, 2, 97, 114, 6, 65, 114, 97, 98, 105, 99, 2, 98, 110, 6, 66, 97, 110, 103, 108, 97, 3, 99, |
| 31 |
99, 112, 6, 67, 104, 97, 107, 109, 97, 3, 99, 104, 114, 8, 67, 104, 101, 114, 111, 107, 101, |
| 32 |
101, 2, 101, 108, 5, 71, 114, 101, 101, 107, 2, 101, 110, 7, 69, 110, 103, 108, 105, 115, 104, |
| 33 |
2, 101, 111, 9, 69, 115, 112, 101, 114, 97, 110, 116, 111, 2, 101, 115, 7, 83, 112, 97, 110, |
| 34 |
105, 115, 104, 2, 102, 114, 6, 70, 114, 101, 110, 99, 104, 2, 105, 117, 9, 73, 110, 117, 107, |
| 35 |
116, 105, 116, 117, 116, 2, 106, 97, 8, 74, 97, 112, 97, 110, 101, 115, 101, 2, 114, 117, 7, |
| 36 |
82, 117, 115, 115, 105, 97, 110, 2, 115, 114, 7, 83, 101, 114, 98, 105, 97, 110, 2, 116, 104, |
| 37 |
4, 84, 104, 97, 105, 2, 116, 114, 7, 84, 117, 114, 107, 105, 115, 104, 2, 122, 104, 7, 67, 104, |
| 38 |
105, 110, 101, 115, 101, |
| 39 |
]; |
| 40 |
|
| 41 |
/// Run this function to print new data to the console. |
| 42 |
#[allow(dead_code)] |
| 43 |
fn generate() { |
| 44 |
let map = build_litemap(false); |
| 45 |
let buf = postcard::to_stdvec(&map).unwrap(); |
| 46 |
println!("{buf:?}"); |
| 47 |
} |
| 48 |
|
| 49 |
fn large_litemap_postcard_bytes() -> Vec<u8> { |
| 50 |
postcard::to_stdvec(&build_litemap(true)).unwrap() |
| 51 |
} |
| 52 |
|
| 53 |
fn overview_bench(c: &mut Criterion) { |
| 54 |
// Uncomment the following line to re-generate the binary data. |
| 55 |
// generate(); |
| 56 |
|
| 57 |
bench_deserialize(c); |
| 58 |
bench_deserialize_large(c); |
| 59 |
bench_lookup(c); |
| 60 |
bench_lookup_large(c); |
| 61 |
bench_from_iter(c); |
| 62 |
bench_from_iter_rand_large(c); |
| 63 |
bench_from_iter_sorted(c); |
| 64 |
bench_from_iter_large_sorted(c); |
| 65 |
bench_extend_rand(c); |
| 66 |
bench_extend_rand_dups(c); |
| 67 |
bench_extend_from_litemap_rand(c); |
| 68 |
} |
| 69 |
|
| 70 |
fn build_litemap(large: bool) -> LiteMap<String, String> { |
| 71 |
let mut map: LiteMap<String, String> = LiteMap::new(); |
| 72 |
for (key, value) in DATA.into_iter() { |
| 73 |
if large { |
| 74 |
for n in 0..8192 { |
| 75 |
map.insert(format!("{key}{n}"), value.to_owned()); |
| 76 |
} |
| 77 |
} else { |
| 78 |
map.insert(key.to_owned(), value.to_owned()); |
| 79 |
} |
| 80 |
} |
| 81 |
map |
| 82 |
} |
| 83 |
|
| 84 |
fn bench_deserialize(c: &mut Criterion) { |
| 85 |
c.bench_function("litemap/deserialize/small", |b| { |
| 86 |
b.iter(|| { |
| 87 |
let map: LiteMap<String, String> = postcard::from_bytes(black_box(&POSTCARD)).unwrap(); |
| 88 |
assert_eq!(map.get("iu"), Some(&"Inuktitut".to_owned())); |
| 89 |
}) |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
fn bench_deserialize_large(c: &mut Criterion) { |
| 94 |
let buf = large_litemap_postcard_bytes(); |
| 95 |
c.bench_function("litemap/deseralize/large", |b| { |
| 96 |
b.iter(|| { |
| 97 |
let map: LiteMap<String, String> = postcard::from_bytes(black_box(&buf)).unwrap(); |
| 98 |
assert_eq!(map.get("iu3333"), Some(&"Inuktitut".to_owned())); |
| 99 |
}); |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
fn bench_from_iter(c: &mut Criterion) { |
| 104 |
c.bench_function("litemap/from_iter_rand/small", |b| { |
| 105 |
let mut ff = build_litemap(false).into_iter().collect::<Vec<_>>(); |
| 106 |
ff[..].shuffle(&mut rand::rng()); |
| 107 |
b.iter(|| { |
| 108 |
let map: LiteMap<&String, &String> = LiteMap::from_iter(ff.iter().map(|(k, v)| (k, v))); |
| 109 |
black_box(map) |
| 110 |
}) |
| 111 |
}); |
| 112 |
} |
| 113 |
|
| 114 |
fn bench_from_iter_rand_large(c: &mut Criterion) { |
| 115 |
c.bench_function("litemap/from_iter_rand/large", |b| { |
| 116 |
let mut ff = build_litemap(true).into_iter().collect::<Vec<_>>(); |
| 117 |
ff[..].shuffle(&mut rand::rng()); |
| 118 |
b.iter(|| { |
| 119 |
let map: LiteMap<&String, &String> = LiteMap::from_iter(ff.iter().map(|(k, v)| (k, v))); |
| 120 |
black_box(map) |
| 121 |
}) |
| 122 |
}); |
| 123 |
} |
| 124 |
|
| 125 |
fn bench_from_iter_sorted(c: &mut Criterion) { |
| 126 |
c.bench_function("litemap/from_iter_sorted/small", |b| { |
| 127 |
let ff = build_litemap(false).into_iter().collect::<Vec<_>>(); |
| 128 |
b.iter(|| { |
| 129 |
let map: LiteMap<&String, &String> = LiteMap::from_iter(ff.iter().map(|(k, v)| (k, v))); |
| 130 |
black_box(map) |
| 131 |
}) |
| 132 |
}); |
| 133 |
} |
| 134 |
|
| 135 |
fn bench_from_iter_large_sorted(c: &mut Criterion) { |
| 136 |
c.bench_function("litemap/from_iter_sorted/large", |b| { |
| 137 |
let ff = build_litemap(true).into_iter().collect::<Vec<_>>(); |
| 138 |
b.iter(|| { |
| 139 |
let map: LiteMap<&String, &String> = LiteMap::from_iter(ff.iter().map(|(k, v)| (k, v))); |
| 140 |
black_box(map) |
| 141 |
}) |
| 142 |
}); |
| 143 |
} |
| 144 |
|
| 145 |
fn bench_extend_rand(c: &mut Criterion) { |
| 146 |
c.bench_function("litemap/extend_rand/large", |b| { |
| 147 |
let mut ff = build_litemap(true).into_iter().collect::<Vec<_>>(); |
| 148 |
ff[..].shuffle(&mut rand::rng()); |
| 149 |
b.iter(|| { |
| 150 |
let mut map: LiteMap<&String, &String> = LiteMap::with_capacity(0); |
| 151 |
let mut iter = ff.iter().map(|(k, v)| (k, v)); |
| 152 |
let step = ff.len().div_ceil(10); |
| 153 |
for _ in 0..10 { |
| 154 |
map.extend(iter.by_ref().take(step)); |
| 155 |
} |
| 156 |
black_box(map) |
| 157 |
}) |
| 158 |
}); |
| 159 |
} |
| 160 |
|
| 161 |
fn bench_extend_rand_dups(c: &mut Criterion) { |
| 162 |
c.bench_function("litemap/extend_rand_dups/large", |b| { |
| 163 |
let mut ff = build_litemap(true).into_iter().collect::<Vec<_>>(); |
| 164 |
ff[..].shuffle(&mut rand::rng()); |
| 165 |
b.iter(|| { |
| 166 |
let mut map: LiteMap<&String, &String> = LiteMap::with_capacity(0); |
| 167 |
for _ in 0..2 { |
| 168 |
let mut iter = ff.iter().map(|(k, v)| (k, v)); |
| 169 |
let step = ff.len().div_ceil(10); |
| 170 |
for _ in 0..10 { |
| 171 |
map.extend(iter.by_ref().take(step)); |
| 172 |
} |
| 173 |
} |
| 174 |
black_box(map) |
| 175 |
}) |
| 176 |
}); |
| 177 |
} |
| 178 |
|
| 179 |
fn bench_extend_from_litemap_rand(c: &mut Criterion) { |
| 180 |
c.bench_function("litemap/extend_from_litemap_rand/large", |b| { |
| 181 |
let mut ff = build_litemap(true).into_iter().collect::<Vec<_>>(); |
| 182 |
ff[..].shuffle(&mut rand::rng()); |
| 183 |
b.iter(|| { |
| 184 |
let mut map: LiteMap<&String, &String> = LiteMap::with_capacity(0); |
| 185 |
let mut iter = ff.iter().map(|(k, v)| (k, v)); |
| 186 |
let step = ff.len().div_ceil(10); |
| 187 |
for _ in 0..10 { |
| 188 |
let tmp: LiteMap<&String, &String> = LiteMap::from_iter(iter.by_ref().take(step)); |
| 189 |
map.extend_from_litemap(tmp); |
| 190 |
} |
| 191 |
black_box(map) |
| 192 |
}) |
| 193 |
}); |
| 194 |
} |
| 195 |
|
| 196 |
fn bench_lookup(c: &mut Criterion) { |
| 197 |
let map: LiteMap<String, String> = postcard::from_bytes(&POSTCARD).unwrap(); |
| 198 |
c.bench_function("litemap/lookup/small", |b| { |
| 199 |
b.iter(|| { |
| 200 |
assert_eq!(map.get(black_box("iu")), Some(&"Inuktitut".to_owned())); |
| 201 |
assert_eq!(map.get(black_box("zz")), None); |
| 202 |
}); |
| 203 |
}); |
| 204 |
} |
| 205 |
|
| 206 |
fn bench_lookup_large(c: &mut Criterion) { |
| 207 |
let buf = large_litemap_postcard_bytes(); |
| 208 |
let map: LiteMap<String, String> = postcard::from_bytes(&buf).unwrap(); |
| 209 |
c.bench_function("litemap/lookup/large", |b| { |
| 210 |
b.iter(|| { |
| 211 |
assert_eq!(map.get(black_box("iu3333")), Some(&"Inuktitut".to_owned())); |
| 212 |
assert_eq!(map.get(black_box("zz")), None); |
| 213 |
}); |
| 214 |
}); |
| 215 |
} |
| 216 |
|
| 217 |
criterion_group!(benches, overview_bench); |
| 218 |
criterion_main!(benches); |