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.
This commit is contained in:
parent
a06547508a
commit
dd28c40c29
2 changed files with 13 additions and 13 deletions
|
@ -1529,7 +1529,7 @@ impl<R: Idx, C: Idx> fmt::Debug for BitMatrix<R, C> {
|
||||||
/// sparse representation.
|
/// sparse representation.
|
||||||
///
|
///
|
||||||
/// Initially, every row has no explicit representation. If any bit within a
|
/// Initially, every row has no explicit representation. If any bit within a
|
||||||
/// row is set, the entire row is instantiated as `Some(<ChunkedBitSet>)`.
|
/// row is set, the entire row is instantiated as `Some(<BitSet>)`.
|
||||||
/// Furthermore, any previously uninstantiated rows prior to it will be
|
/// Furthermore, any previously uninstantiated rows prior to it will be
|
||||||
/// instantiated as `None`. Those prior rows may themselves become fully
|
/// instantiated as `None`. Those prior rows may themselves become fully
|
||||||
/// instantiated later on if any of their bits are set.
|
/// instantiated later on if any of their bits are set.
|
||||||
|
@ -1543,7 +1543,7 @@ where
|
||||||
C: Idx,
|
C: Idx,
|
||||||
{
|
{
|
||||||
num_columns: usize,
|
num_columns: usize,
|
||||||
rows: IndexVec<R, Option<ChunkedBitSet<C>>>,
|
rows: IndexVec<R, Option<BitSet<C>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
||||||
|
@ -1552,10 +1552,10 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
||||||
Self { num_columns, rows: IndexVec::new() }
|
Self { num_columns, rows: IndexVec::new() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ensure_row(&mut self, row: R) -> &mut ChunkedBitSet<C> {
|
fn ensure_row(&mut self, row: R) -> &mut BitSet<C> {
|
||||||
// Instantiate any missing rows up to and including row `row` with an empty ChunkedBitSet.
|
// Instantiate any missing rows up to and including row `row` with an empty `BitSet`.
|
||||||
// Then replace row `row` with a full ChunkedBitSet if necessary.
|
// Then replace row `row` with a full `BitSet` if necessary.
|
||||||
self.rows.get_or_insert_with(row, || ChunkedBitSet::new_empty(self.num_columns))
|
self.rows.get_or_insert_with(row, || BitSet::new_empty(self.num_columns))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the cell at `(row, column)` to true. Put another way, insert
|
/// Sets the cell at `(row, column)` to true. Put another way, insert
|
||||||
|
@ -1629,7 +1629,7 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
||||||
self.row(row).into_iter().flat_map(|r| r.iter())
|
self.row(row).into_iter().flat_map(|r| r.iter())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn row(&self, row: R) -> Option<&ChunkedBitSet<C>> {
|
pub fn row(&self, row: R) -> Option<&BitSet<C>> {
|
||||||
self.rows.get(row)?.as_ref()
|
self.rows.get(row)?.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1639,7 +1639,7 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
||||||
/// Returns true if the row was changed.
|
/// Returns true if the row was changed.
|
||||||
pub fn intersect_row<Set>(&mut self, row: R, set: &Set) -> bool
|
pub fn intersect_row<Set>(&mut self, row: R, set: &Set) -> bool
|
||||||
where
|
where
|
||||||
ChunkedBitSet<C>: BitRelations<Set>,
|
BitSet<C>: BitRelations<Set>,
|
||||||
{
|
{
|
||||||
match self.rows.get_mut(row) {
|
match self.rows.get_mut(row) {
|
||||||
Some(Some(row)) => row.intersect(set),
|
Some(Some(row)) => row.intersect(set),
|
||||||
|
@ -1653,7 +1653,7 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
||||||
/// Returns true if the row was changed.
|
/// Returns true if the row was changed.
|
||||||
pub fn subtract_row<Set>(&mut self, row: R, set: &Set) -> bool
|
pub fn subtract_row<Set>(&mut self, row: R, set: &Set) -> bool
|
||||||
where
|
where
|
||||||
ChunkedBitSet<C>: BitRelations<Set>,
|
BitSet<C>: BitRelations<Set>,
|
||||||
{
|
{
|
||||||
match self.rows.get_mut(row) {
|
match self.rows.get_mut(row) {
|
||||||
Some(Some(row)) => row.subtract(set),
|
Some(Some(row)) => row.subtract(set),
|
||||||
|
@ -1667,7 +1667,7 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
||||||
/// Returns true if the row was changed.
|
/// Returns true if the row was changed.
|
||||||
pub fn union_row<Set>(&mut self, row: R, set: &Set) -> bool
|
pub fn union_row<Set>(&mut self, row: R, set: &Set) -> bool
|
||||||
where
|
where
|
||||||
ChunkedBitSet<C>: BitRelations<Set>,
|
BitSet<C>: BitRelations<Set>,
|
||||||
{
|
{
|
||||||
self.ensure_row(row).union(set)
|
self.ensure_row(row).union(set)
|
||||||
}
|
}
|
||||||
|
|
|
@ -503,15 +503,15 @@ fn sparse_matrix_operations() {
|
||||||
matrix.insert(2, 99);
|
matrix.insert(2, 99);
|
||||||
matrix.insert(4, 0);
|
matrix.insert(4, 0);
|
||||||
|
|
||||||
let mut disjoint: ChunkedBitSet<usize> = ChunkedBitSet::new_empty(100);
|
let mut disjoint: BitSet<usize> = BitSet::new_empty(100);
|
||||||
disjoint.insert(33);
|
disjoint.insert(33);
|
||||||
|
|
||||||
let mut superset = ChunkedBitSet::new_empty(100);
|
let mut superset = BitSet::new_empty(100);
|
||||||
superset.insert(22);
|
superset.insert(22);
|
||||||
superset.insert(75);
|
superset.insert(75);
|
||||||
superset.insert(33);
|
superset.insert(33);
|
||||||
|
|
||||||
let mut subset = ChunkedBitSet::new_empty(100);
|
let mut subset = BitSet::new_empty(100);
|
||||||
subset.insert(22);
|
subset.insert(22);
|
||||||
|
|
||||||
// SparseBitMatrix::remove
|
// SparseBitMatrix::remove
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue