Skip to content

Commit 3d21124

Browse files
committed
Auto merge of #59042 - ljedrz:HirIdification_rework_map, r=Zoxc
HirIdification: rework Map The next iteration of HirIdification (#57578). - remove `NodeId` from `Entry` - change `Map::map` to an `FxHashMap<HirId, Entry>` - base the `NodeId` `Map` methods on `HirId` ones (reverses the current state) - HirIdify `librustdoc` a little bit (some `NodeId` `Map` methods were converted to work on `HirId`s) The second change might have performance implications, so I'd do a perf run to be sure it's fine; it simplifies the codebase and shouldn't have an impact as long as the `Map` searches are cached (which is now possible thanks to using `HirId`s). r? @Zoxc
2 parents e305df1 + 37954df commit 3d21124

File tree

6 files changed

+184
-205
lines changed

6 files changed

+184
-205
lines changed

src/librustc/hir/map/collector.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::ich::Fingerprint;
88
use crate::middle::cstore::CrateStore;
99
use crate::session::CrateDisambiguator;
1010
use crate::session::Session;
11-
use std::iter::repeat;
12-
use syntax::ast::{NodeId, CRATE_NODE_ID};
11+
use crate::util::nodemap::FxHashMap;
12+
use syntax::ast::NodeId;
1313
use syntax::source_map::SourceMap;
1414
use syntax_pos::Span;
1515

@@ -25,7 +25,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
2525
source_map: &'a SourceMap,
2626

2727
/// The node map
28-
map: Vec<Option<Entry<'hir>>>,
28+
map: FxHashMap<HirId, Entry<'hir>>,
2929
/// The parent of this node
3030
parent_node: hir::HirId,
3131

@@ -145,7 +145,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
145145
let mut collector = NodeCollector {
146146
krate,
147147
source_map: sess.source_map(),
148-
map: repeat(None).take(sess.current_node_id_count()).collect(),
148+
map: FxHashMap::with_capacity_and_hasher(sess.current_node_id_count(),
149+
Default::default()),
149150
parent_node: hir::CRATE_HIR_ID,
150151
current_signature_dep_index: root_mod_sig_dep_index,
151152
current_full_dep_index: root_mod_full_dep_index,
@@ -157,9 +158,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
157158
hcx,
158159
hir_body_nodes,
159160
};
160-
collector.insert_entry(CRATE_NODE_ID, Entry {
161-
parent: CRATE_NODE_ID,
162-
parent_hir: hir::CRATE_HIR_ID,
161+
collector.insert_entry(hir::CRATE_HIR_ID, Entry {
162+
parent: hir::CRATE_HIR_ID,
163163
dep_node: root_mod_sig_dep_index,
164164
node: Node::Crate,
165165
});
@@ -171,7 +171,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
171171
crate_disambiguator: CrateDisambiguator,
172172
cstore: &dyn CrateStore,
173173
commandline_args_hash: u64)
174-
-> (Vec<Option<Entry<'hir>>>, Svh)
174+
-> (FxHashMap<HirId, Entry<'hir>>, Svh)
175175
{
176176
self.hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
177177

@@ -222,15 +222,14 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
222222
(self.map, svh)
223223
}
224224

225-
fn insert_entry(&mut self, id: NodeId, entry: Entry<'hir>) {
225+
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>) {
226226
debug!("hir_map: {:?} => {:?}", id, entry);
227-
self.map[id.as_usize()] = Some(entry);
227+
self.map.insert(id, entry);
228228
}
229229

230230
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
231231
let entry = Entry {
232-
parent: self.hir_to_node_id[&self.parent_node],
233-
parent_hir: self.parent_node,
232+
parent: self.parent_node,
234233
dep_node: if self.currently_in_body {
235234
self.current_full_dep_index
236235
} else {
@@ -239,12 +238,11 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
239238
node,
240239
};
241240

242-
let node_id = self.hir_to_node_id[&hir_id];
243-
244241
// Make sure that the DepNode of some node coincides with the HirId
245242
// owner of that node.
246243
if cfg!(debug_assertions) {
247-
assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id);
244+
let node_id = self.hir_to_node_id[&hir_id];
245+
assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id);
248246

249247
if hir_id.owner != self.current_dep_node_owner {
250248
let node_str = match self.definitions.opt_def_index(node_id) {
@@ -277,7 +275,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
277275
}
278276
}
279277

280-
self.insert_entry(node_id, entry);
278+
self.insert_entry(hir_id, entry);
281279
}
282280

283281
fn with_parent<F: FnOnce(&mut Self)>(

0 commit comments

Comments
 (0)