From 24e876b52ed76710593895bc37bb8ed303075193 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 5 Apr 2021 02:03:37 +0200 Subject: Intern more `TypeRef`s in generics Saves ~3 MB --- crates/hir_def/src/generics.rs | 16 +++++++++------- crates/hir_def/src/path.rs | 2 +- crates/hir_def/src/path/lower.rs | 2 +- crates/hir_ty/src/utils.rs | 9 ++++----- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/crates/hir_def/src/generics.rs b/crates/hir_def/src/generics.rs index 7c6cbff11..e02ac2c2f 100644 --- a/crates/hir_def/src/generics.rs +++ b/crates/hir_def/src/generics.rs @@ -18,6 +18,7 @@ use crate::{ child_by_source::ChildBySource, db::DefDatabase, dyn_map::DynMap, + intern::Interned, keys, src::{HasChildSource, HasSource}, type_ref::{LifetimeRef, TypeBound, TypeRef}, @@ -29,7 +30,7 @@ use crate::{ #[derive(Clone, PartialEq, Eq, Debug)] pub struct TypeParamData { pub name: Option, - pub default: Option, + pub default: Option>, pub provenance: TypeParamProvenance, } @@ -43,7 +44,7 @@ pub struct LifetimeParamData { #[derive(Clone, PartialEq, Eq, Debug)] pub struct ConstParamData { pub name: Name, - pub ty: TypeRef, + pub ty: Interned, } #[derive(Copy, Clone, PartialEq, Eq, Debug)] @@ -75,7 +76,7 @@ pub enum WherePredicate { #[derive(Clone, PartialEq, Eq, Debug)] pub enum WherePredicateTypeTarget { - TypeRef(TypeRef), + TypeRef(Interned), /// For desugared where predicates that can directly refer to a type param. TypeParam(LocalTypeParamId), } @@ -256,7 +257,8 @@ impl GenericParams { for type_param in params.type_params() { let name = type_param.name().map_or_else(Name::missing, |it| it.as_name()); // FIXME: Use `Path::from_src` - let default = type_param.default_type().map(|it| TypeRef::from_ast(lower_ctx, it)); + let default = + type_param.default_type().map(|it| Interned::new(TypeRef::from_ast(lower_ctx, it))); let param = TypeParamData { name: Some(name.clone()), default, @@ -280,7 +282,7 @@ impl GenericParams { for const_param in params.const_params() { let name = const_param.name().map_or_else(Name::missing, |it| it.as_name()); let ty = const_param.ty().map_or(TypeRef::Error, |it| TypeRef::from_ast(lower_ctx, it)); - let param = ConstParamData { name, ty }; + let param = ConstParamData { name, ty: Interned::new(ty) }; let param_id = self.consts.alloc(param); sm.const_params.insert(param_id, const_param.clone()); } @@ -334,11 +336,11 @@ impl GenericParams { (Either::Left(type_ref), bound) => match hrtb_lifetimes { Some(hrtb_lifetimes) => WherePredicate::ForLifetime { lifetimes: hrtb_lifetimes.clone(), - target: WherePredicateTypeTarget::TypeRef(type_ref), + target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)), bound, }, None => WherePredicate::TypeBound { - target: WherePredicateTypeTarget::TypeRef(type_ref), + target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)), bound, }, }, diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs index a3e83e2cf..f9c8328f0 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs @@ -122,7 +122,7 @@ impl ModPath { pub struct Path { /// Type based path like `::foo`. /// Note that paths like `::foo` are desugard to `Trait::::foo`. - type_anchor: Option>, + type_anchor: Option>, mod_path: Interned, /// Invariant: the same len as `self.mod_path.segments` generic_args: Vec>>, diff --git a/crates/hir_def/src/path/lower.rs b/crates/hir_def/src/path/lower.rs index 28f6244da..7b29d9d4f 100644 --- a/crates/hir_def/src/path/lower.rs +++ b/crates/hir_def/src/path/lower.rs @@ -69,7 +69,7 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option match trait_ref { // ::foo None => { - type_anchor = Some(Box::new(self_type)); + type_anchor = Some(Interned::new(self_type)); kind = PathKind::Plain; } // >::Foo desugars to Trait::Foo diff --git a/crates/hir_ty/src/utils.rs b/crates/hir_ty/src/utils.rs index b23e91b1b..c85c328af 100644 --- a/crates/hir_ty/src/utils.rs +++ b/crates/hir_ty/src/utils.rs @@ -32,11 +32,10 @@ fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> Vec { .filter_map(|pred| match pred { WherePredicate::ForLifetime { target, bound, .. } | WherePredicate::TypeBound { target, bound } => match target { - WherePredicateTypeTarget::TypeRef(TypeRef::Path(p)) - if p == &Path::from(name![Self]) => - { - bound.as_path() - } + WherePredicateTypeTarget::TypeRef(type_ref) => match &**type_ref { + TypeRef::Path(p) if p == &Path::from(name![Self]) => bound.as_path(), + _ => None, + }, WherePredicateTypeTarget::TypeParam(local_id) if Some(*local_id) == trait_self => { bound.as_path() } -- cgit v1.2.3