Skip to content

Commit 2d1240e

Browse files
committed
Box GenericArgs::Parenthesized.output
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. I also added `static_assert_size!`s to `GenericArgs` and `PathSegment` to ensure they don't increase in size unexpectedly.
1 parent 5d68044 commit 2d1240e

File tree

5 files changed

+21
-10
lines changed

5 files changed

+21
-10
lines changed

src/librustdoc/clean/auto_trait.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,10 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
350350
.into_iter()
351351
.flat_map(|(ty, mut bounds)| {
352352
if let Some(data) = ty_to_fn.get(&ty) {
353-
let (poly_trait, output) =
354-
(data.0.as_ref().expect("as_ref failed").clone(), data.1.as_ref().cloned());
353+
let (poly_trait, output) = (
354+
data.0.as_ref().expect("as_ref failed").clone(),
355+
data.1.as_ref().cloned().map(Box::new),
356+
);
355357
let new_ty = match poly_trait.trait_ {
356358
Type::ResolvedPath { ref path, ref did, ref is_generic } => {
357359
let mut new_path = path.clone();

src/librustdoc/clean/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1763,10 +1763,9 @@ impl Clean<GenericArgs> for hir::GenericArgs<'_> {
17631763
fn clean(&self, cx: &mut DocContext<'_>) -> GenericArgs {
17641764
if self.parenthesized {
17651765
let output = self.bindings[0].ty().clean(cx);
1766-
GenericArgs::Parenthesized {
1767-
inputs: self.inputs().clean(cx),
1768-
output: if output != Type::Tuple(Vec::new()) { Some(output) } else { None },
1769-
}
1766+
let output =
1767+
if output != Type::Tuple(Vec::new()) { Some(Box::new(output)) } else { None };
1768+
GenericArgs::Parenthesized { inputs: self.inputs().clean(cx), output }
17701769
} else {
17711770
GenericArgs::AngleBracketed {
17721771
args: self

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
},

src/librustdoc/clean/types.rs

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

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

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

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)