Skip to content

Commit e0949ac

Browse files
authored
Rollup merge of #161331 - nnethercote:bitset-simplifications, r=Mark-Simulacrum
Bitset simplifications There are a few operations that aren't needed any more. Details in individual commits. r? @Mark-Simulacrum
2 parents 80bac8a + 2151612 commit e0949ac

2 files changed

Lines changed: 25 additions & 208 deletions

File tree

‎compiler/rustc_index/src/bit_set.rs‎

Lines changed: 25 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ const CHUNK_BITS: usize = CHUNK_WORDS * WORD_BITS; // 2048 bits
3434
type ChunkSize = u16;
3535
const _: () = assert!(CHUNK_BITS <= ChunkSize::MAX as usize);
3636

37-
pub trait BitRelations<Rhs> {
38-
fn union(&mut self, other: &Rhs) -> bool;
39-
fn subtract(&mut self, other: &Rhs) -> bool;
40-
fn intersect(&mut self, other: &Rhs) -> bool;
41-
}
42-
4337
#[inline]
4438
fn inclusive_start_end<T: Idx>(
4539
range: impl RangeBounds<T>,
@@ -63,37 +57,6 @@ fn inclusive_start_end<T: Idx>(
6357
Some((start, end))
6458
}
6559

66-
macro_rules! bit_relations_inherent_impls {
67-
() => {
68-
/// Sets `self = self | other` and returns `true` if `self` changed
69-
/// (i.e., if new bits were added).
70-
pub fn union<Rhs>(&mut self, other: &Rhs) -> bool
71-
where
72-
Self: BitRelations<Rhs>,
73-
{
74-
<Self as BitRelations<Rhs>>::union(self, other)
75-
}
76-
77-
/// Sets `self = self - other` and returns `true` if `self` changed.
78-
/// (i.e., if any bits were removed).
79-
pub fn subtract<Rhs>(&mut self, other: &Rhs) -> bool
80-
where
81-
Self: BitRelations<Rhs>,
82-
{
83-
<Self as BitRelations<Rhs>>::subtract(self, other)
84-
}
85-
86-
/// Sets `self = self & other` and return `true` if `self` changed.
87-
/// (i.e., if any bits were removed).
88-
pub fn intersect<Rhs>(&mut self, other: &Rhs) -> bool
89-
where
90-
Self: BitRelations<Rhs>,
91-
{
92-
<Self as BitRelations<Rhs>>::intersect(self, other)
93-
}
94-
};
95-
}
96-
9760
/// A fixed-size bitset type with a dense representation.
9861
///
9962
/// Note 1: Since this bitset is dense, if your domain is big, and/or relatively
@@ -320,12 +283,7 @@ impl<T: Idx> DenseBitSet<T> {
320283
None
321284
}
322285

323-
bit_relations_inherent_impls! {}
324-
325286
/// Sets `self = self | !other`.
326-
///
327-
/// FIXME: Incorporate this into [`BitRelations`] and fill out
328-
/// implementations for other bitset types, if needed.
329287
pub fn union_not(&mut self, other: &DenseBitSet<T>) {
330288
assert_eq!(self.domain_size, other.domain_size);
331289

@@ -339,21 +297,21 @@ impl<T: Idx> DenseBitSet<T> {
339297
// out-of-domain bits, so we need to clear them.
340298
self.clear_excess_bits();
341299
}
342-
}
343300

344-
// dense REL dense
345-
impl<T: Idx> BitRelations<DenseBitSet<T>> for DenseBitSet<T> {
346-
fn union(&mut self, other: &DenseBitSet<T>) -> bool {
301+
/// Returns true if `self` was modified.
302+
pub fn union(&mut self, other: &DenseBitSet<T>) -> bool {
347303
assert_eq!(self.domain_size, other.domain_size);
348304
update_words(&mut self.words, &other.words, |a, b| a | b)
349305
}
350306

351-
fn subtract(&mut self, other: &DenseBitSet<T>) -> bool {
307+
/// Returns true if `self` was modified.
308+
pub fn subtract(&mut self, other: &DenseBitSet<T>) -> bool {
352309
assert_eq!(self.domain_size, other.domain_size);
353310
update_words(&mut self.words, &other.words, |a, b| a & !b)
354311
}
355312

356-
fn intersect(&mut self, other: &DenseBitSet<T>) -> bool {
313+
/// Returns true if `self` was modified.
314+
pub fn intersect(&mut self, other: &DenseBitSet<T>) -> bool {
357315
assert_eq!(self.domain_size, other.domain_size);
358316
update_words(&mut self.words, &other.words, |a, b| a & b)
359317
}
@@ -761,10 +719,7 @@ impl<T: Idx> ChunkedBitSet<T> {
761719
}
762720
}
763721

764-
bit_relations_inherent_impls! {}
765-
}
766-
767-
impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
722+
/// Returns true if `self` was modified.
768723
fn union(&mut self, other: &ChunkedBitSet<T>) -> bool {
769724
assert_eq!(self.domain_size, other.domain_size);
770725

@@ -830,6 +785,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
830785
changed
831786
}
832787

788+
/// Returns true if `self` was modified.
833789
fn subtract(&mut self, other: &ChunkedBitSet<T>) -> bool {
834790
assert_eq!(self.domain_size, other.domain_size);
835791

@@ -906,56 +862,6 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
906862
}
907863
changed
908864
}
909-
910-
fn intersect(&mut self, other: &ChunkedBitSet<T>) -> bool {
911-
assert_eq!(self.domain_size, other.domain_size);
912-
913-
let mut changed = false;
914-
for (mut self_chunk, other_chunk) in self.chunks.iter_mut().zip(other.chunks.iter()) {
915-
match (&mut self_chunk, &other_chunk) {
916-
(Zeros { .. }, _) | (_, Ones { .. }) => {}
917-
(Ones { .. }, Zeros { .. } | Mixed { .. }) | (Mixed { .. }, Zeros { .. }) => {
918-
changed = true;
919-
*self_chunk = other_chunk.clone();
920-
}
921-
(
922-
Mixed {
923-
chunk_domain_size,
924-
ones_count: self_chunk_ones_count,
925-
words: self_chunk_words,
926-
},
927-
Mixed { words: other_chunk_words, .. },
928-
) => {
929-
// See `ChunkedBitSet::union` for details on what is happening here.
930-
let num_words = num_words(*chunk_domain_size as usize);
931-
let op = |a, b| a & b;
932-
if !would_modify_words(
933-
&self_chunk_words[0..num_words],
934-
&other_chunk_words[0..num_words],
935-
op,
936-
) {
937-
continue;
938-
}
939-
940-
let self_chunk_words = Rc::make_mut(self_chunk_words);
941-
let has_changed = update_words(
942-
&mut self_chunk_words[0..num_words],
943-
&other_chunk_words[0..num_words],
944-
op,
945-
);
946-
debug_assert!(has_changed);
947-
*self_chunk_ones_count =
948-
count_ones(&self_chunk_words[0..num_words]) as ChunkSize;
949-
if *self_chunk_ones_count == 0 {
950-
*self_chunk = Zeros { chunk_domain_size: *chunk_domain_size };
951-
}
952-
changed = true;
953-
}
954-
}
955-
}
956-
957-
changed
958-
}
959865
}
960866

961867
impl<T> Clone for ChunkedBitSet<T> {
@@ -1225,7 +1131,23 @@ impl<T: Idx> MixedBitSet<T> {
12251131
}
12261132
}
12271133

1228-
bit_relations_inherent_impls! {}
1134+
/// Returns true if `self` was modified.
1135+
pub fn union(&mut self, other: &MixedBitSet<T>) -> bool {
1136+
match (self, other) {
1137+
(MixedBitSet::Small(set), MixedBitSet::Small(other)) => set.union(other),
1138+
(MixedBitSet::Large(set), MixedBitSet::Large(other)) => set.union(other),
1139+
_ => panic!("MixedBitSet size mismatch"),
1140+
}
1141+
}
1142+
1143+
/// Returns true if `self` was modified.
1144+
pub fn subtract(&mut self, other: &MixedBitSet<T>) -> bool {
1145+
match (self, other) {
1146+
(MixedBitSet::Small(set), MixedBitSet::Small(other)) => set.subtract(other),
1147+
(MixedBitSet::Large(set), MixedBitSet::Large(other)) => set.subtract(other),
1148+
_ => panic!("MixedBitSet size mismatch"),
1149+
}
1150+
}
12291151
}
12301152

