Skip to content

Commit 650bff8

Browse files
committed
Auto merge of rust-lang#100645 - notriddle:notriddle/rustdoc-diet-plan, r=GuillaumeGomez
rustdoc: strategic boxing to reduce the size of ItemKind and Type The `Type` change redesigns `QPath` to box the entire data structure instead of boxing `self_type` and the `trait_`. This reduces the size of several `ItemKind` variants, leaving `Impl` as the biggest variant. The `ItemKind` change boxes that variant's payload.
2 parents 3130203 + 238bcc9 commit 650bff8

File tree

8 files changed

+54
-47
lines changed

8 files changed

+54
-47
lines changed

src/librustdoc/clean/auto_trait.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -551,13 +551,15 @@ where
551551
}
552552
WherePredicate::EqPredicate { lhs, rhs } => {
553553
match lhs {
554-
Type::QPath { ref assoc, ref self_type, ref trait_, .. } => {
554+
Type::QPath(box QPathData {
555+
ref assoc, ref self_type, ref trait_, ..
556+
}) => {
555557
let ty = &*self_type;
556558
let mut new_trait = trait_.clone();
557559

558560
if self.is_fn_trait(trait_) && assoc.name == sym::Output {
559561
ty_to_fn
560-
.entry(*ty.clone())
562+
.entry(ty.clone())
561563
.and_modify(|e| {
562564
*e = (e.0.clone(), Some(rhs.ty().unwrap().clone()))
563565
})
@@ -582,7 +584,7 @@ where
582584
// to 'T: Iterator<Item=u8>'
583585
GenericArgs::AngleBracketed { ref mut bindings, .. } => {
584586
bindings.push(TypeBinding {
585-
assoc: *assoc.clone(),
587+
assoc: assoc.clone(),
586588
kind: TypeBindingKind::Equality { term: rhs },
587589
});
588590
}
@@ -596,7 +598,7 @@ where
596598
}
597599
}
598600

599-
let bounds = ty_to_bounds.entry(*ty.clone()).or_default();
601+
let bounds = ty_to_bounds.entry(ty.clone()).or_default();
600602

601603
bounds.insert(GenericBound::TraitBound(
602604
PolyTrait { trait_: new_trait, generic_params: Vec::new() },
@@ -613,7 +615,7 @@ where
613615
));
614616
// Avoid creating any new duplicate bounds later in the outer
615617
// loop
616-
ty_to_traits.entry(*ty.clone()).or_default().insert(trait_.clone());
618+
ty_to_traits.entry(ty.clone()).or_default().insert(trait_.clone());
617619
}
618620
_ => panic!("Unexpected LHS {:?} for {:?}", lhs, item_def_id),
619621
}

src/librustdoc/clean/inline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(crate) fn try_inline(
6262
Res::Def(DefKind::Trait, did) => {
6363
record_extern_fqn(cx, did, ItemType::Trait);
6464
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
65-
clean::TraitItem(build_external_trait(cx, did))
65+
clean::TraitItem(Box::new(build_external_trait(cx, did)))
6666
}
6767
Res::Def(DefKind::Fn, did) => {
6868
record_extern_fqn(cx, did, ItemType::Function);
@@ -672,7 +672,7 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
672672

673673
g.where_predicates.retain(|pred| match pred {
674674
clean::WherePredicate::BoundPredicate {
675-
ty: clean::QPath { self_type: box clean::Generic(ref s), trait_, .. },
675+
ty: clean::QPath(box clean::QPathData { self_type: clean::Generic(ref s), trait_, .. }),
676676
bounds,
677677
..
678678
} => !(bounds.is_empty() || *s == kw::SelfUpper && trait_.def_id() == trait_did),

src/librustdoc/clean/mod.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,12 @@ fn clean_projection<'tcx>(
410410
self_type.def_id(&cx.cache)
411411
};
412412
let should_show_cast = compute_should_show_cast(self_def_id, &trait_, &self_type);
413-
Type::QPath {
414-
assoc: Box::new(projection_to_path_segment(ty, cx)),
413+
Type::QPath(Box::new(QPathData {
414+
assoc: projection_to_path_segment(ty, cx),
415415
should_show_cast,
416-
self_type: Box::new(self_type),
416+
self_type,
417417
trait_,
418-
}
418+
}))
419419
}
420420

421421
fn compute_should_show_cast(self_def_id: Option<DefId>, trait_: &Path, self_type: &Type) -> bool {
@@ -1182,7 +1182,7 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
11821182
.where_predicates
11831183
.drain_filter(|pred| match *pred {
11841184
WherePredicate::BoundPredicate {
1185-
ty: QPath { ref assoc, ref self_type, ref trait_, .. },
1185+
ty: QPath(box QPathData { ref assoc, ref self_type, ref trait_, .. }),
11861186
..
11871187
} => {
11881188
if assoc.name != my_name {
@@ -1191,7 +1191,7 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
11911191
if trait_.def_id() != assoc_item.container_id(tcx) {
11921192
return false;
11931193
}
1194-
match **self_type {
1194+
match *self_type {
11951195
Generic(ref s) if *s == kw::SelfUpper => {}
11961196
_ => return false,
11971197
}
@@ -1324,15 +1324,12 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
13241324
let self_def_id = DefId::local(qself.hir_id.owner.local_def_index);
13251325
let self_type = clean_ty(qself, cx);
13261326
let should_show_cast = compute_should_show_cast(Some(self_def_id), &trait_, &self_type);
1327-
Type::QPath {
1328-
assoc: Box::new(clean_path_segment(
1329-
p.segments.last().expect("segments were empty"),
1330-
cx,
1331-
)),
1327+
Type::QPath(Box::new(QPathData {
1328+
assoc: clean_path_segment(p.segments.last().expect("segments were empty"), cx),
13321329
should_show_cast,
1333-
self_type: Box::new(self_type),
1330+
self_type,
13341331
trait_,
1335-
}
1332+
}))
13361333
}
13371334
hir::QPath::TypeRelative(qself, segment) => {
13381335
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
@@ -1347,12 +1344,12 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
13471344
let self_def_id = res.opt_def_id();
13481345
let self_type = clean_ty(qself, cx);
13491346
let should_show_cast = compute_should_show_cast(self_def_id, &trait_, &self_type);
1350-
Type::QPath {
1351-
assoc: Box::new(clean_path_segment(segment, cx)),
1347+
Type::QPath(Box::new(QPathData {
1348+
assoc: clean_path_segment(segment, cx),
13521349
should_show_cast,
1353-
self_type: Box::new(self_type),
1350+
self_type,
13541351
trait_,
1355-
}
1352+
}))
13561353
}
13571354
hir::QPath::LangItem(..) => bug!("clean: requiring documentation of lang item"),
13581355
}
@@ -1954,12 +1951,12 @@ fn clean_maybe_renamed_item<'tcx>(
19541951
.map(|ti| clean_trait_item(cx.tcx.hir().trait_item(ti.id), cx))
19551952
.collect();
19561953

1957-
TraitItem(Trait {
1954+
TraitItem(Box::new(Trait {
19581955
def_id,
19591956
items,
19601957
generics: clean_generics(generics, cx),
19611958
bounds: bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
1962-
})
1959+
}))
19631960
}
19641961
ItemKind::ExternCrate(orig_name) => {
19651962
return clean_extern_crate(item, name, orig_name, cx);

src/librustdoc/clean/types.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ pub(crate) enum ItemKind {
727727
OpaqueTyItem(OpaqueTy),
728728
StaticItem(Static),
729729
ConstantItem(Constant),
730-
TraitItem(Trait),
730+
TraitItem(Box<Trait>),
731731
TraitAliasItem(TraitAlias),
732732
ImplItem(Box<Impl>),
733733
/// A required method in a trait declaration meaning it's only a function signature.
@@ -1550,13 +1550,7 @@ pub(crate) enum Type {
15501550
BorrowedRef { lifetime: Option<Lifetime>, mutability: Mutability, type_: Box<Type> },
15511551

15521552
/// A qualified path to an associated item: `<Type as Trait>::Name`
1553-
QPath {
1554-
assoc: Box<PathSegment>,
1555-
self_type: Box<Type>,
1556-
/// FIXME: compute this field on demand.
1557-
should_show_cast: bool,
1558-
trait_: Path,
1559-
},
1553+
QPath(Box<QPathData>),
15601554

15611555
/// A type that is inferred: `_`
15621556
Infer,
@@ -1654,8 +1648,8 @@ impl Type {
16541648
}
16551649

16561650
pub(crate) fn projection(&self) -> Option<(&Type, DefId, PathSegment)> {
1657-
if let QPath { self_type, trait_, assoc, .. } = self {
1658-
Some((self_type, trait_.def_id(), *assoc.clone()))
1651+
if let QPath(box QPathData { self_type, trait_, assoc, .. }) = self {
1652+
Some((self_type, trait_.def_id(), assoc.clone()))
16591653
} else {
16601654
None
16611655
}
@@ -1679,7 +1673,7 @@ impl Type {
16791673
Slice(..) => PrimitiveType::Slice,
16801674
Array(..) => PrimitiveType::Array,
16811675
RawPointer(..) => PrimitiveType::RawPointer,
1682-
QPath { ref self_type, .. } => return self_type.inner_def_id(cache),
1676+
QPath(box QPathData { ref self_type, .. }) => return self_type.inner_def_id(cache),
16831677
Generic(_) | Infer | ImplTrait(_) => return None,
16841678
};
16851679
cache.and_then(|c| Primitive(t).def_id(c))
@@ -1693,6 +1687,15 @@ impl Type {
16931687
}
16941688
}
16951689

1690+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
1691+
pub(crate) struct QPathData {
1692+
pub assoc: PathSegment,
1693+
pub self_type: Type,
1694+
/// FIXME: compute this field on demand.
1695+
pub should_show_cast: bool,
1696+
pub trait_: Path,
1697+
}
1698+
16961699
/// A primitive (aka, builtin) type.
16971700
///
16981701
/// This represents things like `i32`, `str`, etc.
@@ -2484,11 +2487,11 @@ mod size_asserts {
24842487
// These are in alphabetical order, which is easy to maintain.
24852488
static_assert_size!(Crate, 72); // frequently moved by-value
24862489
static_assert_size!(DocFragment, 32);
2487-
static_assert_size!(GenericArg, 80);
2490+
static_assert_size!(GenericArg, 64);
24882491
static_assert_size!(GenericArgs, 32);
24892492
static_assert_size!(GenericParamDef, 56);
24902493
static_assert_size!(Item, 56);
2491-
static_assert_size!(ItemKind, 112);
2494+
static_assert_size!(ItemKind, 96);
24922495
static_assert_size!(PathSegment, 40);
2493-
static_assert_size!(Type, 72);
2496+
static_assert_size!(Type, 56);
24942497
}

src/librustdoc/formats/cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
227227
if let clean::TraitItem(ref t) = *item.kind {
228228
self.cache.traits.entry(item.item_id.expect_def_id()).or_insert_with(|| {
229229
clean::TraitWithExtraInfo {
230-
trait_: t.clone(),
230+
trait_: *t.clone(),
231231
is_notable: item.attrs.has_doc_flag(sym::notable_trait),
232232
}
233233
});

src/librustdoc/html/format.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,12 @@ fn fmt_type<'cx>(
10791079
write!(f, "impl {}", print_generic_bounds(bounds, cx))
10801080
}
10811081
}
1082-
clean::QPath { ref assoc, ref self_type, ref trait_, should_show_cast } => {
1082+
clean::QPath(box clean::QPathData {
1083+
ref assoc,
1084+
ref self_type,
1085+
ref trait_,
1086+
should_show_cast,
1087+
}) => {
10831088
if f.alternate() {
10841089
if should_show_cast {
10851090
write!(f, "<{:#} as {:#}>::", self_type.print(cx), trait_.print(cx))?

src/librustdoc/html/render/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2632,8 +2632,8 @@ fn collect_paths_for_type(first_ty: clean::Type, cache: &Cache) -> Vec<String> {
26322632
clean::Type::BorrowedRef { type_, .. } => {
26332633
work.push_back(*type_);
26342634
}
2635-
clean::Type::QPath { self_type, trait_, .. } => {
2636-
work.push_back(*self_type);
2635+
clean::Type::QPath(box clean::QPathData { self_type, trait_, .. }) => {
2636+
work.push_back(self_type);
26372637
process_path(trait_.def_id());
26382638
}
26392639
_ => {}

src/librustdoc/json/conversions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
248248
VariantItem(v) => ItemEnum::Variant(v.into_tcx(tcx)),
249249
FunctionItem(f) => ItemEnum::Function(from_function(f, header.unwrap(), tcx)),
250250
ForeignFunctionItem(f) => ItemEnum::Function(from_function(f, header.unwrap(), tcx)),
251-
TraitItem(t) => ItemEnum::Trait(t.into_tcx(tcx)),
251+
TraitItem(t) => ItemEnum::Trait((*t).into_tcx(tcx)),
252252
TraitAliasItem(t) => ItemEnum::TraitAlias(t.into_tcx(tcx)),
253253
MethodItem(m, _) => ItemEnum::Method(from_function_method(m, true, header.unwrap(), tcx)),
254254
TyMethodItem(m) => ItemEnum::Method(from_function_method(m, false, header.unwrap(), tcx)),
@@ -480,10 +480,10 @@ impl FromWithTcx<clean::Type> for Type {
480480
mutable: mutability == ast::Mutability::Mut,
481481
type_: Box::new((*type_).into_tcx(tcx)),
482482
},
483-
QPath { assoc, self_type, trait_, .. } => Type::QualifiedPath {
483+
QPath(box clean::QPathData { assoc, self_type, trait_, .. }) => Type::QualifiedPath {
484484
name: assoc.name.to_string(),
485485
args: Box::new(assoc.args.clone().into_tcx(tcx)),
486-
self_type: Box::new((*self_type).into_tcx(tcx)),
486+
self_type: Box::new(self_type.into_tcx(tcx)),
487487
trait_: trait_.into_tcx(tcx),
488488
},
489489
}

0 commit comments

Comments
 (0)