Skip to content

Commit d4c6f40

Browse files
committed
Auto merge of #54485 - arielb1:avoid-ctor-attrs, r=eddyb
avoid loading constructor attributes in AdtDef decoding During metadata loading, the AdtDefs for every ADT in the universe need to be loaded (for example, for coherence of builtin traits). For that, the attributes of the AdtDef need to be loaded too. The attributes of a struct are duplicated between 2 def ids - the constructor def-id, and the "type" def id. Loading attributes for both def-ids, which was done in #53721, slowed the compilation of small crates by 2-3%. This PR makes sure we only load the attributes for the "type" def-id, avoiding the slowdown. r? @eddyb & cc @nnethercote
2 parents f49f6e7 + 2c28c4e commit d4c6f40

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/librustc/ty/mod.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1724,19 +1724,29 @@ impl<'a, 'gcx, 'tcx> VariantDef {
17241724
/// - `did` is the DefId used for the variant - for tuple-structs, it is the constructor DefId,
17251725
/// and for everything else, it is the variant DefId.
17261726
/// - `attribute_def_id` is the DefId that has the variant's attributes.
1727+
/// this is the struct DefId for structs, and the variant DefId for variants.
1728+
///
1729+
/// Note that we *could* use the constructor DefId, because the constructor attributes
1730+
/// redirect to the base attributes, but compiling a small crate requires
1731+
/// loading the AdtDefs for all the structs in the universe (e.g. coherence for any
1732+
/// built-in trait), and we do not want to load attributes twice.
1733+
///
1734+
/// If someone speeds up attribute loading to not be a performance concern, they can
1735+
/// remove this hack and use the constructor DefId everywhere.
17271736
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
17281737
did: DefId,
17291738
name: Name,
17301739
discr: VariantDiscr,
17311740
fields: Vec<FieldDef>,
17321741
adt_kind: AdtKind,
1733-
ctor_kind: CtorKind)
1742+
ctor_kind: CtorKind,
1743+
attribute_def_id: DefId)
17341744
-> Self
17351745
{
1736-
debug!("VariantDef::new({:?}, {:?}, {:?}, {:?}, {:?}, {:?})", did, name, discr, fields,
1737-
adt_kind, ctor_kind);
1746+
debug!("VariantDef::new({:?}, {:?}, {:?}, {:?}, {:?}, {:?}, {:?})", did, name, discr,
1747+
fields, adt_kind, ctor_kind, attribute_def_id);
17381748
let mut flags = VariantFlags::NO_VARIANT_FLAGS;
1739-
if adt_kind == AdtKind::Struct && tcx.has_attr(did, "non_exhaustive") {
1749+
if adt_kind == AdtKind::Struct && tcx.has_attr(attribute_def_id, "non_exhaustive") {
17401750
debug!("found non-exhaustive field list for {:?}", did);
17411751
flags = flags | VariantFlags::IS_FIELD_LIST_NON_EXHAUSTIVE;
17421752
}

src/librustc_metadata/decoder.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,12 @@ impl<'a, 'tcx> CrateMetadata {
556556
_ => bug!(),
557557
};
558558

559+
let def_id = self.local_def_id(data.struct_ctor.unwrap_or(index));
560+
let attribute_def_id = self.local_def_id(index);
561+
559562
ty::VariantDef::new(
560563
tcx,
561-
self.local_def_id(data.struct_ctor.unwrap_or(index)),
564+
def_id,
562565
self.item_name(index).as_symbol(),
563566
data.discr,
564567
item.children.decode(self).map(|index| {
@@ -570,7 +573,8 @@ impl<'a, 'tcx> CrateMetadata {
570573
}
571574
}).collect(),
572575
adt_kind,
573-
data.ctor_kind
576+
data.ctor_kind,
577+
attribute_def_id
574578
)
575579
}
576580

src/librustc_typeck/collect.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,8 @@ fn convert_variant<'a, 'tcx>(
555555
name: ast::Name,
556556
discr: ty::VariantDiscr,
557557
def: &hir::VariantData,
558-
adt_kind: ty::AdtKind
558+
adt_kind: ty::AdtKind,
559+
attribute_def_id: DefId
559560
) -> ty::VariantDef {
560561
let mut seen_fields: FxHashMap<ast::Ident, Span> = FxHashMap();
561562
let node_id = tcx.hir.as_local_node_id(did).unwrap();
@@ -592,7 +593,8 @@ fn convert_variant<'a, 'tcx>(
592593
discr,
593594
fields,
594595
adt_kind,
595-
CtorKind::from_hir(def))
596+
CtorKind::from_hir(def),
597+
attribute_def_id)
596598
}
597599

598600
fn adt_def<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty::AdtDef {
@@ -622,7 +624,8 @@ fn adt_def<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty::Ad
622624
};
623625
distance_from_explicit += 1;
624626

625-
convert_variant(tcx, did, v.node.name, discr, &v.node.data, AdtKind::Enum)
627+
convert_variant(tcx, did, v.node.name, discr, &v.node.data, AdtKind::Enum,
628+
did)
626629
})
627630
.collect(),
628631
)
@@ -642,7 +645,8 @@ fn adt_def<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty::Ad
642645
item.name,
643646
ty::VariantDiscr::Relative(0),
644647
def,
645-
AdtKind::Struct
648+
AdtKind::Struct,
649+
def_id
646650
)],
647651
)
648652
}
@@ -654,7 +658,8 @@ fn adt_def<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty::Ad
654658
item.name,
655659
ty::VariantDiscr::Relative(0),
656660
def,
657-
AdtKind::Union
661+
AdtKind::Union,
662+
def_id
658663
)],
659664
),
660665
_ => bug!(),

0 commit comments

Comments
 (0)