Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Optimized null_count (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Sep 26, 2021
1 parent 39caeb3 commit e27ff27
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
27 changes: 27 additions & 0 deletions benches/bitmap_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@ fn add_benchmark(c: &mut Criterion) {
assert!(r.null_count() > 0);
})
});

let offset = ((size as f64) * 0.1) as usize;
let len = ((size as f64) * 0.85) as usize;

c.bench_function(
&format!("bitmap count zeros 85% slice 2^{}", log2_size),
|b| {
b.iter(|| {
let r = bitmap.clone().slice(offset, len);
assert!(r.null_count() > 0);
})
},
);

let offset = ((size as f64) * 0.2) as usize;
let len = ((size as f64) * 0.51) as usize;

c.bench_function(
&format!("bitmap count zeros 51% slice 2^{}", log2_size),
|b| {
b.iter(|| {
let r = bitmap.clone().slice(offset, len);
assert!(r.null_count() > 0);
})
},
);

let bitmap1 = bitmap.clone().slice(1, size - 1);
c.bench_function(&format!("bitmap not 2^{}", log2_size), |b| {
b.iter(|| {
Expand Down
12 changes: 11 additions & 1 deletion src/bitmap/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,19 @@ impl Bitmap {
/// The caller must ensure that `self.offset + offset + length <= self.len()`
#[inline]
pub unsafe fn slice_unchecked(mut self, offset: usize, length: usize) -> Self {
// count the smallest chunk
if length < self.length / 2 {
// count the null values in the slice
self.null_count = count_zeros(&self.bytes, offset, length);
} else {
// subtract the null count of the chunks we slice off
let start_end = self.offset + offset + length;
let head_count = count_zeros(&self.bytes, self.offset, offset);
let tail_count = count_zeros(&self.bytes, start_end, self.length - length - offset);
self.null_count -= head_count + tail_count;
}
self.offset += offset;
self.length = length;
self.null_count = count_zeros(&self.bytes, self.offset, self.length);
self
}

Expand Down

0 comments on commit e27ff27

Please sign in to comment.