Skip to content

Commit 24e876b

Browse files
Intern more TypeRefs in generics
Saves ~3 MB
1 parent 19e09a4 commit 24e876b

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

crates/hir_def/src/generics.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::{
1818
child_by_source::ChildBySource,
1919
db::DefDatabase,
2020
dyn_map::DynMap,
21+
intern::Interned,
2122
keys,
2223
src::{HasChildSource, HasSource},
2324
type_ref::{LifetimeRef, TypeBound, TypeRef},
@@ -29,7 +30,7 @@ use crate::{
2930
#[derive(Clone, PartialEq, Eq, Debug)]
3031
pub struct TypeParamData {
3132
pub name: Option<Name>,
32-
pub default: Option<TypeRef>,
33+
pub default: Option<Interned<TypeRef>>,
3334
pub provenance: TypeParamProvenance,
3435
}
3536

@@ -43,7 +44,7 @@ pub struct LifetimeParamData {
4344
#[derive(Clone, PartialEq, Eq, Debug)]
4445
pub struct ConstParamData {
4546
pub name: Name,
46-
pub ty: TypeRef,
47+
pub ty: Interned<TypeRef>,
4748
}
4849

4950
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
@@ -75,7 +76,7 @@ pub enum WherePredicate {
7576

7677
#[derive(Clone, PartialEq, Eq, Debug)]
7778
pub enum WherePredicateTypeTarget {
78-
TypeRef(TypeRef),
79+
TypeRef(Interned<TypeRef>),
7980
/// For desugared where predicates that can directly refer to a type param.
8081
TypeParam(LocalTypeParamId),
8182
}
@@ -256,7 +257,8 @@ impl GenericParams {
256257
for type_param in params.type_params() {
257258
let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
258259
// FIXME: Use `Path::from_src`
259-
let default = type_param.default_type().map(|it| TypeRef::from_ast(lower_ctx, it));
260+
let default =
261+
type_param.default_type().map(|it| Interned::new(TypeRef::from_ast(lower_ctx, it)));
260262
let param = TypeParamData {
261263
name: Some(name.clone()),
262264
default,
@@ -280,7 +282,7 @@ impl GenericParams {
280282
for const_param in params.const_params() {
281283
let name = const_param.name().map_or_else(Name::missing, |it| it.as_name());
282284
let ty = const_param.ty().map_or(TypeRef::Error, |it| TypeRef::from_ast(lower_ctx, it));
283-
let param = ConstParamData { name, ty };
285+
let param = ConstParamData { name, ty: Interned::new(ty) };
284286
let param_id = self.consts.alloc(param);
285287
sm.const_params.insert(param_id, const_param.clone());
286288
}
@@ -334,11 +336,11 @@ impl GenericParams {
334336
(Either::Left(type_ref), bound) => match hrtb_lifetimes {
335337
Some(hrtb_lifetimes) => WherePredicate::ForLifetime {
336338
lifetimes: hrtb_lifetimes.clone(),
337-
target: WherePredicateTypeTarget::TypeRef(type_ref),
339+
target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)),
338340
bound,
339341
},
340342
None => WherePredicate::TypeBound {
341-
target: WherePredicateTypeTarget::TypeRef(type_ref),
343+
target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)),
342344
bound,
343345
},
344346
},

crates/hir_def/src/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl ModPath {
122122
pub struct Path {
123123
/// Type based path like `<T>::foo`.
124124
/// Note that paths like `<Type as Trait>::foo` are desugard to `Trait::<Self=Type>::foo`.
125-
type_anchor: Option<Box<TypeRef>>,
125+
type_anchor: Option<Interned<TypeRef>>,
126126
mod_path: Interned<ModPath>,
127127
/// Invariant: the same len as `self.mod_path.segments`
128128
generic_args: Vec<Option<Arc<GenericArgs>>>,

crates/hir_def/src/path/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
6969
match trait_ref {
7070
// <T>::foo
7171
None => {
72-
type_anchor = Some(Box::new(self_type));
72+
type_anchor = Some(Interned::new(self_type));
7373
kind = PathKind::Plain;
7474
}
7575
// <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo

crates/hir_ty/src/utils.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> Vec<TraitId> {
3232
.filter_map(|pred| match pred {
3333
WherePredicate::ForLifetime { target, bound, .. }
3434
| WherePredicate::TypeBound { target, bound } => match target {
35-
WherePredicateTypeTarget::TypeRef(TypeRef::Path(p))
36-
if p == &Path::from(name![Self]) =>
37-
{
38-
bound.as_path()
39-
}
35+
WherePredicateTypeTarget::TypeRef(type_ref) => match &**type_ref {
36+
TypeRef::Path(p) if p == &Path::from(name![Self]) => bound.as_path(),
37+
_ => None,
38+
},
4039
WherePredicateTypeTarget::TypeParam(local_id) if Some(*local_id) == trait_self => {
4140
bound.as_path()
4241
}

0 commit comments

Comments
 (0)