Skip to content

Commit 5a77f30

Browse files
committed
Merge DocContext.{ty,lt,ct}_substs into one map
It should be impossible to have more than one entry with a particular key across the three maps, so they should be one map. In addition to making it impossible for multiple entries to exist, this should improve memory usage since now only one map is allocated on the stack and heap.
1 parent bbc58e8 commit 5a77f30

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

src/librustdoc/clean/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl Clean<Lifetime> for hir::Lifetime {
222222
| rl::Region::Free(_, node_id),
223223
) = def
224224
{
225-
if let Some(lt) = cx.lt_substs.get(&node_id).cloned() {
225+
if let Some(lt) = cx.substs.get(&node_id).and_then(|p| p.as_lt()).cloned() {
226226
return lt;
227227
}
228228
}
@@ -1157,7 +1157,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
11571157
match qpath {
11581158
hir::QPath::Resolved(None, ref path) => {
11591159
if let Res::Def(DefKind::TyParam, did) = path.res {
1160-
if let Some(new_ty) = cx.ty_substs.get(&did).cloned() {
1160+
if let Some(new_ty) = cx.substs.get(&did).and_then(|p| p.as_ty()).cloned() {
11611161
return new_ty;
11621162
}
11631163
if let Some(bounds) = cx.impl_trait_bounds.remove(&did.into()) {
@@ -1227,9 +1227,7 @@ fn maybe_expand_private_type_alias(cx: &mut DocContext<'_>, path: &hir::Path<'_>
12271227
let hir::ItemKind::TyAlias(ty, generics) = alias else { return None };
12281228

12291229
let provided_params = &path.segments.last().expect("segments were empty");
1230-
let mut ty_substs = FxHashMap::default();
1231-
let mut lt_substs = FxHashMap::default();
1232-
let mut ct_substs = FxHashMap::default();
1230+
let mut substs = FxHashMap::default();
12331231
let generic_args = provided_params.args();
12341232

12351233
let mut indices: hir::GenericParamCount = Default::default();
@@ -1254,7 +1252,7 @@ fn maybe_expand_private_type_alias(cx: &mut DocContext<'_>, path: &hir::Path<'_>
12541252
} else {
12551253
self::types::Lifetime::elided()
12561254
};
1257-
lt_substs.insert(lt_def_id.to_def_id(), cleaned);
1255+
substs.insert(lt_def_id.to_def_id(), SubstParam::Lifetime(cleaned));
12581256
}
12591257
indices.lifetimes += 1;
12601258
}
@@ -1272,9 +1270,9 @@ fn maybe_expand_private_type_alias(cx: &mut DocContext<'_>, path: &hir::Path<'_>
12721270
_ => None,
12731271
});
12741272
if let Some(ty) = type_ {
1275-
ty_substs.insert(ty_param_def_id.to_def_id(), ty.clean(cx));
1273+
substs.insert(ty_param_def_id.to_def_id(), SubstParam::Type(ty.clean(cx)));
12761274
} else if let Some(default) = *default {
1277-
ty_substs.insert(ty_param_def_id.to_def_id(), default.clean(cx));
1275+
substs.insert(ty_param_def_id.to_def_id(), SubstParam::Type(default.clean(cx)));
12781276
}
12791277
indices.types += 1;
12801278
}
@@ -1292,15 +1290,16 @@ fn maybe_expand_private_type_alias(cx: &mut DocContext<'_>, path: &hir::Path<'_>
12921290
_ => None,
12931291
});
12941292
if let Some(ct) = const_ {
1295-
ct_substs.insert(const_param_def_id.to_def_id(), ct.clean(cx));
1293+
substs
1294+
.insert(const_param_def_id.to_def_id(), SubstParam::Constant(ct.clean(cx)));
12961295
}
12971296
// FIXME(const_generics_defaults)
12981297
indices.consts += 1;
12991298
}
13001299
}
13011300
}
13021301

1303-
Some(cx.enter_alias(ty_substs, lt_substs, ct_substs, |cx| ty.clean(cx)))
1302+
Some(cx.enter_alias(substs, |cx| ty.clean(cx)))
13041303
}
13051304

13061305
impl Clean<Type> for hir::Ty<'_> {

src/librustdoc/clean/types.rs

