Skip to content

Commit

Permalink
Auto merge of #66931 - cjgillot:hirene-preamble, r=eddyb
Browse files Browse the repository at this point in the history
Allocate HIR on an arena 1/4

This PR is the first in a series of 4, aiming at allocating the HIR on an arena, as a memory optimisation.

1. This first PR lays the groundwork and migrates some low-hanging fruits.
2. The second PR will migrate `hir::Expr`, `hir::Pat` and related.
3. The third PR will migrate `hir::Ty` and related.
4. The final PR will be dedicated to eventual cleanups.

In order to make the transition as gradual as possible, some lowering routines receive `Box`-allocated data and move it into the arena. This is a bit wasteful, but hopefully temporary.
Nonetheless, special care should be taken to avoid double arena allocations.

Work mentored by @Zoxc.
  • Loading branch information
bors committed Dec 22, 2019
2 parents 3982d35 + baa49b2 commit 26286c7
Show file tree
Hide file tree
Showing 84 changed files with 856 additions and 781 deletions.
16 changes: 15 additions & 1 deletion src/librustc/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ macro_rules! arena_types {
rustc::hir::def_id::CrateNum
>
>,
[few] hir_forest: rustc::hir::map::Forest,
[few] diagnostic_items: rustc_data_structures::fx::FxHashMap<
syntax::symbol::Symbol,
rustc::hir::def_id::DefId,
Expand Down Expand Up @@ -123,6 +122,21 @@ macro_rules! arena_types {
[few] crate_variances: rustc::ty::CrateVariancesMap<'tcx>,
[few] inferred_outlives_crate: rustc::ty::CratePredicatesMap<'tcx>,
[] upvars: rustc_data_structures::fx::FxIndexMap<rustc::hir::HirId, rustc::hir::Upvar>,

// HIR types
[few] hir_forest: rustc::hir::map::Forest<$tcx>,
[] attribute: syntax::ast::Attribute,
[few] global_asm: rustc::hir::GlobalAsm,
[] fn_decl: rustc::hir::FnDecl,
[] foreign_item: rustc::hir::ForeignItem<$tcx>,
[] impl_item_ref: rustc::hir::ImplItemRef,
[few] macro_def: rustc::hir::MacroDef<$tcx>,
[] param: rustc::hir::Param,
[] path: rustc::hir::Path,
[] struct_field: rustc::hir::StructField<$tcx>,
[] trait_item_ref: rustc::hir::TraitItemRef,
[] ty: rustc::hir::Ty,
[] variant: rustc::hir::Variant<$tcx>,
], $tcx);
)
}
Expand Down
36 changes: 18 additions & 18 deletions src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! conflicts between multiple such attributes attached to the same
//! item.

use crate::hir::{self, HirId, HirVec, Attribute, Item, ItemKind, TraitItem, TraitItemKind};
use crate::hir::{self, HirId, Attribute, Item, ItemKind, TraitItem, TraitItemKind};
use crate::hir::DUMMY_HIR_ID;
use crate::hir::def_id::DefId;
use crate::hir::intravisit::{self, Visitor, NestedVisitorMap};
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Display for Target {
}

