Skip to content

Commit b9ef914

Browse files
committed
index: add method for checking range on DenseBitSet
1 parent a88fc0e commit b9ef914

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

compiler/rustc_index/src/bit_set.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,28 @@ impl<T: Idx> DenseBitSet<T> {
234234
self.clear_excess_bits();
235235
}
236236

237+
/// Checks whether any bit in the given range is a 1.
238+
#[inline]
239+
pub fn contains_any(&self, elems: impl RangeBounds<T>) -> bool {
240+
let Some((start, end)) = inclusive_start_end(elems, self.domain_size) else {
241+
return true;
242+
};
243+
let (start_word_index, start_mask) = word_index_and_mask(start);
244+
let (end_word_index, end_mask) = word_index_and_mask(end);
245+
246+
if self.words[start_word_index] & (!start_mask | !(start_mask - 1)) != 0 {
247+
return true;
248+
}
249+
250+
let remaining = start_word_index + 1..end_word_index;
251+
if !remaining.is_empty() {
252+
self.words[remaining].iter().position(|&w| w != 0).is_some()
253+
|| self.words[end_word_index] & (end_mask | (end_mask - 1)) != 0
254+
} else {
255+
false
256+
}
257+
}
258+
237259
/// Returns `true` if the set has changed.
238260
#[inline]
239261
pub fn remove(&mut self, elem: T) -> bool {

src/tools/miri/src/alloc/isolated_alloc.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,8 @@ impl IsolatedAlloc {
141141
for offset in (0..page_size).step_by(layout.align()) {
142142
let offset_pinfo = offset / COMPRESSION_FACTOR;
143143
let size_pinfo = layout.size() / COMPRESSION_FACTOR;
144-
// DenseBitSet::contains() panics if the index is out of bounds
145-
if pinfo.domain_size() < offset_pinfo + size_pinfo {
146-
break;
147-
}
148-
// FIXME: is there a more efficient way to check whether the entire range is unset
149-
// in the bitset?
150-
let range_avail = !(offset_pinfo..offset_pinfo + size_pinfo).any(|i| pinfo.contains(i));
151-
if range_avail {
144+
// DenseBitSet::contains_any() returns true on OOB indexing
145+
if !pinfo.contains_any(offset_pinfo..offset_pinfo + size_pinfo) {
152146
pinfo.insert_range(offset_pinfo..offset_pinfo + size_pinfo);
153147
// SAFETY: We checked the available bytes after `idx` in the call
154148
// to `domain_size` above and asserted there are at least `idx +

0 commit comments

Comments
 (0)