Skip to content

Commit c461f7a

Browse files
committed
Auto merge of #98841 - Kobzol:hir-validator-bitset, r=cjgillot
Use a bitset instead of a hash map in HIR ID validator The hashset insertion was slightly hot in incr patched runs, but it seems unnecessary to use a hashset here, as it just checks that a dense set of N integers was seen. I'm not sure if it's possible to know the amount of items beforehand to preallocate the bitset? I suppose not.
2 parents e78e747 + 928c172 commit c461f7a

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

compiler/rustc_index/src/bit_set.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,16 @@ impl<T: Idx> GrowableBitSet<T> {
15461546
let (word_index, mask) = word_index_and_mask(elem);
15471547
self.bit_set.words.get(word_index).map_or(false, |word| (word & mask) != 0)
15481548
}
1549+
1550+
#[inline]
1551+
pub fn iter(&self) -> BitIter<'_, T> {
1552+
self.bit_set.iter()
1553+
}
1554+
1555+
#[inline]
1556+
pub fn len(&self) -> usize {
1557+
self.bit_set.count()
1558+
}
15491559
}
15501560

15511561
impl<T: Idx> From<BitSet<T>> for GrowableBitSet<T> {

compiler/rustc_passes/src/hir_id_validator.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use rustc_data_structures::fx::FxHashSet;
21
use rustc_data_structures::sync::Lock;
32
use rustc_hir as hir;
43
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
54
use rustc_hir::intravisit;
65
use rustc_hir::{HirId, ItemLocalId};
6+
use rustc_index::bit_set::GrowableBitSet;
77
use rustc_middle::hir::map::Map;
88
use rustc_middle::hir::nested_filter;
99
use rustc_middle::ty::TyCtxt;
@@ -15,32 +15,35 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
1515
crate::hir_stats::print_hir_stats(tcx);
1616
}
1717

18-
let errors = Lock::new(Vec::new());
19-
let hir_map = tcx.hir();
18+
#[cfg(debug_assertions)]
19+
{
20+
let errors = Lock::new(Vec::new());
21+
let hir_map = tcx.hir();
2022

21-
hir_map.par_for_each_module(|module_id| {
22-
let mut v = HirIdValidator {
23-
hir_map,
24-
owner: None,
25-
hir_ids_seen: Default::default(),
26-
errors: &errors,
27-
};
23+
hir_map.par_for_each_module(|module_id| {
24+
let mut v = HirIdValidator {
25+
hir_map,
26+
owner: None,
27+
hir_ids_seen: Default::default(),
28+
errors: &errors,
29+
};
2830

29-
tcx.hir().deep_visit_item_likes_in_module(module_id, &mut v);
30-
});
31+
tcx.hir().deep_visit_item_likes_in_module(module_id, &mut v);
32+
});
3133

32-
let errors = errors.into_inner();
34+
let errors = errors.into_inner();
3335

34-
if !errors.is_empty() {
35-
let message = errors.iter().fold(String::new(), |s1, s2| s1 + "\n" + s2);
36-
tcx.sess.delay_span_bug(rustc_span::DUMMY_SP, &message);
36+
if !errors.is_empty() {
37+
let message = errors.iter().fold(String::new(), |s1, s2| s1 + "\n" + s2);
38+
tcx.sess.delay_span_bug(rustc_span::DUMMY_SP, &message);
39+
}
3740
}
3841
}
3942

4043
struct HirIdValidator<'a, 'hir> {
4144
hir_map: Map<'hir>,
4245
owner: Option<LocalDefId>,
43-
hir_ids_seen: FxHashSet<ItemLocalId>,
46+
hir_ids_seen: GrowableBitSet<ItemLocalId>,
4447
errors: &'a Lock<Vec<String>>,
4548
}
4649

@@ -80,7 +83,7 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
8083
if max != self.hir_ids_seen.len() - 1 {
8184
// Collect the missing ItemLocalIds
8285
let missing: Vec<_> = (0..=max as u32)
83-
.filter(|&i| !self.hir_ids_seen.contains(&ItemLocalId::from_u32(i)))
86+
.filter(|&i| !self.hir_ids_seen.contains(ItemLocalId::from_u32(i)))
8487
.collect();
8588

8689
// Try to map those to something more useful
@@ -106,7 +109,7 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
106109
missing_items,
107110
self.hir_ids_seen
108111
.iter()
109-
.map(|&local_id| HirId { owner, local_id })
112+
.map(|local_id| HirId { owner, local_id })
110113
.map(|h| format!("({:?} {})", h, self.hir_map.node_to_string(h)))
111114
.collect::<Vec<_>>()
112115
)

0 commit comments

Comments
 (0)