Skip to content

Commit

Permalink
Clarify ChunkSize invariants.
Browse files Browse the repository at this point in the history
`ChunkedBitSet::is_empty` currently does an unnecessary check. This
commit removes that check and adds clarifying comments and an assertion
that demonstrate why it's unnecessary.
  • Loading branch information
nnethercote committed Nov 29, 2024
1 parent 6b6a867 commit 15b24c4
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions compiler/rustc_index/src/bit_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,11 @@ pub struct ChunkedBitSet<T> {
#[derive(Clone, Debug, PartialEq, Eq)]
enum Chunk {
/// A chunk that is all zeros; we don't represent the zeros explicitly.
/// The `ChunkSize` is always non-zero.
Zeros(ChunkSize),

/// A chunk that is all ones; we don't represent the ones explicitly.
/// `ChunkSize` is always non-zero.
Ones(ChunkSize),

/// A chunk that has a mix of zeros and ones, which are represented
Expand All @@ -384,8 +386,10 @@ enum Chunk {
/// words are always be zero, as are any excess bits in the final in-use
/// word.
///
/// The second field is the count of 1s set in the chunk, and must satisfy
/// `0 < count < chunk_domain_size`.
/// The first `ChunkSize` field is always non-zero.
///
/// The second `ChunkSize` field is the count of 1s set in the chunk, and
/// must satisfy `0 < count < chunk_domain_size`.
///
/// The words are within an `Rc` because it's surprisingly common to
/// duplicate an entire chunk, e.g. in `ChunkedBitSet::clone_from()`, or
Expand Down Expand Up @@ -461,7 +465,7 @@ impl<T: Idx> ChunkedBitSet<T> {
}

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

/// Returns `true` if `self` contains `elem`.
Expand Down Expand Up @@ -1005,7 +1009,7 @@ impl Chunk {
}

fn new(chunk_domain_size: usize, is_empty: bool) -> Self {
debug_assert!(chunk_domain_size <= CHUNK_BITS);
debug_assert!(0 < chunk_domain_size && chunk_domain_size <= CHUNK_BITS);
let chunk_domain_size = chunk_domain_size as ChunkSize;
if is_empty { Zeros(chunk_domain_size) } else { Ones(chunk_domain_size) }
}
Expand Down

0 comments on commit 15b24c4

Please sign in to comment.