Skip to content
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
11 changes: 9 additions & 2 deletions storage/fuzz/fuzz_targets/ordered_index_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,15 @@ fn fuzz(input: FuzzInput) {
}

IndexOperation::CursorInsert { key, value } => {
// Just use regular insert - simpler and avoids borrow issues
index.insert(key, *value);
if index.get(key).next().is_some() {
let mut cursor = index.get_mut(key).unwrap();
if cursor.next().is_some() {
cursor.insert(*value);
let _ = cursor.next();
}
} else {
index.insert(key, *value);
}
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions storage/fuzz/fuzz_targets/unordered_index_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,15 @@ fn fuzz(input: FuzzInput) {
}

IndexOperation::CursorInsert { key, value } => {
// Just use regular insert - simpler and avoids borrow issues
index.insert(key, *value);
if index.get(key).next().is_some() {
let mut cursor = index.get_mut(key).unwrap();
if cursor.next().is_some() {
cursor.insert(*value);
let _ = cursor.next();
}
} else {
index.insert(key, *value);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions storage/src/index/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod hashmap_insert;
mod hashmap_insert_fixed;
mod hashmap_iteration;
mod insert;
mod insert_and_prune;
mod lookup;
mod lookup_miss;

Expand Down Expand Up @@ -51,6 +52,7 @@ criterion_main!(
hashmap_insert_fixed::benches,
hashmap_insert::benches,
insert::benches,
insert_and_prune::benches,
lookup::benches,
lookup_miss::benches,
);
60 changes: 60 additions & 0 deletions storage/src/index/benches/insert_and_prune.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use super::DummyMetrics;
use commonware_cryptography::{Hasher, Sha256};
use commonware_storage::{
index::{unordered, Unordered},
translator::FourCap,
};
use criterion::{criterion_group, Criterion};
use rand::{rngs::StdRng, seq::SliceRandom, SeedableRng};
use std::time::{Duration, Instant};

#[cfg(not(full_bench))]
const N_ITEMS: [usize; 2] = [10_000, 50_000];
#[cfg(full_bench)]
const N_ITEMS: [usize; 4] = [10_000, 50_000, 100_000, 500_000];

fn bench_insert_and_prune(c: &mut Criterion) {
for items in N_ITEMS {
let mut rng = StdRng::seed_from_u64(0);
let mut kvs = Vec::with_capacity(items);
for i in 0..items {
kvs.push((Sha256::hash(&i.to_be_bytes()), i as u64));
}
kvs.shuffle(&mut rng);

c.bench_function(&format!("{}/items={items}", module_path!()), |b| {
let kvs_data = kvs.clone();
b.iter_custom(move |iters| {
let mut total = Duration::ZERO;
for _ in 0..iters {
let mut index = unordered::Index::new(DummyMetrics, FourCap);
total += run_benchmark(&mut index, &kvs_data);
}
total
});
});
}
}

fn run_benchmark<I: Unordered<Value = u64>>(
index: &mut I,
kvs: &[(<Sha256 as Hasher>::Digest, u64)],
) -> Duration {
// Seed the index with initial values.
for (k, v) in kvs {
index.insert(k, *v);
}

// Overwrite every key using insert_and_prune: prune the old value, insert the new one.
let start = Instant::now();
for (k, v) in kvs {
index.insert_and_prune(k, *v + 1, |old| *old == *v);
}
start.elapsed()
}

criterion_group! {
name = benches;
config = Criterion::default().sample_size(10);
targets = bench_insert_and_prune
}
Loading
Loading