Skip to content

Commit e3c71f1

Browse files
committed
Auto merge of #88522 - camelid:box-paren-output, r=jyn514
rustdoc: Box `GenericArgs::Parenthesized.output` Split out from #88379. This reduces the size of `GenericArgs` from 104 bytes to 56 bytes, essentially reducing it by half. `GenericArgs` is one of the fields of `PathSegment`, so this should reduce the amount of memory allocated for `PathSegment`s in the cases where the generics are not for a `Fn`, `FnMut`, or `FnOnce` trait. r? `@jyn514`
2 parents 767edcf + 280e167 commit e3c71f1

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
351351
.flat_map(|(ty, mut bounds)| {
352352
if let Some(data) = ty_to_fn.get(&ty) {
353353
let (poly_trait, output) =
354-
(data.0.as_ref().expect("as_ref failed").clone(), data.1.as_ref().cloned());
354+
(data.0.as_ref().unwrap().clone(), data.1.as_ref().cloned().map(Box::new));
355355
let new_ty = match poly_trait.trait_ {
356356
Type::ResolvedPath { ref path, ref did, ref is_generic } => {
357357
let mut new_path = path.clone();

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -1773,10 +1773,9 @@ impl Clean<GenericArgs> for hir::GenericArgs<'_> {
17731773
fn clean(&self, cx: &mut DocContext<'_>) -> GenericArgs {
17741774
if self.parenthesized {
17751775
let output = self.bindings[0].ty().clean(cx);
1776-
GenericArgs::Parenthesized {
1777-
inputs: self.inputs().clean(cx),
1778-
output: if output != Type::Tuple(Vec::new()) { Some(output) } else { None },
1779-
}
1776+
let output =
1777+
if output != Type::Tuple(Vec::new()) { Some(Box::new(output)) } else { None };
1778+
GenericArgs::Parenthesized { inputs: self.inputs().clean(cx), output }
17801779
} else {
17811780
GenericArgs::AngleBracketed {
17821781
args: self

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ crate fn merge_bounds(
116116
});
117117
}
118118
PP::Parenthesized { ref mut output, .. } => match output {
119-
Some(o) => assert_eq!(o, rhs),
119+
Some(o) => assert_eq!(o.as_ref(), rhs),
120120
None => {
121121
if *rhs != clean::Type::Tuple(Vec::new()) {
122-
*output = Some(rhs.clone());
122+
*output = Some(Box::new(rhs.clone()));
123123
}
124124
}
125125
},

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -2019,15 +2019,25 @@ crate enum GenericArg {
20192019
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
20202020
crate enum GenericArgs {
20212021
AngleBracketed { args: Vec<GenericArg>, bindings: Vec<TypeBinding> },
2022-
Parenthesized { inputs: Vec<Type>, output: Option<Type> },
2022+
Parenthesized { inputs: Vec<Type>, output: Option<Box<Type>> },
20232023
}
20242024

2025+
// `GenericArgs` is in every `PathSegment`, so its size can significantly
2026+
// affect rustdoc's memory usage.
2027+
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
2028+
rustc_data_structures::static_assert_size!(GenericArgs, 56);
2029+
20252030
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
20262031
crate struct PathSegment {
20272032
crate name: Symbol,
20282033
crate args: GenericArgs,
20292034
}
20302035

2036+
// `PathSegment` usually occurs multiple times in every `Path`, so its size can
2037+
// significantly affect rustdoc's memory usage.
2038+
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
2039+
rustc_data_structures::static_assert_size!(PathSegment, 64);
2040+
20312041
#[derive(Clone, Debug)]
20322042
crate struct Typedef {
20332043
crate type_: Type,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl FromWithTcx<clean::GenericArgs> for GenericArgs {
127127
},
128128
Parenthesized { inputs, output } => GenericArgs::Parenthesized {
129129
inputs: inputs.into_iter().map(|a| a.into_tcx(tcx)).collect(),
130-
output: output.map(|a| a.into_tcx(tcx)),
130+
output: output.map(|a| (*a).into_tcx(tcx)),
131131
},
132132
}
133133
}

0 commit comments

Comments
 (0)