Skip to content

Commit caed80b

Browse files
committedAug 26, 2018
Auto merge of #53629 - nnethercote:lazier-SparseBitMatrix, r=nikomatsakis
Lazier sparse bit matrix A small NLL win. r? @nikomatsakis
2 parents ede5551 + 002f03b commit caed80b

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed
 

Diff for: ‎src/librustc_data_structures/bitvec.rs

+38-34
Original file line numberDiff line numberDiff line change
@@ -318,48 +318,57 @@ 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
{
329-
columns: usize,
330-
vector: IndexVec<R, BitArray<C>>,
335+
num_columns: usize,
336+
rows: IndexVec<R, Option<BitArray<C>>>,
331337
}
332338

333339
impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
334340
/// Create a new empty sparse bit matrix with no rows or columns.
335-
pub fn new(columns: usize) -> Self {
341+
pub fn new(num_columns: usize) -> Self {
336342
Self {
337-
columns,
338-
vector: IndexVec::new(),
343+
num_columns,
344+
rows: IndexVec::new(),
339345
}
340346
}
341347

342-
fn ensure_row(&mut self, row: R) {
343-
let columns = self.columns;
344-
self.vector
345-
.ensure_contains_elem(row, || BitArray::new(columns));
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.
354+
let num_columns = self.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.vector[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.vector.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,49 +379,44 @@ 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.vector.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.vector.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.vector[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.vector[row].insert_all();
392-
}
393-
394-
/// Number of elements in the matrix.
395-
pub fn len(&self) -> usize {
396-
self.vector.len()
401+
self.ensure_row(row).insert_all();
397402
}
398403

399404
pub fn rows(&self) -> impl Iterator<Item = R> {
400-
self.vector.indices()
405+
self.rows.indices()
401406
}
402407

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.vector.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.vector.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.vector.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)
Please sign in to comment.