Skip to content

Commit efb3f11

Browse files
committed
Auto merge of #119499 - cjgillot:dtm-opt, r=nnethercote
Two small bitset optimisations
2 parents e21f4cd + 0adfe20 commit efb3f11

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

compiler/rustc_index/src/bit_set.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -1699,14 +1699,15 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
16991699
let (read_start, read_end) = self.range(read);
17001700
let (write_start, write_end) = self.range(write);
17011701
let words = &mut self.words[..];
1702-
let mut changed = false;
1702+
let mut changed = 0;
17031703
for (read_index, write_index) in iter::zip(read_start..read_end, write_start..write_end) {
17041704
let word = words[write_index];
17051705
let new_word = word | words[read_index];
17061706
words[write_index] = new_word;
1707-
changed |= word != new_word;
1707+
// See `bitwise` for the rationale.
1708+
changed |= word ^ new_word;
17081709
}
1709-
changed
1710+
changed != 0
17101711
}
17111712

17121713
/// Adds the bits from `with` to the bits from row `write`, and
@@ -1715,14 +1716,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
17151716
assert!(write.index() < self.num_rows);
17161717
assert_eq!(with.domain_size(), self.num_columns);
17171718
let (write_start, write_end) = self.range(write);
1718-
let mut changed = false;
1719-
for (read_index, write_index) in iter::zip(0..with.words.len(), write_start..write_end) {
1720-
let word = self.words[write_index];
1721-
let new_word = word | with.words[read_index];
1722-
self.words[write_index] = new_word;
1723-
changed |= word != new_word;
1724-
}
1725-
changed
1719+
bitwise(&mut self.words[write_start..write_end], &with.words, |a, b| a | b)
17261720
}
17271721

17281722
/// Sets every cell in `row` to true.

compiler/rustc_mir_transform/src/coroutine.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@ fn compute_storage_conflicts<'mir, 'tcx>(
942942
body,
943943
saved_locals: saved_locals,
944944
local_conflicts: BitMatrix::from_row_n(&ineligible_locals, body.local_decls.len()),
945+
eligible_storage_live: BitSet::new_empty(body.local_decls.len()),
945946
};
946947

947948
requires_storage.visit_reachable_with(body, &mut visitor);
@@ -978,6 +979,8 @@ struct StorageConflictVisitor<'mir, 'tcx, 's> {
978979
// FIXME(tmandry): Consider using sparse bitsets here once we have good
979980
// benchmarks for coroutines.
980981
local_conflicts: BitMatrix<Local, Local>,
982+
// We keep this bitset as a buffer to avoid reallocating memory.
983+
eligible_storage_live: BitSet<Local>,
981984
}
982985

983986
impl<'mir, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'mir, 'tcx, R>
@@ -1009,19 +1012,19 @@ impl<'mir, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'mir, 'tcx, R>
10091012
impl StorageConflictVisitor<'_, '_, '_> {
10101013
fn apply_state(&mut self, flow_state: &BitSet<Local>, loc: Location) {
10111014
// Ignore unreachable blocks.
1012-
if self.body.basic_blocks[loc.block].terminator().kind == TerminatorKind::Unreachable {
1015+
if let TerminatorKind::Unreachable = self.body.basic_blocks[loc.block].terminator().kind {
10131016
return;
10141017
}
10151018

1016-
let mut eligible_storage_live = flow_state.clone();
1017-
eligible_storage_live.intersect(&**self.saved_locals);
1019+
self.eligible_storage_live.clone_from(flow_state);
1020+
self.eligible_storage_live.intersect(&**self.saved_locals);
10181021

1019-
for local in eligible_storage_live.iter() {
1020-
self.local_conflicts.union_row_with(&eligible_storage_live, local);
1022+
for local in self.eligible_storage_live.iter() {
1023+
self.local_conflicts.union_row_with(&self.eligible_storage_live, local);
10211024
}
10221025

1023-
if eligible_storage_live.count() > 1 {
1024-
trace!("at {:?}, eligible_storage_live={:?}", loc, eligible_storage_live);
1026+
if self.eligible_storage_live.count() > 1 {
1027+
trace!("at {:?}, eligible_storage_live={:?}", loc, self.eligible_storage_live);
10251028
}
10261029
}
10271030
}

0 commit comments

Comments
 (0)