use common::database::block_offset_index::BlockOffsetIndex;
use std::hint::black_box;
use std::time::{Duration, Instant};
fn build_index(n: u32) -> BlockOffsetIndex {
let mut idx = BlockOffsetIndex::new();
for i in 0..n {
idx.push_block(i as u64, i * 100);
}
idx.set_total_bytes(n * 100);
idx
}
fn time_noop_shifts(n: u32, iters: u32) -> Duration {
let mut idx = build_index(n);
for _ in 0..50 {
idx.shift_after(black_box(u32::MAX), black_box(1));
}
let start = Instant::now();
for _ in 0..iters {
idx.shift_after(black_box(u32::MAX), black_box(1));
black_box(&idx);
}
start.elapsed()
}
#[test]
fn shift_after_noop_does_not_scale_with_entry_count() {
const ITERS: u32 = 1_000;
let t_small = time_noop_shifts(1_000, ITERS);
let t_large = time_noop_shifts(100_000, ITERS);
let ratio = t_large.as_nanos() as f64 / t_small.as_nanos().max(1) as f64;
assert!(
ratio < 20.0,
"shift_after appears O(N) on the noop path: a 100× larger \
index took {:.1}× longer ({:?} for 100_000 entries vs {:?} \
for 1_000). The fix is a `partition_point`-based binary \
search — after that, the ratio should approach 1.",
ratio,
t_large,
t_small,
);
}