Skip to content

Commit 0d883cb

Browse files
authored
Rollup merge of rust-lang#57232 - Zoxc:par-collector-misc, r=michaelwoerister
Parallelize and optimize parts of HIR map creation r? @michaelwoerister
2 parents 1d029c6 + cbb5a00 commit 0d883cb

File tree

4 files changed

+57
-44
lines changed

4 files changed

+57
-44
lines changed

src/librustc/hir/map/collector.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_data_structures::svh::Svh;
66
use ich::Fingerprint;
77
use middle::cstore::CrateStore;
88
use session::CrateDisambiguator;
9+
use session::Session;
910
use std::iter::repeat;
1011
use syntax::ast::{NodeId, CRATE_NODE_ID};
1112
use syntax::source_map::SourceMap;
@@ -92,11 +93,11 @@ where
9293
}
9394

9495
impl<'a, 'hir> NodeCollector<'a, 'hir> {
95-
pub(super) fn root(krate: &'hir Crate,
96+
pub(super) fn root(sess: &'a Session,
97+
krate: &'hir Crate,
9698
dep_graph: &'a DepGraph,
9799
definitions: &'a definitions::Definitions,
98-
mut hcx: StableHashingContext<'a>,
99-
source_map: &'a SourceMap)
100+
mut hcx: StableHashingContext<'a>)
100101
-> NodeCollector<'a, 'hir> {
101102
let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX);
102103

@@ -141,8 +142,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
141142

142143
let mut collector = NodeCollector {
143144
krate,
144-
source_map,
145-
map: vec![],
145+
source_map: sess.source_map(),
146+
map: repeat(None).take(sess.current_node_id_count()).collect(),
146147
parent_node: CRATE_NODE_ID,
147148
current_signature_dep_index: root_mod_sig_dep_index,
148149
current_full_dep_index: root_mod_full_dep_index,
@@ -219,10 +220,6 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
219220

220221
fn insert_entry(&mut self, id: NodeId, entry: Entry<'hir>) {
221222
debug!("hir_map: {:?} => {:?}", id, entry);
222-
let len = self.map.len();
223-
if id.as_usize() >= len {
224-
self.map.extend(repeat(None).take(id.as_usize() - len + 1));
225-
}
226223
self.map[id.as_usize()] = Some(entry);
227224
}
228225

src/librustc/hir/map/hir_id_validator.rs

+26-19
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@ use hir::{self, intravisit, HirId, ItemLocalId};
33
use syntax::ast::NodeId;
44
use hir::itemlikevisit::ItemLikeVisitor;
55
use rustc_data_structures::fx::FxHashMap;
6+
use rustc_data_structures::sync::{Lock, ParallelIterator, par_iter};
67

78
pub fn check_crate<'hir>(hir_map: &hir::map::Map<'hir>) {
8-
let mut outer_visitor = OuterVisitor {
9-
hir_map,
10-
errors: vec![],
11-
};
12-
139
hir_map.dep_graph.assert_ignored();
1410

15-
hir_map.krate().visit_all_item_likes(&mut outer_visitor);
16-
if !outer_visitor.errors.is_empty() {
17-
let message = outer_visitor
18-
.errors
11+
let errors = Lock::new(Vec::new());
12+
13+
par_iter(&hir_map.krate().modules).for_each(|(module_id, _)| {
14+
hir_map.visit_item_likes_in_module(hir_map.local_def_id(*module_id), &mut OuterVisitor {
15+
hir_map,
16+
errors: &errors,
17+
});
18+
});
19+
20+
let errors = errors.into_inner();
21+
22+
if !errors.is_empty() {
23+
let message = errors
1924
.iter()
2025
.fold(String::new(), |s1, s2| s1 + "\n" + s2);
2126
bug!("{}", message);
@@ -26,12 +31,12 @@ struct HirIdValidator<'a, 'hir: 'a> {
2631
hir_map: &'a hir::map::Map<'hir>,
2732
owner_def_index: Option<DefIndex>,
2833
hir_ids_seen: FxHashMap<ItemLocalId, NodeId>,
29-
errors: Vec<String>,
34+
errors: &'a Lock<Vec<String>>,
3035
}
3136

3237
struct OuterVisitor<'a, 'hir: 'a> {
3338
hir_map: &'a hir::map::Map<'hir>,
34-
errors: Vec<String>,
39+
errors: &'a Lock<Vec<String>>,
3540
}
3641

3742
impl<'a, 'hir: 'a> OuterVisitor<'a, 'hir> {
@@ -42,7 +47,7 @@ impl<'a, 'hir: 'a> OuterVisitor<'a, 'hir> {
4247
hir_map,
4348
owner_def_index: None,
4449
hir_ids_seen: Default::default(),
45-
errors: Vec::new(),
50+
errors: self.errors,
4651
}
4752
}
4853
}
@@ -51,23 +56,25 @@ impl<'a, 'hir: 'a> ItemLikeVisitor<'hir> for OuterVisitor<'a, 'hir> {
5156
fn visit_item(&mut self, i: &'hir hir::Item) {
5257
let mut inner_visitor = self.new_inner_visitor(self.hir_map);
5358
inner_visitor.check(i.id, |this| intravisit::walk_item(this, i));
54-
self.errors.extend(inner_visitor.errors.drain(..));
5559
}
5660

5761
fn visit_trait_item(&mut self, i: &'hir hir::TraitItem) {
5862
let mut inner_visitor = self.new_inner_visitor(self.hir_map);
5963
inner_visitor.check(i.id, |this| intravisit::walk_trait_item(this, i));
60-
self.errors.extend(inner_visitor.errors.drain(..));
6164
}
6265

6366
fn visit_impl_item(&mut self, i: &'hir hir::ImplItem) {
6467
let mut inner_visitor = self.new_inner_visitor(self.hir_map);
6568
inner_visitor.check(i.id, |this| intravisit::walk_impl_item(this, i));
66-
self.errors.extend(inner_visitor.errors.drain(..));
6769
}
6870
}
6971

7072
impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> {
73+
#[cold]
74+
#[inline(never)]
75+
fn error(&self, f: impl FnOnce() -> String) {
76+
self.errors.lock().push(f());
77+
}
7178

7279
fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self,
7380
node_id: NodeId,
@@ -119,7 +126,7 @@ impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> {
119126
local_id,
120127
self.hir_map.node_to_string(node_id)));
121128
}
122-
self.errors.push(format!(
129+
self.error(|| format!(
123130
"ItemLocalIds not assigned densely in {}. \
124131
Max ItemLocalId = {}, missing IDs = {:?}; seens IDs = {:?}",
125132
self.hir_map.def_path(DefId::local(owner_def_index)).to_string_no_crate(),
@@ -145,14 +152,14 @@ impl<'a, 'hir: 'a> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
145152
let stable_id = self.hir_map.definitions().node_to_hir_id[node_id];
146153

147154
if stable_id == hir::DUMMY_HIR_ID {
148-
self.errors.push(format!("HirIdValidator: No HirId assigned for NodeId {}: {:?}",
155+
self.error(|| format!("HirIdValidator: No HirId assigned for NodeId {}: {:?}",
149156
node_id,
150157
self.hir_map.node_to_string(node_id)));
151158
return;
152159
}
153160

154161
if owner != stable_id.owner {
155-
self.errors.push(format!(
162+
self.error(|| format!(
156163
"HirIdValidator: The recorded owner of {} is {} instead of {}",
157164
self.hir_map.node_to_string(node_id),
158165
self.hir_map.def_path(DefId::local(stable_id.owner)).to_string_no_crate(),
@@ -161,7 +168,7 @@ impl<'a, 'hir: 'a> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
161168

162169
if let Some(prev) = self.hir_ids_seen.insert(stable_id.local_id, node_id) {
163170
if prev != node_id {
164-
self.errors.push(format!(
171+
self.error(|| format!(
165172
"HirIdValidator: Same HirId {}/{} assigned for nodes {} and {}",
166173
self.hir_map.def_path(DefId::local(stable_id.owner)).to_string_no_crate(),
167174
stable_id.local_id.as_usize(),

src/librustc/hir/map/mod.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ use dep_graph::{DepGraph, DepNode, DepKind, DepNodeIndex};
77

88
use hir::def_id::{CRATE_DEF_INDEX, DefId, LocalDefId, DefIndexAddressSpace};
99

10-
use middle::cstore::CrateStore;
10+
use middle::cstore::CrateStoreDyn;
1111

1212
use rustc_target::spec::abi::Abi;
1313
use rustc_data_structures::svh::Svh;
14+
use rustc_data_structures::sync::join;
1415
use syntax::ast::{self, Name, NodeId, CRATE_NODE_ID};
1516
use syntax::source_map::Spanned;
1617
use syntax::ext::base::MacroKind;
@@ -20,6 +21,7 @@ use hir::*;
2021
use hir::itemlikevisit::ItemLikeVisitor;
2122
use hir::print::Nested;
2223
use util::nodemap::FxHashMap;
24+
use util::common::time;
2325

2426
use std::io;
2527
use std::result::Result::Err;
@@ -1045,26 +1047,32 @@ impl Named for TraitItem { fn name(&self) -> Name { self.ident.name } }
10451047
impl Named for ImplItem { fn name(&self) -> Name { self.ident.name } }
10461048

10471049
pub fn map_crate<'hir>(sess: &::session::Session,
1048-
cstore: &dyn CrateStore,
1049-
forest: &'hir mut Forest,
1050+
cstore: &CrateStoreDyn,
1051+
forest: &'hir Forest,
10501052
definitions: &'hir Definitions)
10511053
-> Map<'hir> {
1052-
let (map, crate_hash) = {
1054+
let ((map, crate_hash), hir_to_node_id) = join(|| {
10531055
let hcx = ::ich::StableHashingContext::new(sess, &forest.krate, definitions, cstore);
10541056

1055-
let mut collector = NodeCollector::root(&forest.krate,
1057+
let mut collector = NodeCollector::root(sess,
1058+
&forest.krate,
10561059
&forest.dep_graph,
10571060
&definitions,
1058-
hcx,
1059-
sess.source_map());
1061+
hcx);
10601062
intravisit::walk_crate(&mut collector, &forest.krate);
10611063

10621064
let crate_disambiguator = sess.local_crate_disambiguator();
10631065
let cmdline_args = sess.opts.dep_tracking_hash();
1064-
collector.finalize_and_compute_crate_hash(crate_disambiguator,
1065-
cstore,
1066-
cmdline_args)
1067-
};
1066+
collector.finalize_and_compute_crate_hash(
1067+
crate_disambiguator,
1068+
cstore,
1069+
cmdline_args
1070+
)
1071+
}, || {
1072+
// Build the reverse mapping of `node_to_hir_id`.
1073+
definitions.node_to_hir_id.iter_enumerated()
1074+
.map(|(node_id, &hir_id)| (hir_id, node_id)).collect()
1075+
});
10681076

10691077
if log_enabled!(::log::Level::Debug) {
10701078
// This only makes sense for ordered stores; note the
@@ -1078,10 +1086,6 @@ pub fn map_crate<'hir>(sess: &::session::Session,
10781086
entries, vector_length, (entries as f64 / vector_length as f64) * 100.);
10791087
}
10801088

1081-
// Build the reverse mapping of `node_to_hir_id`.
1082-
let hir_to_node_id = definitions.node_to_hir_id.iter_enumerated()
1083-
.map(|(node_id, &hir_id)| (hir_id, node_id)).collect();
1084-
10851089
let map = Map {
10861090
forest,
10871091
dep_graph: forest.dep_graph.clone(),
@@ -1091,7 +1095,9 @@ pub fn map_crate<'hir>(sess: &::session::Session,
10911095
definitions,
10921096
};
10931097

1094-
hir_id_validator::check_crate(&map);
1098+
time(sess, "validate hir map", || {
1099+
hir_id_validator::check_crate(&map);
1100+
});
10951101

10961102
map
10971103
}

src/librustc/session/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ impl Session {
407407
pub fn next_node_id(&self) -> NodeId {
408408
self.reserve_node_ids(1)
409409
}
410+
pub(crate) fn current_node_id_count(&self) -> usize {
411+
self.next_node_id.get().as_u32() as usize
412+
}
410413
pub fn diagnostic<'a>(&'a self) -> &'a errors::Handler {
411414
&self.parse_sess.span_diagnostic
412415
}

0 commit comments

Comments
 (0)