use criterion::*;
use internment::ArcIntern;
const ITER: usize = 200_000;
const RANGE: usize = 20_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct NewType<T>(T);
fn bench_get_container(c: &mut Criterion) {
let mut group = c.benchmark_group("cached");
group.bench_function(BenchmarkId::new("String", "short"), |b| {
b.iter_batched(
|| {},
|_| {
let mut ans = Vec::with_capacity(RANGE);
for idx in 0..ITER {
let s = ArcIntern::<String>::new(format!("short-{}", idx % RANGE));
ans.push(s);
}
},
criterion::BatchSize::PerIteration,
);
});
group.finish();
let mut group = c.benchmark_group("uncached");
group.bench_function(BenchmarkId::new("NewType<String>", "short"), |b| {
b.iter_batched(
|| {},
|_| {
let mut ans = Vec::with_capacity(RANGE);
for idx in 0..ITER {
let s = ArcIntern::<NewType<String>>::new(NewType(format!(
"short-{}",
idx % RANGE
)));
ans.push(s);
}
},
criterion::BatchSize::PerIteration,
);
});
group.bench_function(BenchmarkId::new("usize", "short"), |b| {
b.iter_batched(
|| {},
|_| {
let mut ans = Vec::with_capacity(RANGE);
for idx in 0..ITER {
let s = ArcIntern::<usize>::new(idx % RANGE);
ans.push(s);
}
},
criterion::BatchSize::PerIteration,
);
});
group.bench_function(BenchmarkId::new("NewType<usize>", "short"), |b| {
b.iter_batched(
|| {},
|_| {
let mut ans = Vec::with_capacity(RANGE);
for idx in 0..ITER {
let s = ArcIntern::<NewType<usize>>::new(NewType(idx % RANGE));
ans.push(s);
}
},
criterion::BatchSize::PerIteration,
);
});
group.finish();
}
criterion_group!(benches, bench_get_container);
criterion_main!(benches);