Skip to content

Commit

Permalink
perf: add inlining for range/iter and benchmark (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
arriqaaq authored Oct 9, 2024
1 parent 853e0c5 commit fe149e6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
60 changes: 59 additions & 1 deletion benches/vart_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,56 @@ pub fn seq_get(c: &mut Criterion) {
group.finish();
}

pub fn iter_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("iter_benchmark");

group.throughput(Throughput::Elements(1));
{
let size = 1_000_000;
let mut tree = Tree::<FixedSizeKey<16>, _>::new();
for i in 0..size as u64 {
tree.insert(&i.into(), i, 0, 0).unwrap();
}
group.bench_with_input(BenchmarkId::new("art", size), &size, |b, _size| {
b.iter(|| {
let count = criterion::black_box(tree.iter()).count();
assert_eq!(
count, size as usize,
"Not all items are present in the tree"
);
})
});
}

group.finish();
}

pub fn range_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("range_benchmark");

group.throughput(Throughput::Elements(1));
{
let size = 1_000_000;
let mut tree = Tree::<FixedSizeKey<16>, _>::new();
for i in 0..size as u64 {
tree.insert(&i.into(), i, 0, 0).unwrap();
}
group.bench_with_input(BenchmarkId::new("art", size), &size, |b, _size| {
let start_key: FixedSizeKey<16> = 0u16.into();
let end_key: FixedSizeKey<16> = ((size - 1) as u16).into();
b.iter(|| {
let count = criterion::black_box(tree.range(&start_key..=&end_key)).count();
assert_eq!(
count, size as usize,
"Not all items are present in the tree"
);
})
});
}

group.finish();
}

fn gen_keys(l1_prefix: usize, l2_prefix: usize, suffix: usize) -> Vec<String> {
let mut keys = Vec::new();
let chars: Vec<char> = ('a'..='z').collect();
Expand All @@ -177,4 +227,12 @@ fn gen_keys(l1_prefix: usize, l2_prefix: usize, suffix: usize) -> Vec<String> {
criterion_group!(delete_benches, seq_delete, rand_delete);
criterion_group!(insert_benches, seq_insert, rand_insert);
criterion_group!(read_benches, seq_get, rand_get, rand_get_str);
criterion_main!(insert_benches, read_benches);
criterion_group!(iter_benches, iter_benchmark);
criterion_group!(range_benches, range_benchmark);
criterion_main!(
insert_benches,
read_benches,
delete_benches,
iter_benches,
range_benches
);
2 changes: 2 additions & 0 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl<'a, P: KeyTrait + 'a, V: Clone> Iter<'a, P, V> {
impl<'a, P: KeyTrait + 'a, V: Clone> Iterator for Iter<'a, P, V> {
type Item = (Vec<u8>, &'a V, &'a u64, &'a u64);

#[inline]
fn next(&mut self) -> Option<Self::Item> {
while let Some(node) = self.forward.iters.last_mut() {
let e = node.next();
Expand Down Expand Up @@ -493,6 +494,7 @@ where
impl<'a, K: 'a + KeyTrait, V: Clone, R: RangeBounds<K>> Iterator for Range<'a, K, V, R> {
type Item = (Vec<u8>, &'a V, &'a u64, &'a u64);

#[inline]
fn next(&mut self) -> Option<Self::Item> {
while let Some(node) = self.forward.iters.last_mut() {
if let Some(other) = node.next() {
Expand Down

0 comments on commit fe149e6

Please sign in to comment.