12311153
impl<T> Clone for MixedBitSet<T> {
@@ -1249,28 +1171,6 @@ impl<T> Clone for MixedBitSet<T> {
12491171
}
12501172
}
12511173

1252-
impl<T: Idx> BitRelations<MixedBitSet<T>> for MixedBitSet<T> {
1253-
fn union(&mut self, other: &MixedBitSet<T>) -> bool {
1254-
match (self, other) {
1255-
(MixedBitSet::Small(set), MixedBitSet::Small(other)) => set.union(other),
1256-
(MixedBitSet::Large(set), MixedBitSet::Large(other)) => set.union(other),
1257-
_ => panic!("MixedBitSet size mismatch"),
1258-
}
1259-
}
1260-
1261-
fn subtract(&mut self, other: &MixedBitSet<T>) -> bool {
1262-
match (self, other) {
1263-
(MixedBitSet::Small(set), MixedBitSet::Small(other)) => set.subtract(other),
1264-
(MixedBitSet::Large(set), MixedBitSet::Large(other)) => set.subtract(other),
1265-
_ => panic!("MixedBitSet size mismatch"),
1266-
}
1267-
}
1268-
1269-
fn intersect(&mut self, _other: &MixedBitSet<T>) -> bool {
1270-
unimplemented!("implement if/when necessary");
1271-
}
1272-
}
1273-
12741174
impl<T: Idx> fmt::Debug for MixedBitSet<T> {
12751175
fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
12761176
match self {
@@ -1701,45 +1601,6 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
17011601
pub fn row(&self, row: R) -> Option<&DenseBitSet<C>> {
17021602
self.rows.get(row)?.as_ref()
17031603
}
1704-
1705-
/// Intersects `row` with `set`. `set` can be either `DenseBitSet` or
1706-
/// `ChunkedBitSet`. Has no effect if `row` does not exist.
1707-
///
1708-
/// Returns true if the row was changed.
1709-
pub fn intersect_row<Set>(&mut self, row: R, set: &Set) -> bool
1710-
where
1711-
DenseBitSet<C>: BitRelations<Set>,
1712-
{
1713-
match self.rows.get_mut(row) {
1714-
Some(Some(row)) => row.intersect(set),
1715-
_ => false,
1716-
}
1717-
}
1718-
1719-
/// Subtracts `set` from `row`. `set` can be either `DenseBitSet` or
1720-
/// `ChunkedBitSet`. Has no effect if `row` does not exist.
1721-
///
1722-
/// Returns true if the row was changed.
1723-
pub fn subtract_row<Set>(&mut self, row: R, set: &Set) -> bool
1724-
where
1725-
DenseBitSet<C>: BitRelations<Set>,
1726-
{
1727-
match self.rows.get_mut(row) {
1728-
Some(Some(row)) => row.subtract(set),
1729-
_ => false,
1730-
}
1731-
}
1732-
1733-
/// Unions `row` with `set`. `set` can be either `DenseBitSet` or
1734-
/// `ChunkedBitSet`.
1735-
///
1736-
/// Returns true if the row was changed.
1737-
pub fn union_row<Set>(&mut self, row: R, set: &Set) -> bool
1738-
where
1739-
DenseBitSet<C>: BitRelations<Set>,
1740-
{
1741-
self.ensure_row(row).union(set)
1742-
}
17431604
}
17441605

17451606
#[inline]

‎compiler/rustc_index/src/bit_set/tests.rs‎

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,6 @@ fn chunked_bulk_ops() {
403403
op_fn: ChunkedBitSet::subtract,
404404
spec_fn: |fizz, buzz, i| fizz(i) && !buzz(i),
405405
},
406-
ChunkedBulkOp {
407-
name: "intersect",
408-
op_fn: ChunkedBitSet::intersect,
409-
spec_fn: |fizz, buzz, i| fizz(i) && buzz(i),
410-
},
411406
];
412407