+29
Original file line numberDiff line numberDiff line change
@@ -2247,3 +2247,32 @@ impl TypeBinding {
22472247
}
22482248
}
22492249
}
2250+
2251+
/// The type, lifetime, or constant that a private type alias's parameter should be
2252+
/// replaced with when expanding a use of that type alias.
2253+
///
2254+
/// For example:
2255+
///
2256+
/// ```
2257+
/// type PrivAlias<T> = Vec<T>;
2258+
///
2259+
/// pub fn public_fn() -> PrivAlias<i32> { vec![] }
2260+
/// ```
2261+
///
2262+
/// `public_fn`'s docs will show it as returning `Vec<i32>`, since `PrivAlias` is private.
2263+
/// [`SubstParam`] is used to record that `T` should be mapped to `i32`.
2264+
crate enum SubstParam {
2265+
Type(Type),
2266+
Lifetime(Lifetime),
2267+
Constant(Constant),
2268+
}
2269+
2270+
impl SubstParam {
2271+
crate fn as_ty(&self) -> Option<&Type> {
2272+
if let Self::Type(ty) = self { Some(ty) } else { None }
2273+
}
2274+
2275+
crate fn as_lt(&self) -> Option<&Lifetime> {
2276+
if let Self::Lifetime(lt) = self { Some(lt) } else { None }
2277+
}
2278+
}

src/librustdoc/core.rs

+7-25
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,10 @@ crate struct DocContext<'tcx> {
5454
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
5555
/// the same time.
5656
crate active_extern_traits: FxHashSet<DefId>,
57-
// The current set of type and lifetime substitutions,
57+
// The current set of parameter substitutions,
5858
// for expanding type aliases at the HIR level:
59-
/// Table `DefId` of type parameter -> substituted type
60-
crate ty_substs: FxHashMap<DefId, clean::Type>,
61-
/// Table `DefId` of lifetime parameter -> substituted lifetime
62-
crate lt_substs: FxHashMap<DefId, clean::Lifetime>,
63-
/// Table `DefId` of const parameter -> substituted const
64-
crate ct_substs: FxHashMap<DefId, clean::Constant>,
59+
/// Table `DefId` of type, lifetime, or const parameter -> substituted type, lifetime, or const
60+
crate substs: FxHashMap<DefId, clean::SubstParam>,
6561
/// Table synthetic type parameter for `impl Trait` in argument position -> bounds
6662
crate impl_trait_bounds: FxHashMap<ImplTraitParam, Vec<clean::GenericBound>>,
6763
/// Auto-trait or blanket impls processed so far, as `(self_ty, trait_def_id)`.
@@ -104,25 +100,13 @@ impl<'tcx> DocContext<'tcx> {
104100

105101
/// Call the closure with the given parameters set as
106102
/// the substitutions for a type alias' RHS.
107-
crate fn enter_alias<F, R>(
108-
&mut self,
109-
ty_substs: FxHashMap<DefId, clean::Type>,
110-
lt_substs: FxHashMap<DefId, clean::Lifetime>,
111-
ct_substs: FxHashMap<DefId, clean::Constant>,
112-
f: F,
113-
) -> R
103+
crate fn enter_alias<F, R>(&mut self, substs: FxHashMap<DefId, clean::SubstParam>, f: F) -> R
114104
where
115105
F: FnOnce(&mut Self) -> R,
116106
{
117-
let (old_tys, old_lts, old_cts) = (
118-
mem::replace(&mut self.ty_substs, ty_substs),
119-
mem::replace(&mut self.lt_substs, lt_substs),
120-
mem::replace(&mut self.ct_substs, ct_substs),
121-
);
107+
let old_substs = mem::replace(&mut self.substs, substs);
122108
let r = f(self);
123-
self.ty_substs = old_tys;
124-
self.lt_substs = old_lts;
125-
self.ct_substs = old_cts;
109+
self.substs = old_substs;
126110
r
127111
}
128112

@@ -350,9 +334,7 @@ crate fn run_global_ctxt(
350334
param_env: ParamEnv::empty(),
351335
external_traits: Default::default(),
352336
active_extern_traits: Default::default(),
353-
ty_substs: Default::default(),
354-
lt_substs: Default::default(),
355-
ct_substs: Default::default(),
337+
substs: Default::default(),
356338
impl_trait_bounds: Default::default(),
357339
generated_synthetics: Default::default(),
358340
auto_traits: tcx

0 commit comments

Comments
 (0)