-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize HIR map #60246
Merged
Merged
Optimize HIR map #60246
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ use crate::middle::cstore::CrateStoreDyn; | |
|
||
use rustc_target::spec::abi::Abi; | ||
use rustc_data_structures::svh::Svh; | ||
use rustc_data_structures::indexed_vec::IndexVec; | ||
use syntax::ast::{self, Name, NodeId}; | ||
use syntax::source_map::Spanned; | ||
use syntax::ext::base::MacroKind; | ||
|
@@ -161,6 +162,13 @@ impl Forest { | |
} | ||
} | ||
|
||
/// This type is effectively a `HashMap<HirId, Entry<'hir>>`, | ||
/// but is implemented by 3 layers of arrays. | ||
/// - the outer layer is `[A; 2]` and correspond to the 2 address spaces `DefIndex`es can be in | ||
/// - then we have `A = Vec<Option<B>>` mapping a `DefIndex`'s index to a inner value | ||
/// - which is `B = IndexVec<ItemLocalId, Option<Entry<'hir>>` which finally gives you the `Entry`. | ||
pub(super) type HirEntryMap<'hir> = [Vec<Option<IndexVec<ItemLocalId, Option<Entry<'hir>>>>>; 2]; | ||
|
||
/// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s. | ||
#[derive(Clone)] | ||
pub struct Map<'hir> { | ||
|
@@ -174,7 +182,7 @@ pub struct Map<'hir> { | |
/// The SVH of the local crate. | ||
pub crate_hash: Svh, | ||
|
||
map: FxHashMap<HirId, Entry<'hir>>, | ||
map: HirEntryMap<'hir>, | ||
|
||
definitions: &'hir Definitions, | ||
|
||
|
@@ -183,6 +191,12 @@ pub struct Map<'hir> { | |
} | ||
|
||
impl<'hir> Map<'hir> { | ||
#[inline] | ||
fn lookup(&self, id: HirId) -> Option<&Entry<'hir>> { | ||
let local_map = self.map[id.owner.address_space().index()].get(id.owner.as_array_index())?; | ||
local_map.as_ref()?.get(id.local_id)?.as_ref() | ||
} | ||
|
||
/// Registers a read in the dependency graph of the AST node with | ||
/// the given `id`. This needs to be called each time a public | ||
/// function returns the HIR for a node -- in other words, when it | ||
|
@@ -191,7 +205,7 @@ impl<'hir> Map<'hir> { | |
/// read recorded). If the function just returns a DefId or | ||
/// NodeId, no actual content was returned, so no read is needed. | ||
pub fn read(&self, hir_id: HirId) { | ||
if let Some(entry) = self.map.get(&hir_id) { | ||
if let Some(entry) = self.lookup(hir_id) { | ||
self.dep_graph.read_index(entry.dep_node); | ||
} else { | ||
bug!("called `HirMap::read()` with invalid `HirId`: {:?}", hir_id) | ||
|
@@ -378,12 +392,8 @@ impl<'hir> Map<'hir> { | |
}) | ||
} | ||
|
||
fn entry_count(&self) -> usize { | ||
self.map.len() | ||
} | ||
|
||
fn find_entry(&self, id: HirId) -> Option<Entry<'hir>> { | ||
self.map.get(&id).cloned() | ||
self.lookup(id).cloned() | ||
} | ||
|
||
pub fn krate(&self) -> &'hir Crate { | ||
|
@@ -433,7 +443,7 @@ impl<'hir> Map<'hir> { | |
/// item (possibly associated), a closure, or a `hir::AnonConst`. | ||
pub fn body_owner(&self, BodyId { hir_id }: BodyId) -> NodeId { | ||
let parent = self.get_parent_node_by_hir_id(hir_id); | ||
assert!(self.map.get(&parent).map_or(false, |e| e.is_body_owner(hir_id))); | ||
assert!(self.lookup(parent).map_or(false, |e| e.is_body_owner(hir_id))); | ||
self.hir_to_node_id(parent) | ||
} | ||
|
||
|
@@ -1004,6 +1014,34 @@ impl<'hir> Map<'hir> { | |
attrs.unwrap_or(&[]) | ||
} | ||
|
||
/// Returns an iterator that yields all the hir ids in the map. | ||
fn all_ids<'a>(&'a self) -> impl Iterator<Item = HirId> + 'a { | ||
// This code is a bit awkward because the map is implemented as 3 levels of arrays, | ||
// see the comment on `HirEntryMap`. | ||
let map = &self.map; | ||
|
||
// Look at both the def index address spaces | ||
let spaces = [DefIndexAddressSpace::Low, DefIndexAddressSpace::High].iter().cloned(); | ||
spaces.flat_map(move |space| { | ||
// Iterate over all the indices in the address space and return a reference to | ||
// local maps and their index given that they exist. | ||
let local_maps = map[space.index()].iter().enumerate().filter_map(|(i, local_map)| { | ||
local_map.as_ref().map(|m| (i, m)) | ||
}); | ||
|
||
local_maps.flat_map(move |(array_index, local_map)| { | ||
// Iterate over each valid entry in the local map | ||
local_map.iter_enumerated().filter_map(move |(i, entry)| entry.map(move |_| { | ||
// Reconstruct the HirId based on the 3 indices we used to find it | ||
HirId { | ||
owner: DefIndex::from_array_index(array_index, space), | ||
local_id: i, | ||
} | ||
})) | ||
}) | ||
}) | ||
} | ||
|
||
/// Returns an iterator that yields the node id's with paths that | ||
/// match `parts`. (Requires `parts` is non-empty.) | ||
/// | ||
|
@@ -1012,13 +1050,16 @@ impl<'hir> Map<'hir> { | |
/// such as `foo::bar::quux`, `bar::quux`, `other::bar::quux`, and | ||
/// any other such items it can find in the map. | ||
pub fn nodes_matching_suffix<'a>(&'a self, parts: &'a [String]) | ||
-> NodesMatchingSuffix<'a, 'hir> { | ||
NodesMatchingSuffix { | ||
-> impl Iterator<Item = NodeId> + 'a { | ||
let nodes = NodesMatchingSuffix { | ||
map: self, | ||
item_name: parts.last().unwrap(), | ||
in_which: &parts[..parts.len() - 1], | ||
idx: ast::CRATE_NODE_ID, | ||
} | ||
}; | ||
|
||
self.all_ids().filter(move |hir| nodes.matces_suffix(*hir)).map(move |hir| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. *matches |
||
self.hir_to_node_id(hir) | ||
}) | ||
} | ||
|
||
pub fn span(&self, id: NodeId) -> Span { | ||
|
@@ -1097,21 +1138,20 @@ impl<'hir> Map<'hir> { | |
} | ||
} | ||
|
||
pub struct NodesMatchingSuffix<'a, 'hir:'a> { | ||
map: &'a Map<'hir>, | ||
pub struct NodesMatchingSuffix<'a> { | ||
map: &'a Map<'a>, | ||
item_name: &'a String, | ||
in_which: &'a [String], | ||
idx: NodeId, | ||
} | ||
|
||
impl<'a, 'hir> NodesMatchingSuffix<'a, 'hir> { | ||
impl<'a> NodesMatchingSuffix<'a> { | ||
/// Returns `true` only if some suffix of the module path for parent | ||
/// matches `self.in_which`. | ||
/// | ||
/// In other words: let `[x_0,x_1,...,x_k]` be `self.in_which`; | ||
/// returns true if parent's path ends with the suffix | ||
/// `x_0::x_1::...::x_k`. | ||
fn suffix_matches(&self, parent: NodeId) -> bool { | ||
fn suffix_matches(&self, parent: HirId) -> bool { | ||
let mut cursor = parent; | ||
for part in self.in_which.iter().rev() { | ||
let (mod_id, mod_name) = match find_first_mod_parent(self.map, cursor) { | ||
|
@@ -1121,7 +1161,7 @@ impl<'a, 'hir> NodesMatchingSuffix<'a, 'hir> { | |
if mod_name != &**part { | ||
return false; | ||
} | ||
cursor = self.map.get_parent(mod_id); | ||
cursor = self.map.get_parent_item(mod_id); | ||
} | ||
return true; | ||
|
||
|
@@ -1131,14 +1171,14 @@ impl<'a, 'hir> NodesMatchingSuffix<'a, 'hir> { | |
// If `id` itself is a mod named `m` with parent `p`, then | ||
// returns `Some(id, m, p)`. If `id` has no mod in its parent | ||
// chain, then returns `None`. | ||
fn find_first_mod_parent<'a>(map: &'a Map<'_>, mut id: NodeId) -> Option<(NodeId, Name)> { | ||
fn find_first_mod_parent<'a>(map: &'a Map<'_>, mut id: HirId) -> Option<(HirId, Name)> { | ||
loop { | ||
if let Node::Item(item) = map.find(id)? { | ||
if let Node::Item(item) = map.find_by_hir_id(id)? { | ||
if item_is_mod(&item) { | ||
return Some((id, item.ident.name)) | ||
} | ||
} | ||
let parent = map.get_parent(id); | ||
let parent = map.get_parent_item(id); | ||
if parent == id { return None } | ||
id = parent; | ||
} | ||
|
@@ -1154,35 +1194,21 @@ impl<'a, 'hir> NodesMatchingSuffix<'a, 'hir> { | |
|
||
// We are looking at some node `n` with a given name and parent | ||
// id; do their names match what I am seeking? | ||
fn matches_names(&self, parent_of_n: NodeId, name: Name) -> bool { | ||
fn matches_names(&self, parent_of_n: HirId, name: Name) -> bool { | ||
name == &**self.item_name && self.suffix_matches(parent_of_n) | ||
} | ||
} | ||
|
||
impl<'a, 'hir> Iterator for NodesMatchingSuffix<'a, 'hir> { | ||
type Item = NodeId; | ||
|
||
fn next(&mut self) -> Option<NodeId> { | ||
loop { | ||
let idx = self.idx; | ||
if idx.as_usize() >= self.map.entry_count() { | ||
return None; | ||
} | ||
self.idx = NodeId::from_u32(self.idx.as_u32() + 1); | ||
let hir_idx = self.map.node_to_hir_id(idx); | ||
let name = match self.map.find_entry(hir_idx).map(|entry| entry.node) { | ||
Some(Node::Item(n)) => n.name(), | ||
Some(Node::ForeignItem(n)) => n.name(), | ||
Some(Node::TraitItem(n)) => n.name(), | ||
Some(Node::ImplItem(n)) => n.name(), | ||
Some(Node::Variant(n)) => n.name(), | ||
Some(Node::Field(n)) => n.name(), | ||
_ => continue, | ||
}; | ||
if self.matches_names(self.map.get_parent(idx), name) { | ||
return Some(idx) | ||
} | ||
} | ||
fn matces_suffix(&self, hir: HirId) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. *matches |
||
let name = match self.map.find_entry(hir).map(|entry| entry.node) { | ||
Some(Node::Item(n)) => n.name(), | ||
Some(Node::ForeignItem(n)) => n.name(), | ||
Some(Node::TraitItem(n)) => n.name(), | ||
Some(Node::ImplItem(n)) => n.name(), | ||
Some(Node::Variant(n)) => n.name(), | ||
Some(Node::Field(n)) => n.name(), | ||
_ => return false, | ||
}; | ||
self.matches_names(self.map.get_parent_item(hir), name) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be great to also add a comment here and/or rustfmt the function body