Skip to content

Commit

Permalink
Tiny ChunkedBitSet improvements.
Browse files Browse the repository at this point in the history
- Fix a typo in a comment.
- Remove unnecessary `Chunk::` qualifiers.
- Rename `ChunkedBitIter::bitset` as `ChunkedBitIter::bit_set`, because
  `bit_set` is the form used everywhere else.
- Avoid some unnecessary local variables.
  • Loading branch information
nnethercote committed Nov 29, 2024
1 parent 15b24c4 commit ff78002
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions compiler/rustc_index/src/bit_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,7 @@ enum Chunk {
/// turns out to be both simpler and have better performance than
/// allocating the minimum number of words, largely because we avoid having
/// to store the length, which would make this type larger. These excess
/// words are always be zero, as are any excess bits in the final in-use
/// word.
/// words are always zero, as are any excess bits in the final in-use word.
///
/// The first `ChunkSize` field is always non-zero.
///
Expand Down Expand Up @@ -465,7 +464,7 @@ impl<T: Idx> ChunkedBitSet<T> {
}

pub fn is_empty(&self) -> bool {
self.chunks.iter().all(|chunk| matches!(chunk, Chunk::Zeros(..)))
self.chunks.iter().all(|chunk| matches!(chunk, Zeros(..)))
}

/// Returns `true` if `self` contains `elem`.
Expand Down Expand Up @@ -855,16 +854,16 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for BitSet<T> {
words = &mut words[..CHUNK_WORDS];
}
match chunk {
Chunk::Zeros(..) => {
Zeros(..) => {
for word in words {
if *word != 0 {
changed = true;
*word = 0;
}
}
}
Chunk::Ones(..) => (),
Chunk::Mixed(_, _, data) => {
Ones(..) => (),
Mixed(_, _, data) => {
for (i, word) in words.iter_mut().enumerate() {
let new_val = *word & data[i];
if new_val != *word {
Expand Down Expand Up @@ -902,22 +901,22 @@ impl<T> Clone for ChunkedBitSet<T> {

pub struct ChunkedBitIter<'a, T: Idx> {
index: usize,
bitset: &'a ChunkedBitSet<T>,
bit_set: &'a ChunkedBitSet<T>,
}

impl<'a, T: Idx> ChunkedBitIter<'a, T> {
#[inline]
fn new(bitset: &'a ChunkedBitSet<T>) -> ChunkedBitIter<'a, T> {
ChunkedBitIter { index: 0, bitset }
fn new(bit_set: &'a ChunkedBitSet<T>) -> ChunkedBitIter<'a, T> {
ChunkedBitIter { index: 0, bit_set }
}
}

impl<'a, T: Idx> Iterator for ChunkedBitIter<'a, T> {
type Item = T;
fn next(&mut self) -> Option<T> {
while self.index < self.bitset.domain_size() {
while self.index < self.bit_set.domain_size() {
let elem = T::new(self.index);
let chunk = &self.bitset.chunks[chunk_index(elem)];
let chunk = &self.bit_set.chunks[chunk_index(elem)];
match &chunk {
Zeros(chunk_domain_size) => {
self.index += *chunk_domain_size as usize;
Expand Down Expand Up @@ -954,17 +953,17 @@ impl<'a, T: Idx> Iterator for ChunkedBitIter<'a, T> {
init = f(init, item);
}
let start_chunk = self.index / CHUNK_BITS;
let chunks = &self.bitset.chunks[start_chunk..];
let chunks = &self.bit_set.chunks[start_chunk..];
for (i, chunk) in chunks.iter().enumerate() {
let base = (start_chunk + i) * CHUNK_BITS;
match chunk {
Chunk::Zeros(_) => (),
Chunk::Ones(limit) => {
Zeros(_) => (),
Ones(limit) => {
for j in 0..(*limit as usize) {
init = f(init, T::new(base + j));
}
}
Chunk::Mixed(_, _, words) => {
Mixed(_, _, words) => {
init = BitIter::new(&**words).fold(init, |val, mut item: T| {
item.increment_by(base);
f(val, item)
Expand Down Expand Up @@ -1302,15 +1301,13 @@ impl<'a, T: Idx> Iterator for BitIter<'a, T> {
// Get the position of the next set bit in the current word,
// then clear the bit.
let bit_pos = self.word.trailing_zeros() as usize;
let bit = 1 << bit_pos;
self.word ^= bit;
self.word ^= 1 << bit_pos;
return Some(T::new(bit_pos + self.offset));
}

// Move onto the next word. `wrapping_add()` is needed to handle
// the degenerate initial value given to `offset` in `new()`.
let word = self.iter.next()?;
self.word = *word;
self.word = *self.iter.next()?;
self.offset = self.offset.wrapping_add(WORD_BITS);
}
}
Expand Down

0 comments on commit ff78002

Please sign in to comment.