From 0039153ba2c8d269c8002a4aa76802395abea13e Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 29 Oct 2023 01:44:12 -0400 Subject: [PATCH] rustdoc: Use `ThinVec` in `GenericParamDefKind` This should hopefully reduce memory usage and improve performance since these vectors are often empty. --- src/librustdoc/clean/mod.rs | 12 ++++++------ src/librustdoc/clean/simplify.rs | 2 +- src/librustdoc/clean/types.rs | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index f2447b877ca80..4d4c102e69eaa 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -498,7 +498,7 @@ fn clean_generic_param_def<'tcx>( ) -> GenericParamDef { let (name, kind) = match def.kind { ty::GenericParamDefKind::Lifetime => { - (def.name, GenericParamDefKind::Lifetime { outlives: vec![] }) + (def.name, GenericParamDefKind::Lifetime { outlives: ThinVec::new() }) } ty::GenericParamDefKind::Type { has_default, synthetic, .. } => { let default = if has_default { @@ -515,7 +515,7 @@ fn clean_generic_param_def<'tcx>( def.name, GenericParamDefKind::Type { did: def.def_id, - bounds: vec![], // These are filled in from the where-clauses. + bounds: ThinVec::new(), // These are filled in from the where-clauses. default: default.map(Box::new), synthetic, }, @@ -567,7 +567,7 @@ fn clean_generic_param<'tcx>( }) .collect() } else { - Vec::new() + ThinVec::new() }; (param.name.ident().name, GenericParamDefKind::Lifetime { outlives }) } @@ -580,7 +580,7 @@ fn clean_generic_param<'tcx>( .filter_map(|x| clean_generic_bound(x, cx)) .collect() } else { - Vec::new() + ThinVec::new() }; ( param.name.ident().name, @@ -636,7 +636,7 @@ pub(crate) fn clean_generics<'tcx>( match param.kind { GenericParamDefKind::Lifetime { .. } => unreachable!(), GenericParamDefKind::Type { did, ref bounds, .. } => { - cx.impl_trait_bounds.insert(did.into(), bounds.clone()); + cx.impl_trait_bounds.insert(did.into(), bounds.to_vec()); } GenericParamDefKind::Const { .. } => unreachable!(), } @@ -3146,7 +3146,7 @@ fn clean_bound_vars<'tcx>( name, kind: GenericParamDefKind::Type { did, - bounds: Vec::new(), + bounds: ThinVec::new(), default: None, synthetic: false, }, diff --git a/src/librustdoc/clean/simplify.rs b/src/librustdoc/clean/simplify.rs index eea8c1906c5b5..627f15e67ac30 100644 --- a/src/librustdoc/clean/simplify.rs +++ b/src/librustdoc/clean/simplify.rs @@ -145,7 +145,7 @@ pub(crate) fn move_bounds_to_generic_parameters(generics: &mut clean::Generics) .. }) = generics.params.iter_mut().find(|param| ¶m.name == arg) { - param_bounds.append(bounds); + param_bounds.extend(bounds.drain(..)); } else if let WherePredicate::RegionPredicate { lifetime: Lifetime(arg), bounds } = &mut pred && let Some(GenericParamDef { kind: GenericParamDefKind::Lifetime { outlives: param_bounds }, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 449aac4cfc8b7..7a28645abcf3b 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1325,8 +1325,8 @@ impl WherePredicate { #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub(crate) enum GenericParamDefKind { - Lifetime { outlives: Vec }, - Type { did: DefId, bounds: Vec, default: Option>, synthetic: bool }, + Lifetime { outlives: ThinVec }, + Type { did: DefId, bounds: ThinVec, default: Option>, synthetic: bool }, Const { ty: Box, default: Option>, is_host_effect: bool }, } @@ -1344,7 +1344,7 @@ pub(crate) struct GenericParamDef { impl GenericParamDef { pub(crate) fn lifetime(name: Symbol) -> Self { - Self { name, kind: GenericParamDefKind::Lifetime { outlives: Vec::new() } } + Self { name, kind: GenericParamDefKind::Lifetime { outlives: ThinVec::new() } } } pub(crate) fn is_synthetic_param(&self) -> bool {