Skip to content

Commit ff9eb56

Browse files
committed
Use Sparse bitsets instead of dense ones for NLL results
Fixes #48170
1 parent e5d79c4 commit ff9eb56

File tree

3 files changed

+224
-11
lines changed

3 files changed

+224
-11
lines changed

src/librustc_data_structures/bitvec.rs

+197
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::collections::BTreeMap;
12+
use std::collections::btree_map::Entry;
13+
use std::marker::PhantomData;
1114
use std::iter::FromIterator;
15+
use indexed_vec::{Idx, IndexVec};
1216

1317
type Word = u128;
1418
const WORD_BITS: usize = 128;
@@ -257,6 +261,199 @@ impl BitMatrix {
257261
}
258262
}
259263

264+
#[derive(Clone, Debug)]
265+
pub struct SparseBitMatrix<R, C> where R: Idx, C: Idx {
266+
vector: IndexVec<R, SparseBitSet<C>>,
267+
}
268+
269+
impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
270+
/// Create a new `rows x columns` matrix, initially empty.
271+
pub fn new(rows: R, _columns: C) -> SparseBitMatrix<R, C> {
272+
SparseBitMatrix {
273+
vector: IndexVec::from_elem_n(SparseBitSet::new(), rows.index()),
274+
}
275+
}
276+
277+
/// Sets the cell at `(row, column)` to true. Put another way, insert
278+
/// `column` to the bitset for `row`.
279+
///
280+
/// Returns true if this changed the matrix, and false otherwise.
281+
pub fn add(&mut self, row: R, column: C) -> bool {
282+
self.vector[row].insert(column)
283+
}
284+
285+
/// Do the bits from `row` contain `column`? Put another way, is
286+
/// the matrix cell at `(row, column)` true? Put yet another way,
287+
/// if the matrix represents (transitive) reachability, can
288+
/// `row` reach `column`?
289+
pub fn contains(&self, row: R, column: C) -> bool {
290+
self.vector[row].contains(column)
291+
}
292+
293+
/// Add the bits from row `read` to the bits from row `write`,
294+
/// return true if anything changed.
295+
///
296+
/// This is used when computing transitive reachability because if
297+
/// you have an edge `write -> read`, because in that case
298+
/// `write` can reach everything that `read` can (and
299+
/// potentially more).
300+
pub fn merge(&mut self, read: R, write: R) -> bool {
301+
let mut changed = false;
302+
303+
if read != write {
304+
let (bit_set_read, bit_set_write) = self.vector.pick2_mut(read, write);
305+
306+
for read_val in bit_set_read.iter() {
307+
changed = changed | bit_set_write.insert(read_val);
308+
}
309+
}
310+
311+
changed
312+
}
313+
314+
/// Iterates through all the columns set to true in a given row of
315+
/// the matrix.
316+
pub fn iter<'a>(&'a self, row: R) -> impl Iterator<Item = C> + 'a {
317+
self.vector[row].iter()
318+
}
319+
}
320+
321+
#[derive(Clone, Debug)]
322+
pub struct SparseBitSet<I: Idx> {
323+
chunk_bits: BTreeMap<u32, Word>,
324+
_marker: PhantomData<I>,
325+
}
326+
327+
#[derive(Copy, Clone)]
328+
pub struct SparseChunk<I> {
329+
key: u32,
330+
bits: Word,
331+
_marker: PhantomData<I>,
332+
}
333+
334+
impl<I: Idx> SparseChunk<I> {
335+
pub fn one(index: I) -> Self {
336+
let index = index.index();
337+
let key_usize = index / 128;
338+
let key = key_usize as u32;
339+
assert_eq!(key as usize, key_usize);
340+
SparseChunk {
341+
key,
342+
bits: 1 << (index % 128),
343+
_marker: PhantomData
344+
}
345+
}
346+
347+
pub fn any(&self) -> bool {
348+
self.bits != 0
349+
}
350+
351+
pub fn iter(&self) -> impl Iterator<Item = I> {
352+
let base = self.key as usize * 128;
353+
let mut bits = self.bits;
354+
(0..128).map(move |i| {
355+
let current_bits = bits;
356+
bits >>= 1;
357+
(i, current_bits)
358+
}).take_while(|&(_, bits)| bits != 0)
359+
.filter_map(move |(i, bits)| {
360+
if (bits & 1) != 0 {
361+
Some(I::new(base + i))
362+
} else {
363+
None
364+
}
365+
})
366+
}
367+
}
368+
369+
impl<I: Idx> SparseBitSet<I> {
370+
pub fn new() -> Self {
371+
SparseBitSet {
372+
chunk_bits: BTreeMap::new(),
373+
_marker: PhantomData
374+
}
375+
}
376+
377+
pub fn capacity(&self) -> usize {
378+
self.chunk_bits.len() * 128
379+
}
380+
381+
pub fn contains_chunk(&self, chunk: SparseChunk<I>) -> SparseChunk<I> {
382+
SparseChunk {
383+
bits: self.chunk_bits.get(&chunk.key).map_or(0, |bits| bits & chunk.bits),
384+
..chunk
385+
}
386+
}
387+
388+
pub fn insert_chunk(&mut self, chunk: SparseChunk<I>) -> SparseChunk<I> {
389+
if chunk.bits == 0 {
390+
return chunk;
391+
}
392+
let bits = self.chunk_bits.entry(chunk.key).or_insert(0);
393+
let old_bits = *bits;
394+
let new_bits = old_bits | chunk.bits;
395+
*bits = new_bits;
396+
let changed = new_bits ^ old_bits;
397+
SparseChunk {
398+
bits: changed,
399+
..chunk
400+
}
401+
}
402+
403+
pub fn remove_chunk(&mut self, chunk: SparseChunk<I>) -> SparseChunk<I> {
404+
if chunk.bits == 0 {
405+
return chunk;
406+
}
407+
let changed = match self.chunk_bits.entry(chunk.key) {
408+
Entry::Occupied(mut bits) => {
409+
let old_bits = *bits.get();
410+
let new_bits = old_bits & !chunk.bits;
411+
if new_bits == 0 {
412+
bits.remove();
413+
} else {
414+
bits.insert(new_bits);
415+
}
416+
new_bits ^ old_bits
417+
}
418+
Entry::Vacant(_) => 0
419+
};
420+
SparseChunk {
421+
bits: changed,
422+
..chunk
423+
}
424+
}
425+
426+
pub fn clear(&mut self) {
427+
self.chunk_bits.clear();
428+
}
429+
430+
pub fn chunks<'a>(&'a self) -> impl Iterator<Item = SparseChunk<I>> + 'a {
431+
self.chunk_bits.iter().map(|(&key, &bits)| {
432+
SparseChunk {
433+
key,
434+
bits,
435+
_marker: PhantomData
436+
}
437+
})
438+
}
439+
440+
pub fn contains(&self, index: I) -> bool {
441+
self.contains_chunk(SparseChunk::one(index)).any()
442+
}
443+
444+
pub fn insert(&mut self, index: I) -> bool {
445+
self.insert_chunk(SparseChunk::one(index)).any()
446+
}
447+
448+
pub fn remove(&mut self, index: I) -> bool {
449+
self.remove_chunk(SparseChunk::one(index)).any()
450+
}
451+
452+
pub fn iter<'a>(&'a self) -> impl Iterator<Item = I> + 'a {
453+
self.chunks().flat_map(|chunk| chunk.iter())
454+
}
455+
}
456+
260457
#[inline]
261458
fn words(elements: usize) -> usize {
262459
(elements + WORD_BITS - 1) / WORD_BITS

src/librustc_data_structures/indexed_vec.rs

+15
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,21 @@ impl<I: Idx, T> IndexVec<I, T> {
482482
pub fn get_mut(&mut self, index: I) -> Option<&mut T> {
483483
self.raw.get_mut(index.index())
484484
}
485+
486+
/// Return mutable references to two distinct elements, a and b. Panics if a == b.
487+
#[inline]
488+
pub fn pick2_mut(&mut self, a: I, b: I) -> (&mut T, &mut T) {
489+
let (ai, bi) = (a.index(), b.index());
490+
assert!(ai != bi);
491+
492+
if ai < bi {
493+
let (c1, c2) = self.raw.split_at_mut(bi);
494+
(&mut c1[ai], &mut c2[0])
495+
} else {
496+
let (c2, c1) = self.pick2_mut(b, a);
497+
(c1, c2)
498+
}
499+
}
485500
}
486501

487502
impl<I: Idx, T: Clone> IndexVec<I, T> {

src/librustc_mir/borrow_check/nll/region_infer/values.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use std::rc::Rc;
12-
use rustc_data_structures::bitvec::BitMatrix;
12+
use rustc_data_structures::bitvec::SparseBitMatrix;
1313
use rustc_data_structures::fx::FxHashMap;
1414
use rustc_data_structures::indexed_vec::Idx;
1515
use rustc_data_structures::indexed_vec::IndexVec;
@@ -132,7 +132,7 @@ impl RegionValueElements {
132132
}
133133

134134
/// A newtype for the integers that represent one of the possible
135-
/// elements in a region. These are the rows in the `BitMatrix` that
135+
/// elements in a region. These are the rows in the `SparseBitMatrix` that
136136
/// is used to store the values of all regions. They have the following
137137
/// convention:
138138
///
@@ -184,18 +184,18 @@ impl ToElementIndex for RegionElementIndex {
184184
}
185185

186186
/// Stores the values for a set of regions. These are stored in a
187-
/// compact `BitMatrix` representation, with one row per region
187+
/// compact `SparseBitMatrix` representation, with one row per region
188188
/// variable. The columns consist of either universal regions or
189189
/// points in the CFG.
190190
#[derive(Clone)]
191191
pub(super) struct RegionValues {
192192
elements: Rc<RegionValueElements>,
193-
matrix: BitMatrix,
193+
matrix: SparseBitMatrix<RegionVid, RegionElementIndex>,
194194

195195
/// If cause tracking is enabled, maps from a pair (r, e)
196196
/// consisting of a region `r` that contains some element `e` to
197197
/// the reason that the element is contained. There should be an
198-
/// entry for every bit set to 1 in `BitMatrix`.
198+
/// entry for every bit set to 1 in `SparseBitMatrix`.
199199
causes: Option<CauseMap>,
200200
}
201201

@@ -214,7 +214,8 @@ impl RegionValues {
214214

215215
Self {
216216
elements: elements.clone(),
217-
matrix: BitMatrix::new(num_region_variables, elements.num_elements()),
217+
matrix: SparseBitMatrix::new(RegionVid::new(num_region_variables),
218+
RegionElementIndex::new(elements.num_elements())),
218219
causes: if track_causes.0 {
219220
Some(CauseMap::default())
220221
} else {
@@ -238,7 +239,7 @@ impl RegionValues {
238239
where
239240
F: FnOnce(&CauseMap) -> Cause,
240241
{
241-
if self.matrix.add(r.index(), i.index()) {
242+
if self.matrix.add(r, i) {
242243
debug!("add(r={:?}, i={:?})", r, self.elements.to_element(i));
243244

244245
if let Some(causes) = &mut self.causes {
@@ -289,7 +290,7 @@ impl RegionValues {
289290
constraint_location: Location,
290291
constraint_span: Span,
291292
) -> bool {
292-
// We could optimize this by improving `BitMatrix::merge` so
293+
// We could optimize this by improving `SparseBitMatrix::merge` so
293294
// it does not always merge an entire row. That would
294295
// complicate causal tracking though.
295296
debug!(
@@ -315,7 +316,7 @@ impl RegionValues {
315316
/// True if the region `r` contains the given element.
316317
pub(super) fn contains<E: ToElementIndex>(&self, r: RegionVid, elem: E) -> bool {
317318
let i = self.elements.index(elem);
318-
self.matrix.contains(r.index(), i.index())
319+
self.matrix.contains(r, i)
319320
}
320321

321322
/// Iterate over the value of the region `r`, yielding up element
@@ -326,8 +327,8 @@ impl RegionValues {
326327
r: RegionVid,
327328
) -> impl Iterator<Item = RegionElementIndex> + 'a {
328329
self.matrix
329-
.iter(r.index())
330-
.map(move |i| RegionElementIndex::new(i))
330+
.iter(r)
331+
.map(move |i| i)
331332
}
332333

333334
/// Returns just the universal regions that are contained in a given region's value.

0 commit comments

Comments
 (0)