From 9e60288640fa549329bd518cbb541176839adedb Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 17 Oct 2021 19:40:23 -0700 Subject: [PATCH 1/3] Add static size assertion for `clean::GenericParamDef` --- src/librustdoc/clean/types.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index d4cea8b4a9d..770593a8b86 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1252,6 +1252,10 @@ crate struct GenericParamDef { crate kind: GenericParamDefKind, } +// `GenericParamDef` is used in many places. Make sure it doesn't unintentionally get bigger. +#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] +rustc_data_structures::static_assert_size!(GenericParamDef, 120); + impl GenericParamDef { crate fn is_synthetic_type_param(&self) -> bool { match self.kind { From 9b52a633e46dad4fe1bc99c4fa9794225bff3106 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 17 Oct 2021 19:38:41 -0700 Subject: [PATCH 2/3] rustdoc: Box `default` fields of `GenericParamDefKind` This reduces the size of `GenericParamDef` a bit, but some of the size savings are hidden because of the `ty` field of the `Const` variant. I will box that in the next commit. --- src/librustdoc/clean/mod.rs | 8 ++++---- src/librustdoc/clean/types.rs | 8 ++++---- src/librustdoc/json/conversions.rs | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 9d102d68783..64f7567dac2 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -421,7 +421,7 @@ impl Clean for ty::GenericParamDef { GenericParamDefKind::Type { did: self.def_id, bounds: vec![], // These are filled in from the where-clauses. - default, + default: default.map(Box::new), synthetic, }, ) @@ -432,7 +432,7 @@ impl Clean for ty::GenericParamDef { did: self.def_id, ty: cx.tcx.type_of(self.def_id).clean(cx), default: match has_default { - true => Some(cx.tcx.const_param_default(self.def_id).to_string()), + true => Some(Box::new(cx.tcx.const_param_default(self.def_id).to_string())), false => None, }, }, @@ -462,7 +462,7 @@ impl Clean for hir::GenericParam<'_> { GenericParamDefKind::Type { did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(), bounds: self.bounds.clean(cx), - default: default.clean(cx), + default: default.clean(cx).map(Box::new), synthetic, }, ), @@ -473,7 +473,7 @@ impl Clean for hir::GenericParam<'_> { ty: ty.clean(cx), default: default.map(|ct| { let def_id = cx.tcx.hir().local_def_id(ct.hir_id); - ty::Const::from_anon_const(cx.tcx, def_id).to_string() + Box::new(ty::Const::from_anon_const(cx.tcx, def_id).to_string()) }), }, ), diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 770593a8b86..daebbf782bc 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1219,13 +1219,13 @@ crate enum GenericParamDefKind { Type { did: DefId, bounds: Vec, - default: Option, + default: Option>, synthetic: Option, }, Const { did: DefId, ty: Type, - default: Option, + default: Option>, }, } @@ -1239,7 +1239,7 @@ impl GenericParamDefKind { // any embedded types, but `get_type` seems to be the wrong name for that. crate fn get_type(&self) -> Option { match self { - GenericParamDefKind::Type { default, .. } => default.clone(), + GenericParamDefKind::Type { default, .. } => default.as_deref().cloned(), GenericParamDefKind::Const { ty, .. } => Some(ty.clone()), GenericParamDefKind::Lifetime { .. } => None, } @@ -1254,7 +1254,7 @@ crate struct GenericParamDef { // `GenericParamDef` is used in many places. Make sure it doesn't unintentionally get bigger. #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(GenericParamDef, 120); +rustc_data_structures::static_assert_size!(GenericParamDef, 104); impl GenericParamDef { crate fn is_synthetic_type_param(&self) -> bool { diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 4098f17db81..77eb382a730 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -330,10 +330,10 @@ impl FromWithTcx for GenericParamDefKind { }, Type { did: _, bounds, default, synthetic: _ } => GenericParamDefKind::Type { bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(), - default: default.map(|x| x.into_tcx(tcx)), + default: default.map(|x| (*x).into_tcx(tcx)), }, Const { did: _, ty, default } => { - GenericParamDefKind::Const { ty: ty.into_tcx(tcx), default } + GenericParamDefKind::Const { ty: ty.into_tcx(tcx), default: default.map(|x| *x) } } } } From 90986897c597d76d86673de6e6d17c3d7e026df5 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 17 Oct 2021 19:48:42 -0700 Subject: [PATCH 3/3] rustdoc: Box `ty` field of `GenericParamDefKind::Const` This cuts the size of `GenericParamDef` in half, from 104 bytes to 56 bytes. I think the extra indirection should be worth the size savings. --- src/librustdoc/clean/mod.rs | 4 ++-- src/librustdoc/clean/types.rs | 6 +++--- src/librustdoc/json/conversions.rs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 64f7567dac2..075efd29b59 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -430,7 +430,7 @@ impl Clean for ty::GenericParamDef { self.name, GenericParamDefKind::Const { did: self.def_id, - ty: cx.tcx.type_of(self.def_id).clean(cx), + ty: Box::new(cx.tcx.type_of(self.def_id).clean(cx)), default: match has_default { true => Some(Box::new(cx.tcx.const_param_default(self.def_id).to_string())), false => None, @@ -470,7 +470,7 @@ impl Clean for hir::GenericParam<'_> { self.name.ident().name, GenericParamDefKind::Const { did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(), - ty: ty.clean(cx), + ty: Box::new(ty.clean(cx)), default: default.map(|ct| { let def_id = cx.tcx.hir().local_def_id(ct.hir_id); Box::new(ty::Const::from_anon_const(cx.tcx, def_id).to_string()) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index daebbf782bc..98bb32ed8d9 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1224,7 +1224,7 @@ crate enum GenericParamDefKind { }, Const { did: DefId, - ty: Type, + ty: Box, default: Option>, }, } @@ -1240,7 +1240,7 @@ impl GenericParamDefKind { crate fn get_type(&self) -> Option { match self { GenericParamDefKind::Type { default, .. } => default.as_deref().cloned(), - GenericParamDefKind::Const { ty, .. } => Some(ty.clone()), + GenericParamDefKind::Const { ty, .. } => Some((&**ty).clone()), GenericParamDefKind::Lifetime { .. } => None, } } @@ -1254,7 +1254,7 @@ crate struct GenericParamDef { // `GenericParamDef` is used in many places. Make sure it doesn't unintentionally get bigger. #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(GenericParamDef, 104); +rustc_data_structures::static_assert_size!(GenericParamDef, 56); impl GenericParamDef { crate fn is_synthetic_type_param(&self) -> bool { diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 77eb382a730..924275dc185 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -333,7 +333,7 @@ impl FromWithTcx for GenericParamDefKind { default: default.map(|x| (*x).into_tcx(tcx)), }, Const { did: _, ty, default } => { - GenericParamDefKind::Const { ty: ty.into_tcx(tcx), default: default.map(|x| *x) } + GenericParamDefKind::Const { ty: (*ty).into_tcx(tcx), default: default.map(|x| *x) } } } }