Skip to content

Commit

Permalink
Auto merge of #59042 - ljedrz:HirIdification_rework_map, r=<try>
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Mar 9, 2019
2 parents c9f8304 + 12c55fd commit 920010c
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 200 deletions.
29 changes: 13 additions & 16 deletions src/librustc/hir/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::ich::Fingerprint;
use crate::middle::cstore::CrateStore;
use crate::session::CrateDisambiguator;
use crate::session::Session;
use std::iter::repeat;
use syntax::ast::{NodeId, CRATE_NODE_ID};
use crate::util::nodemap::FxHashMap;
use syntax::ast::NodeId;
use syntax::source_map::SourceMap;
use syntax_pos::Span;

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

/// The node map
map: Vec<Option<Entry<'hir>>>,
map: FxHashMap<HirId, Entry<'hir>>,
/// The parent of this node
parent_node: hir::HirId,

Expand Down Expand Up @@ -146,7 +146,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
let mut collector = NodeCollector {
krate,
source_map: sess.source_map(),
map: repeat(None).take(sess.current_node_id_count()).collect(),
map: Default::default(),
parent_node: hir::CRATE_HIR_ID,
current_signature_dep_index: root_mod_sig_dep_index,
current_full_dep_index: root_mod_full_dep_index,
Expand All @@ -158,9 +158,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
hcx,
hir_body_nodes,
};
collector.insert_entry(CRATE_NODE_ID, Entry {
parent: CRATE_NODE_ID,
parent_hir: hir::CRATE_HIR_ID,
collector.insert_entry(hir::CRATE_HIR_ID, Entry {
parent: hir::CRATE_HIR_ID,
dep_node: root_mod_sig_dep_index,
node: Node::Crate,
});
Expand All @@ -172,7 +171,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
crate_disambiguator: CrateDisambiguator,
cstore: &dyn CrateStore,
commandline_args_hash: u64)
-> (Vec<Option<Entry<'hir>>>, Svh)
-> (FxHashMap<HirId, Entry<'hir>>, Svh)
{
self.hir_body_nodes.sort_unstable_by_key(|bn| bn.0);

Expand Down Expand Up @@ -223,15 +222,14 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
(self.map, svh)
}

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

fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
let entry = Entry {
parent: self.hir_to_node_id[&self.parent_node],
parent_hir: self.parent_node,
parent: self.parent_node,
dep_node: if self.currently_in_body {
self.current_full_dep_index
} else {
Expand All @@ -240,12 +238,11 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
node,
};

let node_id = self.hir_to_node_id[&hir_id];

// Make sure that the DepNode of some node coincides with the HirId
// owner of that node.
if cfg!(debug_assertions) {
assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id);
let node_id = self.hir_to_node_id[&hir_id];
assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id);

if hir_id.owner != self.current_dep_node_owner {
let node_str = match self.definitions.opt_def_index(node_id) {
Expand Down Expand Up @@ -278,7 +275,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
}
}

self.insert_entry(node_id, entry);
self.insert_entry(hir_id, entry);
}

fn with_parent<F: FnOnce(&mut Self)>(
Expand Down
Loading

0 comments on commit 920010c

Please sign in to comment.