Skip to content

Commit 4626184

Browse files
committed
Auto merge of #89998 - camelid:box-default, r=jyn514
rustdoc: Box some fields of `GenericParamDefKind` to reduce size This change shrinks `GenericParamDef` from 120 to 56 bytes. `GenericParamDef` is used a lot, so the extra indirection should hopefully be worth the size savings. r? `@ghost`
2 parents efd0483 + 9098689 commit 4626184

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

Diff for: src/librustdoc/clean/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
421421
GenericParamDefKind::Type {
422422
did: self.def_id,
423423
bounds: vec![], // These are filled in from the where-clauses.
424-
default,
424+
default: default.map(Box::new),
425425
synthetic,
426426
},
427427
)
@@ -430,9 +430,9 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
430430
self.name,
431431
GenericParamDefKind::Const {
432432
did: self.def_id,
433-
ty: cx.tcx.type_of(self.def_id).clean(cx),
433+
ty: Box::new(cx.tcx.type_of(self.def_id).clean(cx)),
434434
default: match has_default {
435-
true => Some(cx.tcx.const_param_default(self.def_id).to_string()),
435+
true => Some(Box::new(cx.tcx.const_param_default(self.def_id).to_string())),
436436
false => None,
437437
},
438438
},
@@ -462,18 +462,18 @@ impl Clean<GenericParamDef> for hir::GenericParam<'_> {
462462
GenericParamDefKind::Type {
463463
did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
464464
bounds: self.bounds.clean(cx),
465-
default: default.clean(cx),
465+
default: default.clean(cx).map(Box::new),
466466
synthetic,
467467
},
468468
),
469469
hir::GenericParamKind::Const { ref ty, default } => (
470470
self.name.ident().name,
471471
GenericParamDefKind::Const {
472472
did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
473-
ty: ty.clean(cx),
473+
ty: Box::new(ty.clean(cx)),
474474
default: default.map(|ct| {
475475
let def_id = cx.tcx.hir().local_def_id(ct.hir_id);
476-
ty::Const::from_anon_const(cx.tcx, def_id).to_string()
476+
Box::new(ty::Const::from_anon_const(cx.tcx, def_id).to_string())
477477
}),
478478
},
479479
),

Diff for: src/librustdoc/clean/types.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1219,13 +1219,13 @@ crate enum GenericParamDefKind {
12191219
Type {
12201220
did: DefId,
12211221
bounds: Vec<GenericBound>,
1222-
default: Option<Type>,
1222+
default: Option<Box<Type>>,
12231223
synthetic: Option<hir::SyntheticTyParamKind>,
12241224
},
12251225
Const {
12261226
did: DefId,
1227-
ty: Type,
1228-
default: Option<String>,
1227+
ty: Box<Type>,
1228+
default: Option<Box<String>>,
12291229
},
12301230
}
12311231

@@ -1239,8 +1239,8 @@ impl GenericParamDefKind {
12391239
// any embedded types, but `get_type` seems to be the wrong name for that.
12401240
crate fn get_type(&self) -> Option<Type> {
12411241
match self {
1242-
GenericParamDefKind::Type { default, .. } => default.clone(),
1243-
GenericParamDefKind::Const { ty, .. } => Some(ty.clone()),
1242+
GenericParamDefKind::Type { default, .. } => default.as_deref().cloned(),
1243+
GenericParamDefKind::Const { ty, .. } => Some((&**ty).clone()),
12441244
GenericParamDefKind::Lifetime { .. } => None,
12451245
}
12461246
}
@@ -1252,6 +1252,10 @@ crate struct GenericParamDef {
12521252
crate kind: GenericParamDefKind,
12531253
}
12541254

1255+
// `GenericParamDef` is used in many places. Make sure it doesn't unintentionally get bigger.
1256+
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
1257+
rustc_data_structures::static_assert_size!(GenericParamDef, 56);
1258+
12551259
impl GenericParamDef {
12561260
crate fn is_synthetic_type_param(&self) -> bool {
12571261
match self.kind {

Diff for: src/librustdoc/json/conversions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,10 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
330330
},
331331
Type { did: _, bounds, default, synthetic: _ } => GenericParamDefKind::Type {
332332
bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
333-
default: default.map(|x| x.into_tcx(tcx)),
333+
default: default.map(|x| (*x).into_tcx(tcx)),
334334
},
335335
Const { did: _, ty, default } => {
336-
GenericParamDefKind::Const { ty: ty.into_tcx(tcx), default }
336+
GenericParamDefKind::Const { ty: (*ty).into_tcx(tcx), default: default.map(|x| *x) }
337337
}
338338
}
339339
}

0 commit comments

Comments
 (0)