413408
let domain_sizes = [
@@ -687,17 +682,6 @@ fn sparse_matrix_operations() {
687682
matrix.insert(2, 99);
688683
matrix.insert(4, 0);
689684

690-
let mut disjoint: DenseBitSet<usize> = DenseBitSet::new_empty(100);
691-
disjoint.insert(33);
692-
693-
let mut superset = DenseBitSet::new_empty(100);
694-
superset.insert(22);
695-
superset.insert(75);
696-
superset.insert(33);
697-
698-
let mut subset = DenseBitSet::new_empty(100);
699-
subset.insert(22);
700-
701685
// SparseBitMatrix::remove
702686
{
703687
let mut matrix = matrix.clone();
@@ -715,34 +699,6 @@ fn sparse_matrix_operations() {
715699
matrix.clear(0);
716700
assert!(matrix.row(0).is_none());
717701
}
718-
719-
// SparseBitMatrix::intersect_row
720-
{
721-
let mut matrix = matrix.clone();
722-
assert!(!matrix.intersect_row(3, &superset));
723-
assert!(matrix.intersect_row(3, &subset));
724-
matrix.intersect_row(0, &disjoint);
725-
assert!(matrix.row(0).is_none());
726-
}
727-
728-
// SparseBitMatrix::subtract_row
729-
{
730-
let mut matrix = matrix.clone();
731-
assert!(!matrix.subtract_row(3, &disjoint));
732-
assert!(matrix.subtract_row(3, &subset));
733-
assert!(matrix.subtract_row(3, &superset));
734-
matrix.intersect_row(0, &disjoint);
735-
assert!(matrix.row(0).is_none());
736-
}
737-
738-
// SparseBitMatrix::union_row
739-
{
740-
let mut matrix = matrix.clone();
741-
assert!(!matrix.union_row(3, &subset));
742-
assert!(matrix.union_row(3, &disjoint));
743-
matrix.union_row(0, &disjoint);
744-
assert!(matrix.row(0).is_some());
745-
}
746702
}
747703

748704
#[test]

0 commit comments

Comments
 (0)