Skip to content

Commit 002f03b

Browse files
committed
Make SparseBitMatrix a bit lazier.
Currently when a row is instantiated in SparseBitMatrix, any missing rows prior to it are also fully instantiated. This patch changes things so that those prior rows are minimally instantiated (with a `None`). This avoids a decent number of allocations in NLL, speeding up several benchmarks by up to 0.5%. The patch also removes two unused methods, `len()` and `iter_enumerated()`.
1 parent 7f4c168 commit 002f03b

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

src/librustc_data_structures/bitvec.rs

+32-28
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,22 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
318318
}
319319
}
320320

321-
/// A moderately sparse bit matrix: rows are appended lazily, but columns
322-
/// within appended rows are instantiated fully upon creation.
321+
/// A moderately sparse bit matrix, in which rows are instantiated lazily.
322+
///
323+
/// Initially, every row has no explicit representation. If any bit within a
324+
/// row is set, the entire row is instantiated as
325+
/// `Some(<full-column-width-BitArray>)`. Furthermore, any previously
326+
/// uninstantiated rows prior to it will be instantiated as `None`. Those prior
327+
/// rows may themselves become fully instantiated later on if any of their bits
328+
/// are set.
323329
#[derive(Clone, Debug)]
324330
pub struct SparseBitMatrix<R, C>
325331
where
326332
R: Idx,
327333
C: Idx,
328334
{
329335
num_columns: usize,
330-
rows: IndexVec<R, BitArray<C>>,
336+
rows: IndexVec<R, Option<BitArray<C>>>,
331337
}
332338

333339
impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
@@ -339,27 +345,30 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
339345
}
340346
}
341347

342-
fn ensure_row(&mut self, row: R) {
348+
fn ensure_row(&mut self, row: R) -> &mut BitArray<C> {
349+
// Instantiate any missing rows up to and including row `row` with an
350+
// empty BitArray.
351+
self.rows.ensure_contains_elem(row, || None);
352+
353+
// Then replace row `row` with a full BitArray if necessary.
343354
let num_columns = self.num_columns;
344-
self.rows
345-
.ensure_contains_elem(row, || BitArray::new(num_columns));
355+
self.rows[row].get_or_insert_with(|| BitArray::new(num_columns))
346356
}
347357

348358
/// Sets the cell at `(row, column)` to true. Put another way, insert
349359
/// `column` to the bitset for `row`.
350360
///
351361
/// Returns true if this changed the matrix, and false otherwise.
352362
pub fn add(&mut self, row: R, column: C) -> bool {
353-
self.ensure_row(row);
354-
self.rows[row].insert(column)
363+
self.ensure_row(row).insert(column)
355364
}
356365

357366
/// Do the bits from `row` contain `column`? Put another way, is
358367
/// the matrix cell at `(row, column)` true? Put yet another way,
359368
/// if the matrix represents (transitive) reachability, can
360369
/// `row` reach `column`?
361370
pub fn contains(&self, row: R, column: C) -> bool {
362-
self.rows.get(row).map_or(false, |r| r.contains(column))
371+
self.row(row).map_or(false, |r| r.contains(column))
363372
}
364373

365374
/// Add the bits from row `read` to the bits from row `write`,
@@ -370,30 +379,26 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
370379
/// `write` can reach everything that `read` can (and
371380
/// potentially more).
372381
pub fn merge(&mut self, read: R, write: R) -> bool {
373-
if read == write || self.rows.get(read).is_none() {
382+
if read == write || self.row(read).is_none() {
374383
return false;
375384
}
376385

377386
self.ensure_row(write);
378-
let (bitvec_read, bitvec_write) = self.rows.pick2_mut(read, write);
379-
bitvec_write.merge(bitvec_read)
387+
if let (Some(bitvec_read), Some(bitvec_write)) = self.rows.pick2_mut(read, write) {
388+
bitvec_write.merge(bitvec_read)
389+
} else {
390+
unreachable!()
391+
}
380392
}
381393

382394
/// Merge a row, `from`, into the `into` row.
383395
pub fn merge_into(&mut self, into: R, from: &BitArray<C>) -> bool {
384-
self.ensure_row(into);
385-
self.rows[into].merge(from)
396+
self.ensure_row(into).merge(from)
386397
}
387398

388399
/// Add all bits to the given row.
389400
pub fn add_all(&mut self, row: R) {
390-
self.ensure_row(row);
391-
self.rows[row].insert_all();
392-
}
393-
394-
/// Number of elements in the matrix.
395-
pub fn len(&self) -> usize {
396-
self.rows.len()
401+
self.ensure_row(row).insert_all();
397402
}
398403

399404
pub fn rows(&self) -> impl Iterator<Item = R> {
@@ -403,16 +408,15 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
403408
/// Iterates through all the columns set to true in a given row of
404409
/// the matrix.
405410
pub fn iter<'a>(&'a self, row: R) -> impl Iterator<Item = C> + 'a {
406-
self.rows.get(row).into_iter().flat_map(|r| r.iter())
407-
}
408-
409-
/// Iterates through each row and the accompanying bit set.
410-
pub fn iter_enumerated<'a>(&'a self) -> impl Iterator<Item = (R, &'a BitArray<C>)> + 'a {
411-
self.rows.iter_enumerated()
411+
self.row(row).into_iter().flat_map(|r| r.iter())
412412
}
413413

414414
pub fn row(&self, row: R) -> Option<&BitArray<C>> {
415-
self.rows.get(row)
415+
if let Some(Some(row)) = self.rows.get(row) {
416+
Some(row)
417+
} else {
418+
None
419+
}
416420
}
417421
}
418422

0 commit comments

Comments
 (0)