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

Dev bp clippy #18

Merged
merged 10 commits into from
Jan 9, 2025
52 changes: 34 additions & 18 deletions benches/bp.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![allow(long_running_const_eval)]

use std::cmp::Reverse;
use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashSet};
use rand::rngs::StdRng;
use vers_vecs::trees::bp::BpDfsBuilder;
use vers_vecs::trees::bp::BpTree;
use vers_vecs::trees::{DfsTreeBuilder, Tree};
Expand Down Expand Up @@ -37,7 +37,11 @@ fn generate_tree<R: Rng>(rng: &mut R, nodes: u64) -> BpTree<BLOCK_SIZE> {

// keep a priority queue of nodes with degree one to reduce runtime from O(n^2) to O(n log n)
let mut degree_one_set = BinaryHeap::new();
degrees.iter().enumerate().filter(|(_, &v)| v == 1).for_each(|(idx, _)| degree_one_set.push(Reverse(idx as u64)));
degrees
.iter()
.enumerate()
.filter(|(_, &v)| v == 1)
.for_each(|(idx, _)| degree_one_set.push(Reverse(idx as u64)));

sequence.iter().for_each(|&i| {
let j = degree_one_set.pop().unwrap().0;
Expand Down Expand Up @@ -74,9 +78,13 @@ fn generate_tree<R: Rng>(rng: &mut R, nodes: u64) -> BpTree<BLOCK_SIZE> {
if enter {
bpb.enter_node();
stack.push((depth, node, false));
for c in prefix_sum[node as usize]..*prefix_sum.get(node as usize + 1).unwrap_or(&children.len()) {
if visited.insert(children[c]) {
stack.push((depth + 1, children[c], true))
for child in children
.iter()
.take(*prefix_sum.get(node as usize + 1).unwrap_or(&children.len()))
.skip(prefix_sum[node as usize])
{
if visited.insert(*child) {
stack.push((depth + 1, *child, true))
}
}
} else {
Expand All @@ -102,27 +110,35 @@ fn bench_navigation(b: &mut Criterion) {
let node_handles = (0..l).map(|i| bp.node_handle(i)).collect::<Vec<_>>();

group.bench_with_input(BenchmarkId::new("parent", l), &l, |b, _| {
b.iter_batched(|| node_handles[rng.gen_range(0..node_handles.len())], |h| {
black_box(bp.parent(h))
}, BatchSize::SmallInput)
b.iter_batched(
|| node_handles[rng.gen_range(0..node_handles.len())],
|h| black_box(bp.parent(h)),
BatchSize::SmallInput,
)
});

group.bench_with_input(BenchmarkId::new("last_child", l), &l, |b, _| {
b.iter_batched(|| node_handles[rng.gen_range(0..node_handles.len())], |h| {
black_box(bp.last_child(h))
}, BatchSize::SmallInput)
b.iter_batched(
|| node_handles[rng.gen_range(0..node_handles.len())],
|h| black_box(bp.last_child(h)),
BatchSize::SmallInput,
)
});

group.bench_with_input(BenchmarkId::new("next_sibling", l), &l, |b, _| {
b.iter_batched(|| node_handles[rng.gen_range(0..node_handles.len())], |h| {
black_box(bp.next_sibling(h))
}, BatchSize::SmallInput)
b.iter_batched(
|| node_handles[rng.gen_range(0..node_handles.len())],
|h| black_box(bp.next_sibling(h)),
BatchSize::SmallInput,
)
});

group.bench_with_input(BenchmarkId::new("prev_sibling", l), &l, |b, _| {
b.iter_batched(|| node_handles[rng.gen_range(0..node_handles.len())], |h| {
black_box(bp.previous_sibling(h))
}, BatchSize::SmallInput)
b.iter_batched(
|| node_handles[rng.gen_range(0..node_handles.len())],
|h| black_box(bp.previous_sibling(h)),
BatchSize::SmallInput,
)
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions benches/sparse_equals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn bench(b: &mut Criterion<TimeDiff>) {

for fill_factor in FILL_FACTORS {
group.bench_with_input(
BenchmarkId::new("sparse overhead equal", &fill_factor),
BenchmarkId::new("sparse overhead equal", fill_factor),
&fill_factor,
|b, _| {
b.iter_custom(|iters| {
Expand All @@ -69,7 +69,7 @@ fn bench(b: &mut Criterion<TimeDiff>) {
);

group.bench_with_input(
BenchmarkId::new("sparse overhead unequal", &fill_factor),
BenchmarkId::new("sparse overhead unequal", fill_factor),
&fill_factor,
|b, _| {
b.iter_custom(|iters| {
Expand Down
34 changes: 17 additions & 17 deletions src/bit_vec/fast_rs_vec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ fn test_random_data_rank() {
let data_index = rnd_index / WORD_SIZE;
let bit_index = rnd_index % WORD_SIZE;

for i in 0..data_index {
expected_rank1 += data[i].count_ones() as usize;
expected_rank0 += data[i].count_zeros() as usize;
for v in data.iter().take(data_index) {
expected_rank1 += v.count_ones() as usize;
expected_rank0 += v.count_zeros() as usize;
}

if bit_index > 0 {
expected_rank1 += (data[data_index] & (1 << bit_index) - 1).count_ones() as usize;
expected_rank0 += (!data[data_index] & (1 << bit_index) - 1).count_ones() as usize;
expected_rank1 += (data[data_index] & ((1 << bit_index) - 1)).count_ones() as usize;
expected_rank0 += (!data[data_index] & ((1 << bit_index) - 1)).count_ones() as usize;
}

assert_eq!(actual_rank1, expected_rank1);
Expand Down Expand Up @@ -503,14 +503,14 @@ fn test_custom_iter_behavior() {
assert!(iter.advance_by(6).is_err());
assert!(iter.advance_back_by(5).is_ok());

assert_eq!(rs.iter().skip(2).next(), Some(0));
assert_eq!(rs.iter().nth(2), Some(0));
assert_eq!(rs.iter().count(), 10);
assert_eq!(rs.iter().skip(2).count(), 8);
assert_eq!(rs.iter().last(), Some(0));
assert_eq!(rs.iter().nth(3), Some(1));
assert_eq!(rs.iter().nth(12), None);

assert_eq!(rs.clone().into_iter().skip(2).next(), Some(0));
assert_eq!(rs.clone().into_iter().nth(2), Some(0));
assert_eq!(rs.clone().into_iter().count(), 10);
assert_eq!(rs.clone().into_iter().skip(2).count(), 8);
assert_eq!(rs.clone().into_iter().last(), Some(0));
Expand Down Expand Up @@ -1093,21 +1093,21 @@ fn test_sparse_equals() {
let rs1 = RsVec::from_bit_vec(bv.clone());
let rs2 = RsVec::from_bit_vec(bv.clone());

assert_eq!(rs1.sparse_equals::<false>(&rs2), true);
assert_eq!(rs1.sparse_equals::<true>(&rs2), true);
assert!(rs1.sparse_equals::<false>(&rs2));
assert!(rs1.sparse_equals::<true>(&rs2));

bv.flip_bit(3);
let rs2 = RsVec::from_bit_vec(bv.clone());

assert_eq!(rs1.sparse_equals::<false>(&rs2), false);
assert_eq!(rs1.sparse_equals::<true>(&rs2), false);
assert!(!rs1.sparse_equals::<false>(&rs2));
assert!(!rs1.sparse_equals::<true>(&rs2));

bv.flip_bit(3);
bv.flip_bit(2 * SUPER_BLOCK_SIZE - 1);
let rs1 = RsVec::from_bit_vec(bv.clone());

assert_eq!(rs1.sparse_equals::<false>(&rs2), false);
assert_eq!(rs1.sparse_equals::<true>(&rs2), false);
assert!(!rs1.sparse_equals::<false>(&rs2));
assert!(!rs1.sparse_equals::<true>(&rs2));
}

#[test]
Expand Down Expand Up @@ -1137,18 +1137,18 @@ fn test_full_equals() {
let rs1 = RsVec::from_bit_vec(bv.clone());
let rs2 = RsVec::from_bit_vec(bv.clone());

assert_eq!(rs1.full_equals(&rs2), true);
assert!(rs1.full_equals(&rs2));

bv.flip_bit(3);
let rs2 = RsVec::from_bit_vec(bv.clone());

assert_eq!(rs1.full_equals(&rs2), false);
assert!(!rs1.full_equals(&rs2));

bv.flip_bit(3);
bv.flip_bit(2 * SUPER_BLOCK_SIZE - 1);
let rs1 = RsVec::from_bit_vec(bv.clone());

assert_eq!(rs1.full_equals(&rs2), false);
assert!(!rs1.full_equals(&rs2));
}

// fuzzing test for iter1 and iter0 as last ditch fail-safe
Expand Down Expand Up @@ -1332,7 +1332,7 @@ fn test_iter1_regression_i8() {
let mut bv = BitVec::from_zeros(8193);

for idx in &input_on_bits {
bv.set(*idx as usize, 1).unwrap();
bv.set(*idx, 1).unwrap();
}

let bv = RsVec::from_bit_vec(bv);
Expand Down
2 changes: 1 addition & 1 deletion src/bit_vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ impl BitVec {
return;
}

let new_limb_count = (self.len - n + WORD_SIZE - 1) / WORD_SIZE;
let new_limb_count = (self.len - n).div_ceil(WORD_SIZE);

// cut off limbs that we no longer need
if new_limb_count < self.data.len() {
Expand Down
4 changes: 2 additions & 2 deletions src/bit_vec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,14 @@ fn test_custom_iter_behavior() {
assert!(iter.advance_by(6).is_err());
assert!(iter.advance_back_by(5).is_ok());

assert_eq!(bv.iter().skip(2).next(), Some(0));
faassen marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(bv.iter().nth(2), Some(0));
assert_eq!(bv.iter().count(), 10);
assert_eq!(bv.iter().skip(2).count(), 8);
assert_eq!(bv.iter().last(), Some(0));
assert_eq!(bv.iter().nth(3), Some(1));
assert_eq!(bv.iter().nth(12), None);

assert_eq!(bv.clone().into_iter().skip(2).next(), Some(0));
assert_eq!(bv.clone().into_iter().nth(2), Some(0));
assert_eq!(bv.clone().into_iter().count(), 10);
assert_eq!(bv.clone().into_iter().skip(2).count(), 8);
assert_eq!(bv.clone().into_iter().last(), Some(0));
Expand Down
Loading