impl Target {
pub(crate) fn from_item(item: &Item) -> Target {
pub(crate) fn from_item(item: &Item<'_>) -> Target {
match item.kind {
ItemKind::ExternCrate(..) => Target::ExternCrate,
ItemKind::Use(..) => Target::Use,
Expand All @@ -107,7 +107,7 @@ impl Target {
}
}

fn from_trait_item(trait_item: &TraitItem) -> Target {
fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
match trait_item.kind {
TraitItemKind::Const(..) => Target::AssocConst,
TraitItemKind::Method(_, hir::TraitMethod::Required(_)) => {
Expand All @@ -120,15 +120,15 @@ impl Target {
}
}

fn from_foreign_item(foreign_item: &hir::ForeignItem) -> Target {
fn from_foreign_item(foreign_item: &hir::ForeignItem<'_>) -> Target {
match foreign_item.kind {
hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
hir::ForeignItemKind::Type => Target::ForeignTy,
}
}

fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem) -> Target {
fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem<'_>) -> Target {
match impl_item.kind {
hir::ImplItemKind::Const(..) => Target::AssocConst,
hir::ImplItemKind::Method(..) => {
Expand Down Expand Up @@ -158,10 +158,10 @@ impl CheckAttrVisitor<'tcx> {
fn check_attributes(
&self,
hir_id: HirId,
attrs: &HirVec<Attribute>,
attrs: &'hir [Attribute],
span: &Span,
target: Target,
item: Option<&Item>,
item: Option<&Item<'_>>,
) {
let mut is_valid = true;
for attr in attrs {
Expand Down Expand Up @@ -241,7 +241,7 @@ impl CheckAttrVisitor<'tcx> {
fn check_track_caller(
&self,
attr_span: &Span,
attrs: &HirVec<Attribute>,
attrs: &'hir [Attribute],
span: &Span,
target: Target,
) -> bool {
Expand Down Expand Up @@ -332,10 +332,10 @@ impl CheckAttrVisitor<'tcx> {
/// Checks if the `#[repr]` attributes on `item` are valid.
fn check_repr(
&self,
attrs: &HirVec<Attribute>,
attrs: &'hir [Attribute],
span: &Span,
target: Target,
item: Option<&Item>,
item: Option<&Item<'_>>,
) {
// Extract the names of all repr hints, e.g., [foo, bar, align] for:
// ```
Expand Down Expand Up @@ -477,7 +477,7 @@ impl CheckAttrVisitor<'tcx> {
}
}

fn check_used(&self, attrs: &HirVec<Attribute>, target: Target) {
fn check_used(&self, attrs: &'hir [Attribute], target: Target) {
for attr in attrs {
if attr.check_name(sym::used) && target != Target::Static {
self.tcx.sess
Expand All @@ -492,25 +492,25 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
NestedVisitorMap::OnlyBodies(&self.tcx.hir())
}

fn visit_item(&mut self, item: &'tcx Item) {
fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
let target = Target::from_item(item);
self.check_attributes(item.hir_id, &item.attrs, &item.span, target, Some(item));
self.check_attributes(item.hir_id, item.attrs, &item.span, target, Some(item));
intravisit::walk_item(self, item)
}

fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem) {
fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem<'tcx>) {
let target = Target::from_trait_item(trait_item);
self.check_attributes(trait_item.hir_id, &trait_item.attrs, &trait_item.span, target, None);
intravisit::walk_trait_item(self, trait_item)
}

fn visit_foreign_item(&mut self, f_item: &'tcx hir::ForeignItem) {
fn visit_foreign_item(&mut self, f_item: &'tcx hir::ForeignItem<'tcx>) {
let target = Target::from_foreign_item(f_item);
self.check_attributes(f_item.hir_id, &f_item.attrs, &f_item.span, target, None);
intravisit::walk_foreign_item(self, f_item)
}

fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
let target = Target::from_impl_item(self.tcx, impl_item);
self.check_attributes(impl_item.hir_id, &impl_item.attrs, &impl_item.span, target, None);
intravisit::walk_impl_item(self, impl_item)
Expand All @@ -527,9 +527,9 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
}
}

fn is_c_like_enum(item: &Item) -> bool {
fn is_c_like_enum(item: &Item<'_>) -> bool {
if let ItemKind::Enum(ref def, _) = item.kind {
for variant in &def.variants {
for variant in def.variants {
match variant.data {
hir::VariantData::Unit(..) => { /* continue */ }
_ => return false,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl CtorKind {
}
}

pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
pub fn from_hir(vdata: &hir::VariantData<'_>) -> CtorKind {
match *vdata {
hir::VariantData::Tuple(..) => CtorKind::Fn,
hir::VariantData::Unit(..) => CtorKind::Const,
Expand Down
Loading

0 comments on commit 26286c7

Please sign in to comment.