From 7f4c168a9dd2195b45998ddfecdac111663b26d1 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 23 Aug 2018 11:08:07 +1000 Subject: [PATCH 1/2] Rename the fields in `SparseBitMatrix`. The new names are clearer. --- src/librustc_data_structures/bitvec.rs | 38 +++++++++++++------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/librustc_data_structures/bitvec.rs b/src/librustc_data_structures/bitvec.rs index 49ab3e58812dc..1ac32af55f603 100644 --- a/src/librustc_data_structures/bitvec.rs +++ b/src/librustc_data_structures/bitvec.rs @@ -326,23 +326,23 @@ where R: Idx, C: Idx, { - columns: usize, - vector: IndexVec>, + num_columns: usize, + rows: IndexVec>, } impl SparseBitMatrix { /// Create a new empty sparse bit matrix with no rows or columns. - pub fn new(columns: usize) -> Self { + pub fn new(num_columns: usize) -> Self { Self { - columns, - vector: IndexVec::new(), + num_columns, + rows: IndexVec::new(), } } fn ensure_row(&mut self, row: R) { - let columns = self.columns; - self.vector - .ensure_contains_elem(row, || BitArray::new(columns)); + let num_columns = self.num_columns; + self.rows + .ensure_contains_elem(row, || BitArray::new(num_columns)); } /// Sets the cell at `(row, column)` to true. Put another way, insert @@ -351,7 +351,7 @@ impl SparseBitMatrix { /// Returns true if this changed the matrix, and false otherwise. pub fn add(&mut self, row: R, column: C) -> bool { self.ensure_row(row); - self.vector[row].insert(column) + self.rows[row].insert(column) } /// Do the bits from `row` contain `column`? Put another way, is @@ -359,7 +359,7 @@ impl SparseBitMatrix { /// if the matrix represents (transitive) reachability, can /// `row` reach `column`? pub fn contains(&self, row: R, column: C) -> bool { - self.vector.get(row).map_or(false, |r| r.contains(column)) + self.rows.get(row).map_or(false, |r| r.contains(column)) } /// Add the bits from row `read` to the bits from row `write`, @@ -370,49 +370,49 @@ impl SparseBitMatrix { /// `write` can reach everything that `read` can (and /// potentially more). pub fn merge(&mut self, read: R, write: R) -> bool { - if read == write || self.vector.get(read).is_none() { + if read == write || self.rows.get(read).is_none() { return false; } self.ensure_row(write); - let (bitvec_read, bitvec_write) = self.vector.pick2_mut(read, write); + let (bitvec_read, bitvec_write) = self.rows.pick2_mut(read, write); bitvec_write.merge(bitvec_read) } /// Merge a row, `from`, into the `into` row. pub fn merge_into(&mut self, into: R, from: &BitArray) -> bool { self.ensure_row(into); - self.vector[into].merge(from) + self.rows[into].merge(from) } /// Add all bits to the given row. pub fn add_all(&mut self, row: R) { self.ensure_row(row); - self.vector[row].insert_all(); + self.rows[row].insert_all(); } /// Number of elements in the matrix. pub fn len(&self) -> usize { - self.vector.len() + self.rows.len() } pub fn rows(&self) -> impl Iterator { - self.vector.indices() + self.rows.indices() } /// Iterates through all the columns set to true in a given row of /// the matrix. pub fn iter<'a>(&'a self, row: R) -> impl Iterator + 'a { - self.vector.get(row).into_iter().flat_map(|r| r.iter()) + self.rows.get(row).into_iter().flat_map(|r| r.iter()) } /// Iterates through each row and the accompanying bit set. pub fn iter_enumerated<'a>(&'a self) -> impl Iterator)> + 'a { - self.vector.iter_enumerated() + self.rows.iter_enumerated() } pub fn row(&self, row: R) -> Option<&BitArray> { - self.vector.get(row) + self.rows.get(row) } } From 002f03b654845667023cdaad8af988909a030bfe Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 23 Aug 2018 14:29:38 +1000 Subject: [PATCH 2/2] 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()`. --- src/librustc_data_structures/bitvec.rs | 60 ++++++++++++++------------ 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/src/librustc_data_structures/bitvec.rs b/src/librustc_data_structures/bitvec.rs index 1ac32af55f603..642d24f48313a 100644 --- a/src/librustc_data_structures/bitvec.rs +++ b/src/librustc_data_structures/bitvec.rs @@ -318,8 +318,14 @@ impl BitMatrix { } } -/// A moderately sparse bit matrix: rows are appended lazily, but columns -/// within appended rows are instantiated fully upon creation. +/// A moderately sparse bit matrix, in which rows are instantiated lazily. +/// +/// Initially, every row has no explicit representation. If any bit within a +/// row is set, the entire row is instantiated as +/// `Some()`. Furthermore, any previously +/// uninstantiated rows prior to it will be instantiated as `None`. Those prior +/// rows may themselves become fully instantiated later on if any of their bits +/// are set. #[derive(Clone, Debug)] pub struct SparseBitMatrix where @@ -327,7 +333,7 @@ where C: Idx, { num_columns: usize, - rows: IndexVec>, + rows: IndexVec>>, } impl SparseBitMatrix { @@ -339,10 +345,14 @@ impl SparseBitMatrix { } } - fn ensure_row(&mut self, row: R) { + fn ensure_row(&mut self, row: R) -> &mut BitArray { + // Instantiate any missing rows up to and including row `row` with an + // empty BitArray. + self.rows.ensure_contains_elem(row, || None); + + // Then replace row `row` with a full BitArray if necessary. let num_columns = self.num_columns; - self.rows - .ensure_contains_elem(row, || BitArray::new(num_columns)); + self.rows[row].get_or_insert_with(|| BitArray::new(num_columns)) } /// Sets the cell at `(row, column)` to true. Put another way, insert @@ -350,8 +360,7 @@ impl SparseBitMatrix { /// /// Returns true if this changed the matrix, and false otherwise. pub fn add(&mut self, row: R, column: C) -> bool { - self.ensure_row(row); - self.rows[row].insert(column) + self.ensure_row(row).insert(column) } /// Do the bits from `row` contain `column`? Put another way, is @@ -359,7 +368,7 @@ impl SparseBitMatrix { /// if the matrix represents (transitive) reachability, can /// `row` reach `column`? pub fn contains(&self, row: R, column: C) -> bool { - self.rows.get(row).map_or(false, |r| r.contains(column)) + self.row(row).map_or(false, |r| r.contains(column)) } /// Add the bits from row `read` to the bits from row `write`, @@ -370,30 +379,26 @@ impl SparseBitMatrix { /// `write` can reach everything that `read` can (and /// potentially more). pub fn merge(&mut self, read: R, write: R) -> bool { - if read == write || self.rows.get(read).is_none() { + if read == write || self.row(read).is_none() { return false; } self.ensure_row(write); - let (bitvec_read, bitvec_write) = self.rows.pick2_mut(read, write); - bitvec_write.merge(bitvec_read) + if let (Some(bitvec_read), Some(bitvec_write)) = self.rows.pick2_mut(read, write) { + bitvec_write.merge(bitvec_read) + } else { + unreachable!() + } } /// Merge a row, `from`, into the `into` row. pub fn merge_into(&mut self, into: R, from: &BitArray) -> bool { - self.ensure_row(into); - self.rows[into].merge(from) + self.ensure_row(into).merge(from) } /// Add all bits to the given row. pub fn add_all(&mut self, row: R) { - self.ensure_row(row); - self.rows[row].insert_all(); - } - - /// Number of elements in the matrix. - pub fn len(&self) -> usize { - self.rows.len() + self.ensure_row(row).insert_all(); } pub fn rows(&self) -> impl Iterator { @@ -403,16 +408,15 @@ impl SparseBitMatrix { /// Iterates through all the columns set to true in a given row of /// the matrix. pub fn iter<'a>(&'a self, row: R) -> impl Iterator + 'a { - self.rows.get(row).into_iter().flat_map(|r| r.iter()) - } - - /// Iterates through each row and the accompanying bit set. - pub fn iter_enumerated<'a>(&'a self) -> impl Iterator)> + 'a { - self.rows.iter_enumerated() + self.row(row).into_iter().flat_map(|r| r.iter()) } pub fn row(&self, row: R) -> Option<&BitArray> { - self.rows.get(row) + if let Some(Some(row)) = self.rows.get(row) { + Some(row) + } else { + None + } } }