diff options
Diffstat (limited to '')
-rw-r--r-- | vendor/zerovec/benches/zeromap.rs | 111 |
1 files changed, 50 insertions, 61 deletions
diff --git a/vendor/zerovec/benches/zeromap.rs b/vendor/zerovec/benches/zeromap.rs index 5a759fd09..5f3e87b8c 100644 --- a/vendor/zerovec/benches/zeromap.rs +++ b/vendor/zerovec/benches/zeromap.rs @@ -2,7 +2,7 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). -use std::{collections::HashMap, fs}; +use std::collections::HashMap; use criterion::{black_box, criterion_group, criterion_main, Criterion}; @@ -103,52 +103,46 @@ fn generate_zerohashmap() { println!("{buf:?}"); } -#[cfg(feature = "generate")] -fn generate_test_data() { - let zeromap = build_zeromap(true); - let zeromap_bytes = postcard::to_stdvec(&zeromap).unwrap(); - fs::write("large_zeromap.postcard", &zeromap_bytes).unwrap(); - - let hashmap = build_hashmap(true); - let hashmap_bytes = postcard::to_stdvec(&hashmap).unwrap(); - fs::write("large_hashmap.postcard", &hashmap_bytes).unwrap(); - - let zerohashmap = build_zerohashmap(true); - let zerohashmap_bytes = postcard::to_stdvec(&zerohashmap).unwrap(); - fs::write("large_zerohashmap.postcard", &zerohashmap_bytes).unwrap(); -} - fn overview_bench(c: &mut Criterion) { bench_zeromap(c); bench_hashmap(c); bench_zerohashmap(c); - - #[cfg(feature = "generate")] - generate_test_data(); } fn bench_zeromap(c: &mut Criterion) { - // Uncomment the following line to re-generate the binary data. + // Uncomment the following line to re-generate the const data. // generate_hashmap(); bench_deserialize(c); + #[cfg(feature = "bench")] bench_deserialize_large(c); bench_lookup(c); + #[cfg(feature = "bench")] bench_lookup_large(c); } fn build_zeromap(large: bool) -> ZeroMap<'static, Index32Str, Index32Str> { - let mut map: ZeroMap<Index32Str, Index32Str> = ZeroMap::new(); - for (key, value) in DATA.iter() { + // TODO(#2826): This should use ZeroMap::from_iter, however that currently takes + // *minutes*, whereas this code runs in milliseconds + let mut keys = Vec::new(); + let mut values = Vec::new(); + let mut data = DATA.to_vec(); + data.sort(); + for &(key, value) in data.iter() { if large { for n in 0..8192 { - map.insert(indexify(&format!("{key}{n}")), indexify(value)); + keys.push(format!("{key}{n:04}")); + values.push(indexify(value)); } } else { - map.insert(indexify(key), indexify(value)); + keys.push(key.to_owned()); + values.push(indexify(value)); } } - map + + let keys = keys.iter().map(|s| indexify(s)).collect::<Vec<_>>(); + // keys are sorted by construction + unsafe { ZeroMap::from_parts_unchecked(VarZeroVec::from(&keys), VarZeroVec::from(&values)) } } fn bench_deserialize(c: &mut Criterion) { @@ -161,8 +155,9 @@ fn bench_deserialize(c: &mut Criterion) { }); } +#[cfg(feature = "bench")] fn bench_deserialize_large(c: &mut Criterion) { - let buf = read_large_zeromap_postcard_bytes(); + let buf = large_zeromap_postcard_bytes(); c.bench_function("zeromap/deserialize/large", |b| { b.iter(|| { let map: ZeroMap<Index32Str, Index32Str> = @@ -185,8 +180,9 @@ fn bench_lookup(c: &mut Criterion) { }); } +#[cfg(feature = "bench")] fn bench_lookup_large(c: &mut Criterion) { - let buf = read_large_zeromap_postcard_bytes(); + let buf = large_zeromap_postcard_bytes(); let map: ZeroMap<Index32Str, Index32Str> = postcard::from_bytes(&buf).unwrap(); c.bench_function("zeromap/lookup/large", |b| { b.iter(|| { @@ -199,21 +195,20 @@ fn bench_lookup_large(c: &mut Criterion) { }); } -fn read_large_zeromap_postcard_bytes() -> Vec<u8> { - let path = concat!( - env!("CARGO_MANIFEST_DIR"), - "/benches/testdata/large_zeromap.postcard" - ); - fs::read(path).unwrap() +#[cfg(feature = "bench")] +fn large_zeromap_postcard_bytes() -> Vec<u8> { + postcard::to_stdvec(&build_zeromap(true)).unwrap() } fn bench_hashmap(c: &mut Criterion) { - // Uncomment the following line to re-generate the binary data. + // Uncomment the following line to re-generate the const data. // generate_hashmap(); bench_deserialize_hashmap(c); + #[cfg(feature = "bench")] bench_deserialize_large_hashmap(c); bench_lookup_hashmap(c); + #[cfg(feature = "bench")] bench_lookup_large_hashmap(c); } @@ -241,8 +236,9 @@ fn bench_deserialize_hashmap(c: &mut Criterion) { }); } +#[cfg(feature = "bench")] fn bench_deserialize_large_hashmap(c: &mut Criterion) { - let buf = read_large_hashmap_postcard_bytes(); + let buf = large_hashmap_postcard_bytes(); c.bench_function("zeromap/deserialize/large/hashmap", |b| { b.iter(|| { let map: HashMap<String, String> = postcard::from_bytes(black_box(&buf)).unwrap(); @@ -261,8 +257,9 @@ fn bench_lookup_hashmap(c: &mut Criterion) { }); } +#[cfg(feature = "bench")] fn bench_lookup_large_hashmap(c: &mut Criterion) { - let buf = read_large_hashmap_postcard_bytes(); + let buf = large_hashmap_postcard_bytes(); let map: HashMap<String, String> = postcard::from_bytes(&buf).unwrap(); c.bench_function("zeromap/lookup/large/hashmap", |b| { b.iter(|| { @@ -272,33 +269,29 @@ fn bench_lookup_large_hashmap(c: &mut Criterion) { }); } -fn read_large_hashmap_postcard_bytes() -> Vec<u8> { - let path = concat!( - env!("CARGO_MANIFEST_DIR"), - "/benches/testdata/large_hashmap.postcard" - ); - fs::read(path).unwrap() +#[cfg(feature = "bench")] +fn large_hashmap_postcard_bytes() -> Vec<u8> { + postcard::to_stdvec(&build_hashmap(true)).unwrap() } fn bench_zerohashmap(c: &mut Criterion) { - // Uncomment the following line to re-generate the binary data. + // Uncomment the following line to re-generate the const data. // generate_zerohashmap(); bench_deserialize_zerohashmap(c); + #[cfg(feature = "bench")] bench_deserialize_large_zerohashmap(c); bench_zerohashmap_lookup(c); + #[cfg(feature = "bench")] bench_zerohashmap_lookup_large(c); } fn build_zerohashmap(large: bool) -> ZeroHashMap<'static, Index32Str, Index32Str> { - let mut kv = match large { - true => Vec::with_capacity(8192 * DATA.len()), - false => Vec::with_capacity(DATA.len()), - }; + let mut kv = Vec::new(); for (key, value) in DATA.iter() { if large { - for n in 0..8192 { + for n in 0..512 { kv.push((format!("{key}{n}"), indexify(value))); } } else { @@ -320,12 +313,12 @@ fn bench_deserialize_zerohashmap(c: &mut Criterion) { } fn bench_deserialize_large_zerohashmap(c: &mut Criterion) { - let buf = read_large_zerohashmap_postcard_bytes(); + let buf = large_zerohashmap_postcard_bytes(); c.bench_function("zerohashmap/deserialize/large", |b| { b.iter(|| { let map: ZeroHashMap<Index32Str, Index32Str> = postcard::from_bytes(black_box(&buf)).unwrap(); - assert_eq!(map.get(indexify("iu3333")).map(|x| &x.0), Some("Inuktitut")); + assert_eq!(map.get(indexify("iu333")).map(|x| &x.0), Some("Inuktitut")); }) }); } @@ -348,16 +341,15 @@ fn bench_zerohashmap_lookup(c: &mut Criterion) { }); } +#[cfg(feature = "bench")] fn bench_zerohashmap_lookup_large(c: &mut Criterion) { - let buf = read_large_zerohashmap_postcard_bytes(); + let buf = large_zerohashmap_postcard_bytes(); let zero_hashmap: ZeroHashMap<Index32Str, Index32Str> = postcard::from_bytes(&buf).unwrap(); c.bench_function("zerohashmap/lookup/large", |b| { b.iter(|| { assert_eq!( - zero_hashmap - .get(black_box(indexify("iu3333"))) - .map(|x| &x.0), + zero_hashmap.get(black_box(indexify("iu333"))).map(|x| &x.0), Some("Inuktitut") ); assert_eq!( @@ -368,12 +360,9 @@ fn bench_zerohashmap_lookup_large(c: &mut Criterion) { }); } -fn read_large_zerohashmap_postcard_bytes() -> Vec<u8> { - let path = concat!( - env!("CARGO_MANIFEST_DIR"), - "/benches/testdata/large_zerohashmap.postcard" - ); - fs::read(path).unwrap() +#[cfg(feature = "bench")] +fn large_zerohashmap_postcard_bytes() -> Vec<u8> { + postcard::to_stdvec(&build_zerohashmap(true)).unwrap() } criterion_group!(benches, overview_bench); @@ -403,5 +392,5 @@ impl<'a> ZeroMapKV<'a> for Index32Str { #[inline] fn indexify(s: &str) -> &Index32Str { - unsafe { ::core::mem::transmute(s) } + unsafe { &*(s as *const str as *const Index32Str) } } |