Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit cd1ace4

Browse files
committed
Use an IndexVec for bodies.
1 parent 48a339d commit cd1ace4

File tree

7 files changed

+23
-19
lines changed

7 files changed

+23
-19
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
974974
let body = hir::Body { generator_kind: self.generator_kind, params, value };
975975
let id = body.id();
976976
debug_assert_eq!(id.hir_id.owner, self.current_hir_id_owner);
977-
self.bodies.insert(id.hir_id.local_id, body);
977+
self.bodies.ensure_contains_elem(id.hir_id.local_id, || None);
978+
self.bodies[id.hir_id.local_id] = Some(self.arena.alloc(body));
978979
id
979980
}
980981

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct LoweringContext<'a, 'hir: 'a> {
9797

9898
/// The items being lowered are collected here.
9999
owners: IndexVec<LocalDefId, Option<hir::OwnerInfo<'hir>>>,
100-
bodies: BTreeMap<hir::ItemLocalId, hir::Body<'hir>>,
100+
bodies: IndexVec<hir::ItemLocalId, Option<&'hir hir::Body<'hir>>>,
101101
attrs: BTreeMap<hir::ItemLocalId, &'hir [Attribute]>,
102102

103103
generator_kind: Option<hir::GeneratorKind>,
@@ -322,7 +322,7 @@ pub fn lower_crate<'a, 'hir>(
322322
nt_to_tokenstream,
323323
arena,
324324
owners,
325-
bodies: BTreeMap::new(),
325+
bodies: IndexVec::new(),
326326
attrs: BTreeMap::default(),
327327
catch_scope: None,
328328
loop_scope: None,

compiler/rustc_hir/src/arena.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ macro_rules! arena_types {
1919
[] attribute: rustc_ast::Attribute,
2020
[] block: rustc_hir::Block<$tcx>,
2121
[] bare_fn_ty: rustc_hir::BareFnTy<$tcx>,
22+
[] body: rustc_hir::Body<$tcx>,
2223
[] generic_arg: rustc_hir::GenericArg<$tcx>,
2324
[] generic_args: rustc_hir::GenericArgs<$tcx>,
2425
[] generic_bound: rustc_hir::GenericBound<$tcx>,

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ pub struct WhereEqPredicate<'hir> {
666666
pub struct OwnerInfo<'hir> {
667667
pub node: OwnerNode<'hir>,
668668
pub attrs: BTreeMap<ItemLocalId, &'hir [Attribute]>,
669-
pub bodies: BTreeMap<ItemLocalId, Body<'hir>>,
669+
pub bodies: IndexVec<ItemLocalId, Option<&'hir Body<'hir>>>,
670670
/// Map indicating what traits are in scope for places where this
671671
/// is relevant; generated by resolve.
672672
pub trait_map: FxHashMap<ItemLocalId, Box<[TraitCandidate]>>,
@@ -705,9 +705,9 @@ impl Crate<'hir> {
705705
self.owners[id.def_id].as_ref().unwrap().node.expect_foreign_item()
706706
}
707707

708-
pub fn body(&self, id: BodyId) -> &Body<'hir> {
708+
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
709709
let HirId { owner, local_id } = id.hir_id;
710-
&self.owners[owner].as_ref().unwrap().bodies[&local_id]
710+
self.owners[owner].as_ref().unwrap().bodies[local_id].unwrap()
711711
}
712712

713713
pub fn attrs(&self, id: HirId) -> &'hir [Attribute] {

compiler/rustc_middle/src/hir/map/collector.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
9696
let mut nodes = IndexVec::new();
9797
nodes.push(Some(ParentedNode { parent: ItemLocalId::new(0), node: node.into() }));
9898

99-
let mut bodies = FxHashMap::default();
100-
for (id, body) in self.krate.owners[owner].as_ref().unwrap().bodies.iter() {
101-
let _old = bodies.insert(*id, body);
102-
debug_assert!(_old.is_none());
103-
}
99+
let bodies = &self.krate.owners[owner].as_ref().unwrap().bodies;
104100

105101
debug_assert!(self.map[owner].is_none());
106102
self.map[owner] = Some(self.arena.alloc(OwnerNodes { hash, nodes, bodies }));

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl<'hir> Map<'hir> {
381381
}
382382

383383
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
384-
self.tcx.hir_owner_nodes(id.hir_id.owner).unwrap().bodies.get(&id.hir_id.local_id).unwrap()
384+
self.tcx.hir_owner_nodes(id.hir_id.owner).unwrap().bodies[id.hir_id.local_id].unwrap()
385385
}
386386

387387
pub fn fn_decl_by_hir_id(&self, hir_id: HirId) -> Option<&'hir FnDecl<'hir>> {
@@ -500,10 +500,13 @@ impl<'hir> Map<'hir> {
500500
.iter_enumerated()
501501
.flat_map(move |(owner, owner_info)| {
502502
let bodies = &owner_info.as_ref()?.bodies;
503-
Some(bodies.keys().map(move |&local_id| {
503+
Some(bodies.iter_enumerated().filter_map(move |(local_id, body)| {
504+
if body.is_none() {
505+
return None;
506+
}
504507
let hir_id = HirId { owner, local_id };
505508
let body_id = BodyId { hir_id };
506-
self.body_owner_def_id(body_id)
509+
Some(self.body_owner_def_id(body_id))
507510
}))
508511
})
509512
.flatten()
@@ -517,10 +520,13 @@ impl<'hir> Map<'hir> {
517520
par_iter(&self.krate().owners.raw).enumerate().for_each(|(owner, owner_info)| {
518521
let owner = LocalDefId::new(owner);
519522
if let Some(owner_info) = owner_info {
520-
par_iter(&owner_info.bodies).for_each(|(&local_id, _)| {
521-
let hir_id = HirId { owner, local_id };
522-
let body_id = BodyId { hir_id };
523-
f(self.body_owner_def_id(body_id))
523+
par_iter(&owner_info.bodies.raw).enumerate().for_each(|(local_id, body)| {
524+
if body.is_some() {
525+
let local_id = ItemLocalId::new(local_id);
526+
let hir_id = HirId { owner, local_id };
527+
let body_id = BodyId { hir_id };
528+
f(self.body_owner_def_id(body_id))
529+
}
524530
})
525531
}
526532
});

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub struct OwnerNodes<'tcx> {
6565
// The zeroth node's parent is trash, but is never accessed.
6666
nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
6767
/// Content of local bodies.
68-
bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
68+
bodies: &'tcx IndexVec<ItemLocalId, Option<&'tcx Body<'tcx>>>,
6969
}
7070

7171
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for OwnerNodes<'tcx> {

0 commit comments

Comments
 (0)