Skip to content

Commit d7aae93

Browse files
committed
(DO NOT MERGE) Extract insert and remove (tweaked)
1 parent a5965da commit d7aae93

2 files changed

Lines changed: 52 additions & 29 deletions

File tree

compiler/rustc_index/src/bit_set.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,11 @@ impl<T: Idx> DenseBitSet<T> {
185185
self.words.iter().all(|a| *a == 0)
186186
}
187187

188-
/// Insert `elem`. Returns whether the set has changed.
188+
/// Inserts `value` into the set, and returns true if the set has changed
189+
/// (i.e. the set did not contain the value).
189190
#[inline]
190-
pub fn insert(&mut self, elem: T) -> bool {
191-
assert!(
192-
elem.index() < self.domain_size,
193-
"inserting element at index {} but domain size is {}",
194-
elem.index(),
195-
self.domain_size,
196-
);
197-
let (word_index, mask) = word_index_and_mask(elem);
198-
let word_ref = &mut self.words[word_index];
199-
let word = *word_ref;
200-
let new_word = word | mask;
201-
*word_ref = new_word;
202-
new_word != word
191+
pub fn insert(&mut self, value: T) -> bool {
192+
raw::insert(self.domain_size, &mut self.words, value.index())
203193
}
204194

205195
#[inline]
@@ -223,16 +213,11 @@ impl<T: Idx> DenseBitSet<T> {
223213
raw::contains_any(self.domain_size, &self.words, (start, end))
224214
}
225215

226-
/// Returns `true` if the set has changed.
216+
/// Removes `value` from the set, and returns true if the set has changed
217+
/// (i.e. the set did contain the value).
227218
#[inline]
228-
pub fn remove(&mut self, elem: T) -> bool {
229-
assert!(elem.index() < self.domain_size);
230-
let (word_index, mask) = word_index_and_mask(elem);
231-
let word_ref = &mut self.words[word_index];
232-
let word = *word_ref;
233-
let new_word = word & !mask;
234-
*word_ref = new_word;
235-
new_word != word
219+
pub fn remove(&mut self, value: T) -> bool {
220+
raw::remove(self.domain_size, &mut self.words, value.index())
236221
}
237222

238223
/// Iterates over the indices of set bits in a sorted order.
@@ -1608,12 +1593,9 @@ fn num_chunks<T: Idx>(domain_size: T) -> usize {
16081593
domain_size.index().div_ceil(CHUNK_BITS)
16091594
}
16101595

1611-
#[inline]
1596+
#[inline(always)]
16121597
fn word_index_and_mask<T: Idx>(elem: T) -> (usize, Word) {
1613-
let elem = elem.index();
1614-
let word_index = elem / WORD_BITS;
1615-
let mask = 1 << (elem % WORD_BITS);
1616-
(word_index, mask)
1598+
raw::word_index_and_mask(elem.index())
16171599
}
16181600

16191601
#[inline]

compiler/rustc_index/src/bit_set/raw.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::ops::Bound;
22
use std::slice;
33

4-
use crate::bit_set::{WORD_BITS, Word, inclusive_start_end, max_bit, word_index_and_mask};
4+
use crate::bit_set::{WORD_BITS, Word, inclusive_start_end, max_bit};
55

66
#[inline]
77
pub(crate) fn contains_any(
@@ -67,6 +67,40 @@ pub(crate) fn last_set_in(
6767
None
6868
}
6969

70+
#[inline(always)]
71+
pub(crate) fn insert(domain_size: usize, words: &mut [Word], index: usize) -> bool {
72+
if index >= domain_size {
73+
index_not_in_domain("inserting", index, domain_size);
74+
}
75+
76+
let (word_index, mask) = word_index_and_mask(index);
77+
modify_word(&mut words[word_index], |w| w | mask)
78+
}
79+
80+
#[inline(always)]
81+
pub(crate) fn remove(domain_size: usize, words: &mut [Word], index: usize) -> bool {
82+
if index >= domain_size {
83+
index_not_in_domain("removing", index, domain_size);
84+
}
85+
86+
let (word_index, mask) = word_index_and_mask(index);
87+
modify_word(&mut words[word_index], |w| w & !mask)
88+
}
89+
90+
#[cold]
91+
#[inline(never)]
92+
fn index_not_in_domain(verb: &'static str, index: usize, domain_size: usize) -> ! {
93+
panic!("{verb} at index {index} but domain size is {domain_size}");
94+
}
95+
96+
/// Updates a `&mut Word` using the given function, and returns true if the value was changed.
97+
#[inline(always)]
98+
fn modify_word(word: &mut Word, modify_fn: impl Fn(Word) -> Word) -> bool {
99+
let old = *word;
100+
*word = modify_fn(old);
101+
old != *word
102+
}
103+
70104
#[inline]
71105
pub(crate) fn insert_range(
72106
domain_size: usize,
@@ -149,3 +183,10 @@ impl<'a> Iterator for RawBitIter<'a> {
149183
}
150184
}
151185
}
186+
187+
#[inline(always)]
188+
pub(crate) fn word_index_and_mask(bit_index: usize) -> (usize, Word) {
189+
let word_index = bit_index / WORD_BITS;
190+
let mask = 1 << (bit_index % WORD_BITS);
191+
(word_index, mask)
192+
}

0 commit comments

Comments
 (0)