Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more BTreeMap like apis to LiteMap #6068

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions utils/litemap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ serde = { workspace = true, optional = true, features = ["alloc"]}
yoke = { workspace = true, features = ["derive"], optional = true }

[dev-dependencies]
rand = { workspace = true }
bincode = { workspace = true }
icu_benchmark_macros = { path = "../../tools/benchmark/macros" }
icu_locale_core = { path = "../../components/locale_core" }
Expand Down
101 changes: 101 additions & 0 deletions utils/litemap/benches/litemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use litemap::LiteMap;
use rand::seq::SliceRandom;

const DATA: [(&str, &str); 16] = [
("ar", "Arabic"),
Expand Down Expand Up @@ -57,6 +58,13 @@ fn overview_bench(c: &mut Criterion) {
bench_deserialize_large(c);
bench_lookup(c);
bench_lookup_large(c);
bench_from_iter(c);
bench_from_iter_rand_large(c);
bench_from_iter_sorted(c);
bench_from_iter_large_sorted(c);
bench_extend_rand(c);
bench_extend_rand_dups(c);
bench_extend_from_litemap_rand(c);
}

fn build_litemap(large: bool) -> LiteMap<String, String> {
Expand Down Expand Up @@ -92,6 +100,99 @@ fn bench_deserialize_large(c: &mut Criterion) {
});
}

fn bench_from_iter(c: &mut Criterion) {
c.bench_function("litemap/from_iter_rand/small", |b| {
let mut ff = build_litemap(false).into_iter().collect::<Vec<_>>();
ff[..].shuffle(&mut rand::thread_rng());
b.iter(|| {
let map: LiteMap<&String, &String> = LiteMap::from_iter(ff.iter().map(|(k, v)| (k, v)));
black_box(map)
})
});
}

fn bench_from_iter_rand_large(c: &mut Criterion) {
c.bench_function("litemap/from_iter_rand/large", |b| {
let mut ff = build_litemap(true).into_iter().collect::<Vec<_>>();
ff[..].shuffle(&mut rand::thread_rng());
b.iter(|| {
let map: LiteMap<&String, &String> = LiteMap::from_iter(ff.iter().map(|(k, v)| (k, v)));
black_box(map)
})
});
}

fn bench_from_iter_sorted(c: &mut Criterion) {
c.bench_function("litemap/from_iter_sorted/small", |b| {
let ff = build_litemap(false).into_iter().collect::<Vec<_>>();
b.iter(|| {
let map: LiteMap<&String, &String> = LiteMap::from_iter(ff.iter().map(|(k, v)| (k, v)));
black_box(map)
})
});
}

fn bench_from_iter_large_sorted(c: &mut Criterion) {
c.bench_function("litemap/from_iter_sorted/large", |b| {
let ff = build_litemap(true).into_iter().collect::<Vec<_>>();
b.iter(|| {
let map: LiteMap<&String, &String> = LiteMap::from_iter(ff.iter().map(|(k, v)| (k, v)));
black_box(map)
})
});
}

fn bench_extend_rand(c: &mut Criterion) {
c.bench_function("litemap/extend_rand/large", |b| {
let mut ff = build_litemap(true).into_iter().collect::<Vec<_>>();
ff[..].shuffle(&mut rand::thread_rng());
b.iter(|| {
let mut map: LiteMap<&String, &String> = LiteMap::with_capacity(0);
let mut iter = ff.iter().map(|(k, v)| (k, v));
let step = ff.len().div_ceil(10);
for _ in 0..10 {
map.extend(iter.by_ref().take(step));
}
black_box(map)
})
});
}

fn bench_extend_rand_dups(c: &mut Criterion) {
c.bench_function("litemap/extend_rand_dups/large", |b| {
let mut ff = build_litemap(true).into_iter().collect::<Vec<_>>();
ff[..].shuffle(&mut rand::thread_rng());
b.iter(|| {
let mut map: LiteMap<&String, &String> = LiteMap::with_capacity(0);
for _ in 0..2 {
let mut iter = ff.iter().map(|(k, v)| (k, v));
let step = ff.len().div_ceil(10);
for _ in 0..10 {
map.extend(iter.by_ref().take(step));
}
}
black_box(map)
})
});
}

fn bench_extend_from_litemap_rand(c: &mut Criterion) {
c.bench_function("litemap/extend_from_litemap_rand/large", |b| {
let mut ff = build_litemap(true).into_iter().collect::<Vec<_>>();
ff[..].shuffle(&mut rand::thread_rng());
b.iter(|| {
let mut map: LiteMap<&String, &String> = LiteMap::with_capacity(0);
let mut iter = ff.iter().map(|(k, v)| (k, v));
let step = ff.len().div_ceil(10);
for _ in 0..10 {
let tmp: LiteMap<&String, &String> = LiteMap::from_iter(iter.by_ref().take(step));
map.extend_from_litemap(tmp);
}
black_box(map)
})
});
}

fn bench_lookup(c: &mut Criterion) {
let map: LiteMap<String, String> = postcard::from_bytes(&POSTCARD).unwrap();
c.bench_function("litemap/lookup/small", |b| {
Expand Down
10 changes: 7 additions & 3 deletions utils/litemap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
//! `litemap` is a crate providing [`LiteMap`], a highly simplistic "flat" key-value map
//! based off of a single sorted vector.
//!
//! The goal of this crate is to provide a map that is good enough for small
//! The main goal of this crate is to provide a map that is good enough for small
//! sizes, and does not carry the binary size impact of [`HashMap`](std::collections::HashMap)
//! or [`BTreeMap`](alloc::collections::BTreeMap).
//!
//! The flat nature of [`LiteMap`] allows it to be pre-allocated and sized more finely than
//! [`std::collections::BTreeMap`] so it also has a smaller memory footprint for small collections.
//!
//! If binary size is not a concern, [`std::collections::BTreeMap`] may be a better choice
//! for your use case. It behaves very similarly to [`LiteMap`] for less than 12 elements,
//! and upgrades itself gracefully for larger inputs.
//! and upgrades itself gracefully for larger inputs. For larger inputs mutating [`LiteMap`]
//! in random or reverse order might be slower than [`std::collections::BTreeMap`].
//!
//! ## Pluggable Backends
//!
Expand Down Expand Up @@ -65,4 +69,4 @@ pub mod store;
#[cfg(any(test, feature = "testing"))]
pub mod testing;

pub use map::LiteMap;
pub use map::{Entry, LiteMap, OccupiedEntry, VacantEntry};
Loading
Loading