Skip to content

Commit cc575a6

Browse files
committed
rustc: use IndexVec<DefIndex, T> instead of Vec<T>.
1 parent 9285d40 commit cc575a6

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

src/librustc/hir/map/collector.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
149149
let mut collector = NodeCollector {
150150
krate,
151151
source_map: sess.source_map(),
152-
map: vec![None; definitions.def_index_count()],
152+
map: IndexVec::from_elem_n(IndexVec::new(), definitions.def_index_count()),
153153
parent_node: hir::CRATE_HIR_ID,
154154
current_signature_dep_index: root_mod_sig_dep_index,
155155
current_full_dep_index: root_mod_full_dep_index,
@@ -227,12 +227,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
227227

228228
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>) {
229229
debug!("hir_map: {:?} => {:?}", id, entry);
230-
let local_map = &mut self.map[id.owner.index()];
230+
let local_map = &mut self.map[id.owner];
231231
let i = id.local_id.as_u32() as usize;
232-
if local_map.is_none() {
233-
*local_map = Some(IndexVec::with_capacity(i + 1));
234-
}
235-
let local_map = local_map.as_mut().unwrap();
236232
let len = local_map.len();
237233
if i >= len {
238234
local_map.extend(repeat(None).take(i - len + 1));

src/librustc/hir/map/definitions.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use syntax_pos::{Span, DUMMY_SP};
2727
/// There is one `DefPathTable` for each crate.
2828
#[derive(Clone, Default, RustcDecodable, RustcEncodable)]
2929
pub struct DefPathTable {
30-
index_to_key: Vec<DefKey>,
31-
def_path_hashes: Vec<DefPathHash>,
30+
index_to_key: IndexVec<DefIndex, DefKey>,
31+
def_path_hashes: IndexVec<DefIndex, DefPathHash>,
3232
}
3333

3434
impl DefPathTable {
@@ -53,14 +53,14 @@ impl DefPathTable {
5353

5454
#[inline(always)]
5555
pub fn def_key(&self, index: DefIndex) -> DefKey {
56-
self.index_to_key[index.index()]
56+
self.index_to_key[index]
5757
}
5858

5959
#[inline(always)]
6060
pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
61-
let ret = self.def_path_hashes[index.index()];
62-
debug!("def_path_hash({:?}) = {:?}", index, ret);
63-
return ret
61+
let hash = self.def_path_hashes[index];
62+
debug!("def_path_hash({:?}) = {:?}", index, hash);
63+
hash
6464
}
6565

6666
pub fn add_def_path_hashes_to(&self,
@@ -92,7 +92,7 @@ impl DefPathTable {
9292
pub struct Definitions {
9393
table: DefPathTable,
9494
node_to_def_index: NodeMap<DefIndex>,
95-
def_index_to_node: Vec<ast::NodeId>,
95+
def_index_to_node: IndexVec<DefIndex, ast::NodeId>,
9696
pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
9797
/// If `ExpnId` is an ID of some macro expansion,
9898
/// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
@@ -375,7 +375,7 @@ impl Definitions {
375375
#[inline]
376376
pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
377377
if def_id.krate == LOCAL_CRATE {
378-
let node_id = self.def_index_to_node[def_id.index.index()];
378+
let node_id = self.def_index_to_node[def_id.index];
379379
if node_id != ast::DUMMY_NODE_ID {
380380
return Some(node_id);
381381
}
@@ -404,7 +404,7 @@ impl Definitions {
404404

405405
#[inline]
406406
pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> hir::HirId {
407-
let node_id = self.def_index_to_node[def_index.index()];
407+
let node_id = self.def_index_to_node[def_index];
408408
self.node_to_hir_id[node_id]
409409
}
410410

src/librustc/hir/map/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ impl Forest {
156156

157157
/// This type is effectively a `HashMap<HirId, Entry<'hir>>`,
158158
/// but it is implemented as 2 layers of arrays.
159-
/// - first we have `A = Vec<Option<B>>` mapping a `DefIndex`'s index to an inner value
159+
/// - first we have `A = IndexVec<DefIndex, B>` mapping `DefIndex`s to an inner value
160160
/// - which is `B = IndexVec<ItemLocalId, Option<Entry<'hir>>` which gives you the `Entry`.
161-
pub(super) type HirEntryMap<'hir> = Vec<Option<IndexVec<ItemLocalId, Option<Entry<'hir>>>>>;
161+
pub(super) type HirEntryMap<'hir> = IndexVec<DefIndex, IndexVec<ItemLocalId, Option<Entry<'hir>>>>;
162162

163163
/// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s.
164164
#[derive(Clone)]
@@ -222,8 +222,8 @@ impl<'map> Iterator for ParentHirIterator<'map> {
222222
impl<'hir> Map<'hir> {
223223
#[inline]
224224
fn lookup(&self, id: HirId) -> Option<&Entry<'hir>> {
225-
let local_map = self.map.get(id.owner.index())?;
226-
local_map.as_ref()?.get(id.local_id)?.as_ref()
225+
let local_map = self.map.get(id.owner)?;
226+
local_map.get(id.local_id)?.as_ref()
227227
}
228228

229229
/// Registers a read in the dependency graph of the AST node with
@@ -1031,14 +1031,12 @@ impl<'hir> Map<'hir> {
10311031
// see the comment on `HirEntryMap`.
10321032
// Iterate over all the indices and return a reference to
10331033
// local maps and their index given that they exist.
1034-
self.map.iter().enumerate().filter_map(|(i, local_map)| {
1035-
local_map.as_ref().map(|m| (i, m))
1036-
}).flat_map(move |(array_index, local_map)| {
1034+
self.map.iter_enumerated().flat_map(move |(owner, local_map)| {
10371035
// Iterate over each valid entry in the local map.
10381036
local_map.iter_enumerated().filter_map(move |(i, entry)| entry.map(move |_| {
10391037
// Reconstruct the `HirId` based on the 3 indices we used to find it.
10401038
HirId {
1041-
owner: DefIndex::from(array_index),
1039+
owner,
10421040
local_id: i,
10431041
}
10441042
}))

0 commit comments

Comments
 (0)