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

Implement ones using trailing_zeros #18

Merged
merged 2 commits into from
Jan 23, 2021
Merged
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
58 changes: 18 additions & 40 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ impl FixedBitSet
length: bits,
}
}

/// Create a new **FixedBitSet** with a specific number of bits,
/// initialized from provided blocks.
///
/// If the blocks are not the exact size needed for the capacity
/// they will be padded with zeros (if shorter) or truncated to
/// the capacity (if longer).
///
///
/// For example:
/// ```
/// let data = vec![4];
Expand Down Expand Up @@ -302,17 +303,15 @@ impl FixedBitSet
match self.as_slice().split_first() {
Some((&block, rem)) => {
Ones {
current_bit_idx: 0,
current_block_idx: 0,
current_block: block,
bitset: block,
block_idx: 0,
remaining_blocks: rem
}
}
None => {
Ones {
current_bit_idx: 0,
current_block_idx: 0,
current_block: 0,
bitset: 0,
block_idx: 0,
remaining_blocks: &[]
}
}
Expand Down Expand Up @@ -593,49 +592,28 @@ impl Iterator for Masks {
///
/// This struct is created by the [`FixedBitSet::ones`] method.
pub struct Ones<'a> {
current_bit_idx: usize,
current_block_idx: usize,
bitset: Block,
block_idx: usize,
remaining_blocks: &'a [Block],
current_block: Block
}

impl<'a> Iterator for Ones<'a> {
type Item = usize; // the bit position of the '1'

#[inline]
fn next(&mut self) -> Option<Self::Item> {
let mut block = self.current_block;
let mut idx = self.current_bit_idx;

loop {
loop {
if (block & 1) == 1 {
self.current_block = block >> 1;
self.current_bit_idx = idx + 1;
return Some(idx);
}
// reordering the two lines below makes a huge (2x) difference in performance!
block = block >> 1;
idx += 1;
if block == 0 {
break;
}
}

// go to next block
match self.remaining_blocks.split_first() {
Some((&next_block, rest)) => {
self.remaining_blocks = rest;
self.current_block_idx += 1;
idx = self.current_block_idx * BITS;
block = next_block;
}
None => {
// last block => done
return None;
}
while self.bitset == 0 {
if self.remaining_blocks.is_empty() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can accomplish all of is_empty, [0] and [1..] in one method call - self.remaining_blocks.split_first() for a presumed minor win in terms of bounds checking overhead 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or for that matter, reading split_first's implmentation with slice patterns - but I think that's a much more recent feature if let [first, tail @ ..] = self { Some((first, tail)) } else { None } but it's good to read and use elsewhere if not here.

return None;
}
self.bitset = self.remaining_blocks[0];
Copy link

@akraines akraines Jan 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of immediately setting bitset to remaining_blocks[0] and updating remaining blocks, why don't you search for the first none zero block and only then update remaining_blocks? (just to save some assignments)

Copy link

@akraines akraines Jan 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(the PR has been opened for almost 3 years, so I guess nothing needs be done until the maintainer shows interest, but I figured I'd add the comment in case something comes of it)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this is faster, because it might mess up vectorization. Could be interesting to benchmark.

self.remaining_blocks = &self.remaining_blocks[1..];
self.block_idx += 1;
}
let t = self.bitset & (0 as Block).wrapping_sub(self.bitset);
let r = self.bitset.trailing_zeros() as usize;
self.bitset ^= t;
Some(self.block_idx * BITS + r)
}
}

Expand Down