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

Commit

Permalink
Improve bitmap slice unchecked (#1574)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dousir9 committed Oct 4, 2023
1 parent 6271f48 commit 63e99ad
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/bitmap/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,15 @@ 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) {
// first guard a no-op slice so that we don't do a bitcount
// if there isn't any data sliced
if !(offset == 0 && length == self.length) {
// count the smallest chunk
if length < self.length / 2 {
// we don't do a bitcount in the following cases:
// 1. if there isn't any data sliced.
// 2. if this [`Bitmap`] is all true or all false.
if !(offset == 0 && length == self.length || self.unset_bits == 0) {
// if `self.unset_bits == self.length` is false, we count the smallest chunk
// and do a bitcount.
if self.unset_bits == self.length {
self.unset_bits = length;
} else if length < self.length / 2 {
// count the null values in the slice
self.unset_bits = count_zeros(&self.bytes, self.offset + offset, length);
} else {
Expand All @@ -186,9 +190,9 @@ impl Bitmap {
let tail_count = count_zeros(&self.bytes, start_end, self.length - length - offset);
self.unset_bits -= head_count + tail_count;
}
self.offset += offset;
self.length = length;
}
self.offset += offset;
self.length = length;
}

/// Slices `self`, offsetting by `offset` and truncating up to `length` bits.
Expand Down

0 comments on commit 63e99ad

Please sign in to comment.