Skip to content

Commit dd28c40

Browse files
committed
Use BitSet in SparseBitMatrix.
A `ChunkedBitSet` has to be at least 2048 bits for it to outperform a `BitSet`, because that's the chunk size. The largest `SparseBitMatrix` encountered when compiling the compiler and the entire rustc-perf benchmark suite is less than 600 bits. This change is a tiny perf win, but the motivation is more about avoiding uses of `ChunkedBitSet` outside of `MixedBitSet`. The test change is necessary to avoid hitting the `<BitSet<T> as BitRelations<ChunkedBitSet<T>>>::subtract` method that has `unimplemented!` in its body and isn't otherwise used.
1 parent a065475 commit dd28c40

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

Diff for: compiler/rustc_index/src/bit_set.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ impl<R: Idx, C: Idx> fmt::Debug for BitMatrix<R, C> {
15291529
/// sparse representation.
15301530
///
15311531
/// Initially, every row has no explicit representation. If any bit within a
1532-
/// row is set, the entire row is instantiated as `Some(<ChunkedBitSet>)`.
1532+
/// row is set, the entire row is instantiated as `Some(<BitSet>)`.
15331533
/// Furthermore, any previously uninstantiated rows prior to it will be
15341534
/// instantiated as `None`. Those prior rows may themselves become fully
15351535
/// instantiated later on if any of their bits are set.
@@ -1543,7 +1543,7 @@ where
15431543
C: Idx,
15441544
{
15451545
num_columns: usize,
1546-
rows: IndexVec<R, Option<ChunkedBitSet<C>>>,
1546+
rows: IndexVec<R, Option<BitSet<C>>>,
15471547
}
15481548

15491549
impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
@@ -1552,10 +1552,10 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
15521552
Self { num_columns, rows: IndexVec::new() }
15531553
}
15541554

1555-
fn ensure_row(&mut self, row: R) -> &mut ChunkedBitSet<C> {
1556-
// Instantiate any missing rows up to and including row `row` with an empty ChunkedBitSet.
1557-
// Then replace row `row` with a full ChunkedBitSet if necessary.
1558-
self.rows.get_or_insert_with(row, || ChunkedBitSet::new_empty(self.num_columns))
1555+
fn ensure_row(&mut self, row: R) -> &mut BitSet<C> {
1556+
// Instantiate any missing rows up to and including row `row` with an empty `BitSet`.
1557+
// Then replace row `row` with a full `BitSet` if necessary.
1558+
self.rows.get_or_insert_with(row, || BitSet::new_empty(self.num_columns))
15591559
}
15601560

15611561
/// Sets the cell at `(row, column)` to true. Put another way, insert
@@ -1629,7 +1629,7 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
16291629
self.row(row).into_iter().flat_map(|r| r.iter())
16301630
}
16311631

1632-
pub fn row(&self, row: R) -> Option<&ChunkedBitSet<C>> {
1632+
pub fn row(&self, row: R) -> Option<&BitSet<C>> {
16331633
self.rows.get(row)?.as_ref()
16341634
}
16351635

@@ -1639,7 +1639,7 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
16391639
/// Returns true if the row was changed.
16401640
pub fn intersect_row<Set>(&mut self, row: R, set: &Set) -> bool
16411641
where
1642-
ChunkedBitSet<C>: BitRelations<Set>,
1642+
BitSet<C>: BitRelations<Set>,
16431643
{
16441644
match self.rows.get_mut(row) {
16451645
Some(Some(row)) => row.intersect(set),
@@ -1653,7 +1653,7 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
16531653
/// Returns true if the row was changed.
16541654
pub fn subtract_row<Set>(&mut self, row: R, set: &Set) -> bool
16551655
where
1656-
ChunkedBitSet<C>: BitRelations<Set>,
1656+
BitSet<C>: BitRelations<Set>,
16571657
{
16581658
match self.rows.get_mut(row) {
16591659
Some(Some(row)) => row.subtract(set),
@@ -1667,7 +1667,7 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
16671667
/// Returns true if the row was changed.
16681668
pub fn union_row<Set>(&mut self, row: R, set: &Set) -> bool
16691669
where
1670-
ChunkedBitSet<C>: BitRelations<Set>,
1670+
BitSet<C>: BitRelations<Set>,
16711671
{
16721672
self.ensure_row(row).union(set)
16731673
}

Diff for: compiler/rustc_index/src/bit_set/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -503,15 +503,15 @@ fn sparse_matrix_operations() {
503503
matrix.insert(2, 99);
504504
matrix.insert(4, 0);
505505

506-
let mut disjoint: ChunkedBitSet<usize> = ChunkedBitSet::new_empty(100);
506+
let mut disjoint: BitSet<usize> = BitSet::new_empty(100);
507507
disjoint.insert(33);
508508

509-
let mut superset = ChunkedBitSet::new_empty(100);
509+
let mut superset = BitSet::new_empty(100);
510510
superset.insert(22);
511511
superset.insert(75);
512512
superset.insert(33);
513513

514-
let mut subset = ChunkedBitSet::new_empty(100);
514+
let mut subset = BitSet::new_empty(100);
515515
subset.insert(22);
516516

517517
// SparseBitMatrix::remove

0 commit comments

Comments
 (0)