From a169fa6a832a513cd1534d1a6566e4584ca5fb6a Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 18:13:02 +0200 Subject: Intern VariableKinds --- crates/hir_ty/src/traits/chalk/interner.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index bd9395b7e..7a3bd41d4 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -4,7 +4,10 @@ use super::tls; use base_db::salsa::InternId; use chalk_ir::{GenericArg, Goal, GoalData}; -use hir_def::TypeAliasId; +use hir_def::{ + intern::{impl_internable, InternStorage, Internable, Interned}, + TypeAliasId, +}; use smallvec::SmallVec; use std::{fmt, sync::Arc}; @@ -26,6 +29,11 @@ pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId; pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum; pub(crate) type Variances = chalk_ir::Variances; +#[derive(PartialEq, Eq, Hash, Debug)] +pub struct InternedVariableKindsInner(Vec>); + +impl_internable!(InternedVariableKindsInner,); + impl chalk_ir::interner::Interner for Interner { type InternedType = Arc>; type InternedLifetime = chalk_ir::LifetimeData; @@ -38,7 +46,7 @@ impl chalk_ir::interner::Interner for Interner { type InternedProgramClause = Arc>; type InternedProgramClauses = Arc<[chalk_ir::ProgramClause]>; type InternedQuantifiedWhereClauses = Vec>; - type InternedVariableKinds = Vec>; + type InternedVariableKinds = Interned; type InternedCanonicalVarKinds = Vec>; type InternedConstraints = Vec>>; type InternedVariances = Arc<[chalk_ir::Variance]>; @@ -322,14 +330,16 @@ impl chalk_ir::interner::Interner for Interner { &self, data: impl IntoIterator, E>>, ) -> Result { - data.into_iter().collect() + Ok(Interned::new(InternedVariableKindsInner( + data.into_iter().collect::, E>>()?, + ))) } fn variable_kinds_data<'a>( &self, parameter_kinds: &'a Self::InternedVariableKinds, ) -> &'a [chalk_ir::VariableKind] { - ¶meter_kinds + ¶meter_kinds.as_ref().0 } fn intern_canonical_var_kinds( -- cgit v1.2.3 From be03db0e3a75533f34d48c3014d532919b30a9e9 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 18:25:18 +0200 Subject: Intern Substitutions (Costs a bit of performance, reduces memory usage on RA by ~10%.) --- crates/hir/src/lib.rs | 2 +- crates/hir_ty/src/display.rs | 20 ++++++++++---------- crates/hir_ty/src/infer/expr.rs | 2 +- crates/hir_ty/src/infer/pat.rs | 4 ++-- crates/hir_ty/src/infer/path.rs | 2 +- crates/hir_ty/src/lib.rs | 4 ++-- crates/hir_ty/src/traits/chalk/interner.rs | 23 +++++++++++++++-------- crates/hir_ty/src/traits/chalk/mapping.rs | 4 ++-- 8 files changed, 34 insertions(+), 27 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 817a01db1..04875240b 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1825,7 +1825,7 @@ impl Type { Solution::Unique(s) => s .value .subst - .interned() + .as_slice(&Interner) .first() .map(|ty| self.derived(ty.assert_ty_ref(&Interner).clone())), Solution::Ambig(_) => None, diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index d7a3977e5..92224b46b 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -265,7 +265,7 @@ impl HirDisplay for ProjectionTy { write!(f, " as {}", trait_.name)?; if self.substitution.len(&Interner) > 1 { write!(f, "<")?; - f.write_joined(&self.substitution.interned()[1..], ", ")?; + f.write_joined(&self.substitution.as_slice(&Interner)[1..], ", ")?; write!(f, ">")?; } write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty_id)).name)?; @@ -416,7 +416,7 @@ impl HirDisplay for Ty { write!(f, ",)")?; } else { write!(f, "(")?; - f.write_joined(&*substs.interned(), ", ")?; + f.write_joined(&*substs.as_slice(&Interner), ", ")?; write!(f, ")")?; } } @@ -444,7 +444,7 @@ impl HirDisplay for Ty { // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self? if total_len > 0 { write!(f, "<")?; - f.write_joined(¶meters.interned()[..total_len], ", ")?; + f.write_joined(¶meters.as_slice(&Interner)[..total_len], ", ")?; write!(f, ">")?; } } @@ -491,7 +491,7 @@ impl HirDisplay for Ty { .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) .filter(|defaults| !defaults.is_empty()) { - None => parameters.interned().as_ref(), + None => parameters.as_slice(&Interner), Some(default_parameters) => { let mut default_from = 0; for (i, parameter) in parameters.iter(&Interner).enumerate() { @@ -515,11 +515,11 @@ impl HirDisplay for Ty { } } } - ¶meters.interned()[0..default_from] + ¶meters.as_slice(&Interner)[0..default_from] } } } else { - parameters.interned().as_ref() + parameters.as_slice(&Interner) }; if !parameters_to_write.is_empty() { write!(f, "<")?; @@ -542,7 +542,7 @@ impl HirDisplay for Ty { write!(f, "{}::{}", trait_.name, type_alias_data.name)?; if parameters.len(&Interner) > 0 { write!(f, "<")?; - f.write_joined(&*parameters.interned(), ", ")?; + f.write_joined(&*parameters.as_slice(&Interner), ", ")?; write!(f, ">")?; } } else { @@ -749,13 +749,13 @@ fn write_bounds_like_dyn_trait( // existential) here, which is the only thing that's // possible in actual Rust, and hence don't print it write!(f, "{}", f.db.trait_data(trait_).name)?; - if let [_, params @ ..] = &*trait_ref.substitution.interned().as_slice() { + if let [_, params @ ..] = &*trait_ref.substitution.as_slice(&Interner) { if is_fn_trait { if let Some(args) = params.first().and_then(|it| it.assert_ty_ref(&Interner).as_tuple()) { write!(f, "(")?; - f.write_joined(&*args.interned(), ", ")?; + f.write_joined(args.as_slice(&Interner), ", ")?; write!(f, ")")?; } } else if !params.is_empty() { @@ -814,7 +814,7 @@ fn fmt_trait_ref(tr: &TraitRef, f: &mut HirFormatter, use_as: bool) -> Result<() write!(f, "{}", f.db.trait_data(tr.hir_trait_id()).name)?; if tr.substitution.len(&Interner) > 1 { write!(f, "<")?; - f.write_joined(&tr.substitution.interned()[1..], ", ")?; + f.write_joined(&tr.substitution.as_slice(&Interner)[1..], ", ")?; write!(f, ">")?; } Ok(()) diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index cbbfa8b5c..ee6763926 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -462,7 +462,7 @@ impl<'a> InferenceContext<'a> { }; match canonicalized.decanonicalize_ty(derefed_ty.value).kind(&Interner) { TyKind::Tuple(_, substs) => name.as_tuple_index().and_then(|idx| { - substs.interned().get(idx).map(|a| a.assert_ty_ref(&Interner)).cloned() + substs.as_slice(&Interner).get(idx).map(|a| a.assert_ty_ref(&Interner)).cloned() }), TyKind::Adt(AdtId(hir_def::AdtId::StructId(s)), parameters) => { let local_id = self.db.struct_data(*s).variant_data.field(name)?; diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index a41e8e116..aea354cde 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -122,7 +122,7 @@ impl<'a> InferenceContext<'a> { let ty = match &body[pat] { &Pat::Tuple { ref args, ellipsis } => { let expectations = match expected.as_tuple() { - Some(parameters) => &*parameters.interned().as_slice(), + Some(parameters) => &*parameters.as_slice(&Interner), _ => &[], }; @@ -242,7 +242,7 @@ impl<'a> InferenceContext<'a> { let (inner_ty, alloc_ty) = match expected.as_adt() { Some((adt, subst)) if adt == box_adt => ( subst.at(&Interner, 0).assert_ty_ref(&Interner).clone(), - subst.interned().get(1).and_then(|a| a.ty(&Interner).cloned()), + subst.as_slice(&Interner).get(1).and_then(|a| a.ty(&Interner).cloned()), ), _ => (self.result.standard_types.unknown.clone(), None), }; diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs index f8955aa32..495282eba 100644 --- a/crates/hir_ty/src/infer/path.rs +++ b/crates/hir_ty/src/infer/path.rs @@ -101,7 +101,7 @@ impl<'a> InferenceContext<'a> { let substs = ctx.substs_from_path(path, typable, true); let ty = TyBuilder::value_ty(self.db, typable) .use_parent_substs(&parent_substs) - .fill(substs.interned()[parent_substs.len(&Interner)..].iter().cloned()) + .fill(substs.as_slice(&Interner)[parent_substs.len(&Interner)..].iter().cloned()) .build(); Some(ty) } diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 874c95411..beb58d711 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -109,7 +109,7 @@ pub type WhereClause = chalk_ir::WhereClause; pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution { Substitution::from_iter( &Interner, - s.interned()[..std::cmp::min(s.len(&Interner), n)].iter().cloned(), + s.as_slice(&Interner)[..std::cmp::min(s.len(&Interner), n)].iter().cloned(), ) } @@ -187,7 +187,7 @@ impl CallableSig { .shifted_out_to(&Interner, DebruijnIndex::ONE) .expect("unexpected lifetime vars in fn ptr") .0 - .interned() + .as_slice(&Interner) .iter() .map(|arg| arg.assert_ty_ref(&Interner).clone()) .collect(), diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index 7a3bd41d4..17e056f03 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -3,11 +3,12 @@ use super::tls; use base_db::salsa::InternId; -use chalk_ir::{GenericArg, Goal, GoalData}; +use chalk_ir::{Goal, GoalData}; use hir_def::{ intern::{impl_internable, InternStorage, Internable, Interned}, TypeAliasId, }; +use crate::GenericArg; use smallvec::SmallVec; use std::{fmt, sync::Arc}; @@ -32,7 +33,13 @@ pub(crate) type Variances = chalk_ir::Variances; #[derive(PartialEq, Eq, Hash, Debug)] pub struct InternedVariableKindsInner(Vec>); -impl_internable!(InternedVariableKindsInner,); +#[derive(PartialEq, Eq, Hash, Debug)] +pub struct InternedSubstitutionInner(SmallVec<[GenericArg; 2]>); + +impl_internable!( + InternedVariableKindsInner, + InternedSubstitutionInner, +); impl chalk_ir::interner::Interner for Interner { type InternedType = Arc>; @@ -42,7 +49,7 @@ impl chalk_ir::interner::Interner for Interner { type InternedGenericArg = chalk_ir::GenericArgData; type InternedGoal = Arc>; type InternedGoals = Vec>; - type InternedSubstitution = SmallVec<[GenericArg; 2]>; + type InternedSubstitution = Interned; type InternedProgramClause = Arc>; type InternedProgramClauses = Arc<[chalk_ir::ProgramClause]>; type InternedQuantifiedWhereClauses = Vec>; @@ -107,7 +114,7 @@ impl chalk_ir::interner::Interner for Interner { } fn debug_generic_arg( - parameter: &GenericArg, + parameter: &GenericArg, fmt: &mut fmt::Formatter<'_>, ) -> Option { tls::with_current_program(|prog| Some(prog?.debug_generic_arg(parameter, fmt))) @@ -272,16 +279,16 @@ impl chalk_ir::interner::Interner for Interner { fn intern_substitution( &self, - data: impl IntoIterator, E>>, + data: impl IntoIterator>, ) -> Result { - data.into_iter().collect() + Ok(Interned::new(InternedSubstitutionInner(data.into_iter().collect::, _>>()?))) } fn substitution_data<'a>( &self, substitution: &'a Self::InternedSubstitution, - ) -> &'a [GenericArg] { - substitution + ) -> &'a [GenericArg] { + &substitution.as_ref().0 } fn intern_program_clause( diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 7818f6387..e78581ea5 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -99,7 +99,7 @@ pub(super) fn generic_predicate_to_inline_bound( // have the expected self type return None; } - let args_no_self = trait_ref.substitution.interned()[1..] + let args_no_self = trait_ref.substitution.as_slice(&Interner)[1..] .iter() .map(|ty| ty.clone().cast(&Interner)) .collect(); @@ -111,7 +111,7 @@ pub(super) fn generic_predicate_to_inline_bound( return None; } let trait_ = projection_ty.trait_(db); - let args_no_self = projection_ty.substitution.interned()[1..] + let args_no_self = projection_ty.substitution.as_slice(&Interner)[1..] .iter() .map(|ty| ty.clone().cast(&Interner)) .collect(); -- cgit v1.2.3 From 37cb6805afc397adf769391cec99b2e11d0d52e0 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 18:32:20 +0200 Subject: Intern types Performance about the same, memory reduced by ~5%. --- crates/hir_ty/src/traits/chalk/interner.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index 17e056f03..83787b8c0 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -36,13 +36,17 @@ pub struct InternedVariableKindsInner(Vec>); #[derive(PartialEq, Eq, Hash, Debug)] pub struct InternedSubstitutionInner(SmallVec<[GenericArg; 2]>); +#[derive(PartialEq, Eq, Hash, Debug)] +pub struct InternedTypeInner(chalk_ir::TyData); + impl_internable!( InternedVariableKindsInner, InternedSubstitutionInner, + InternedTypeInner, ); impl chalk_ir::interner::Interner for Interner { - type InternedType = Arc>; + type InternedType = Interned; type InternedLifetime = chalk_ir::LifetimeData; type InternedConst = Arc>; type InternedConcreteConst = (); @@ -209,11 +213,11 @@ impl chalk_ir::interner::Interner for Interner { fn intern_ty(&self, kind: chalk_ir::TyKind) -> Self::InternedType { let flags = kind.compute_flags(self); - Arc::new(chalk_ir::TyData { kind, flags }) + Interned::new(InternedTypeInner(chalk_ir::TyData { kind, flags })) } fn ty_data<'a>(&self, ty: &'a Self::InternedType) -> &'a chalk_ir::TyData { - ty + &ty.0 } fn intern_lifetime(&self, lifetime: chalk_ir::LifetimeData) -> Self::InternedLifetime { -- cgit v1.2.3 From 90656f86745afe073a5b590d51b22ec5f6b60e1b Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 18:45:07 +0200 Subject: Intern consts & lifetimes Slight memory usage reduction. --- crates/hir_ty/src/chalk_ext.rs | 2 +- crates/hir_ty/src/traits/chalk/interner.rs | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index 28ed3aac6..8c4542956 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs @@ -75,7 +75,7 @@ impl TyExt for Ty { } fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> { match self.kind(&Interner) { - TyKind::Ref(mutability, lifetime, ty) => Some((ty, *lifetime, *mutability)), + TyKind::Ref(mutability, lifetime, ty) => Some((ty, lifetime.clone(), *mutability)), _ => None, } } diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index 83787b8c0..3a6ceef05 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -39,16 +39,29 @@ pub struct InternedSubstitutionInner(SmallVec<[GenericArg; 2]>); #[derive(PartialEq, Eq, Hash, Debug)] pub struct InternedTypeInner(chalk_ir::TyData); +#[derive(PartialEq, Eq, Hash, Debug)] +pub struct InternedWrapper(T); + +impl std::ops::Deref for InternedWrapper { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + impl_internable!( InternedVariableKindsInner, InternedSubstitutionInner, InternedTypeInner, + InternedWrapper>, + InternedWrapper>, ); impl chalk_ir::interner::Interner for Interner { type InternedType = Interned; - type InternedLifetime = chalk_ir::LifetimeData; - type InternedConst = Arc>; + type InternedLifetime = Interned>>; + type InternedConst = Interned>>; type InternedConcreteConst = (); type InternedGenericArg = chalk_ir::GenericArgData; type InternedGoal = Arc>; @@ -221,22 +234,22 @@ impl chalk_ir::interner::Interner for Interner { } fn intern_lifetime(&self, lifetime: chalk_ir::LifetimeData) -> Self::InternedLifetime { - lifetime + Interned::new(InternedWrapper(lifetime)) } fn lifetime_data<'a>( &self, lifetime: &'a Self::InternedLifetime, ) -> &'a chalk_ir::LifetimeData { - lifetime + &lifetime.0 } fn intern_const(&self, constant: chalk_ir::ConstData) -> Self::InternedConst { - Arc::new(constant) + Interned::new(InternedWrapper(constant)) } fn const_data<'a>(&self, constant: &'a Self::InternedConst) -> &'a chalk_ir::ConstData { - constant + &constant.0 } fn const_eq( -- cgit v1.2.3 From 317c4b972f5f8da9b5007b8a441a5a79a60a4cd5 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 20:06:19 +0200 Subject: Intern CanonicalVarKinds Slight savings in performance and memory. --- crates/hir_ty/src/traits/chalk/interner.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index 3a6ceef05..d63252382 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -56,6 +56,7 @@ impl_internable!( InternedTypeInner, InternedWrapper>, InternedWrapper>, + InternedWrapper>>, ); impl chalk_ir::interner::Interner for Interner { @@ -71,7 +72,7 @@ impl chalk_ir::interner::Interner for Interner { type InternedProgramClauses = Arc<[chalk_ir::ProgramClause]>; type InternedQuantifiedWhereClauses = Vec>; type InternedVariableKinds = Interned; - type InternedCanonicalVarKinds = Vec>; + type InternedCanonicalVarKinds = Interned>>>; type InternedConstraints = Vec>>; type InternedVariances = Arc<[chalk_ir::Variance]>; type DefId = InternId; @@ -370,7 +371,7 @@ impl chalk_ir::interner::Interner for Interner { &self, data: impl IntoIterator, E>>, ) -> Result { - data.into_iter().collect() + Ok(Interned::new(InternedWrapper(data.into_iter().collect::>()?))) } fn canonical_var_kinds_data<'a>( -- cgit v1.2.3 From 15b0b55b4e73fb25d94dd9bcc49a2b4ed5156dc1 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 20:13:21 +0200 Subject: Intern ProgramClauses --- crates/hir_ty/src/traits/chalk/interner.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index d63252382..11fa2622d 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -57,6 +57,7 @@ impl_internable!( InternedWrapper>, InternedWrapper>, InternedWrapper>>, + InternedWrapper>>, ); impl chalk_ir::interner::Interner for Interner { @@ -69,7 +70,7 @@ impl chalk_ir::interner::Interner for Interner { type InternedGoals = Vec>; type InternedSubstitution = Interned; type InternedProgramClause = Arc>; - type InternedProgramClauses = Arc<[chalk_ir::ProgramClause]>; + type InternedProgramClauses = Interned>>>; type InternedQuantifiedWhereClauses = Vec>; type InternedVariableKinds = Interned; type InternedCanonicalVarKinds = Interned>>>; @@ -327,7 +328,7 @@ impl chalk_ir::interner::Interner for Interner { &self, data: impl IntoIterator, E>>, ) -> Result { - data.into_iter().collect() + Ok(Interned::new(InternedWrapper(data.into_iter().collect::>()?))) } fn program_clauses_data<'a>( -- cgit v1.2.3 From 566200342a2bed328d3e73bbb8b8bbe6d49f255c Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 20:18:51 +0200 Subject: Intern QuantifiedWhereClauses Slight performance and memory usage improvement. --- crates/hir_ty/src/traits/chalk/interner.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index 11fa2622d..ea42be932 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -58,6 +58,7 @@ impl_internable!( InternedWrapper>, InternedWrapper>>, InternedWrapper>>, + InternedWrapper>>, ); impl chalk_ir::interner::Interner for Interner { @@ -71,7 +72,7 @@ impl chalk_ir::interner::Interner for Interner { type InternedSubstitution = Interned; type InternedProgramClause = Arc>; type InternedProgramClauses = Interned>>>; - type InternedQuantifiedWhereClauses = Vec>; + type InternedQuantifiedWhereClauses = Interned>>>; type InternedVariableKinds = Interned; type InternedCanonicalVarKinds = Interned>>>; type InternedConstraints = Vec>>; @@ -342,7 +343,7 @@ impl chalk_ir::interner::Interner for Interner { &self, data: impl IntoIterator, E>>, ) -> Result { - data.into_iter().collect() + Ok(Interned::new(InternedWrapper(data.into_iter().collect::>()?))) } fn quantified_where_clauses_data<'a>( -- cgit v1.2.3 From f778e50b7183e727ec7ec1091b9b64471524704a Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 20:39:43 +0200 Subject: Don't intern ProgramClause at all This seems to work best performance/memory-wise. --- crates/hir_ty/src/traits/chalk/interner.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index ea42be932..f35695f6f 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -70,7 +70,7 @@ impl chalk_ir::interner::Interner for Interner { type InternedGoal = Arc>; type InternedGoals = Vec>; type InternedSubstitution = Interned; - type InternedProgramClause = Arc>; + type InternedProgramClause = chalk_ir::ProgramClauseData; type InternedProgramClauses = Interned>>>; type InternedQuantifiedWhereClauses = Interned>>>; type InternedVariableKinds = Interned; @@ -315,7 +315,7 @@ impl chalk_ir::interner::Interner for Interner { &self, data: chalk_ir::ProgramClauseData, ) -> Self::InternedProgramClause { - Arc::new(data) + data } fn program_clause_data<'a>( -- cgit v1.2.3 From 417473aa3d53fb1a094031251af37ecd71e0cc09 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 20:55:03 +0200 Subject: Intern Variances This may be a slight performance improvement. --- crates/hir_ty/src/traits/chalk/interner.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index f35695f6f..f2c9a0d4b 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -59,6 +59,7 @@ impl_internable!( InternedWrapper>>, InternedWrapper>>, InternedWrapper>>, + InternedWrapper>, ); impl chalk_ir::interner::Interner for Interner { @@ -76,7 +77,7 @@ impl chalk_ir::interner::Interner for Interner { type InternedVariableKinds = Interned; type InternedCanonicalVarKinds = Interned>>>; type InternedConstraints = Vec>>; - type InternedVariances = Arc<[chalk_ir::Variance]>; + type InternedVariances = Interned>>; type DefId = InternId; type InternedAdtId = hir_def::AdtId; type Identifier = TypeAliasId; @@ -413,7 +414,7 @@ impl chalk_ir::interner::Interner for Interner { &self, data: impl IntoIterator>, ) -> Result { - data.into_iter().collect() + Ok(Interned::new(InternedWrapper(data.into_iter().collect::>()?))) } fn variances_data<'a>( -- cgit v1.2.3 From 37ff15ad835ee4ba6d231cac8b1adbd301aec20b Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 21:15:01 +0200 Subject: Cleanup --- crates/hir_ty/src/infer/expr.rs | 6 ++++- crates/hir_ty/src/traits/chalk/interner.rs | 37 +++++++++++------------------- 2 files changed, 19 insertions(+), 24 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index ee6763926..7961f4a52 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -462,7 +462,11 @@ impl<'a> InferenceContext<'a> { }; match canonicalized.decanonicalize_ty(derefed_ty.value).kind(&Interner) { TyKind::Tuple(_, substs) => name.as_tuple_index().and_then(|idx| { - substs.as_slice(&Interner).get(idx).map(|a| a.assert_ty_ref(&Interner)).cloned() + substs + .as_slice(&Interner) + .get(idx) + .map(|a| a.assert_ty_ref(&Interner)) + .cloned() }), TyKind::Adt(AdtId(hir_def::AdtId::StructId(s)), parameters) => { let local_id = self.db.struct_data(*s).variant_data.field(name)?; diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index f2c9a0d4b..b6a3cec6d 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -2,13 +2,13 @@ //! representation of the various objects Chalk deals with (types, goals etc.). use super::tls; +use crate::GenericArg; use base_db::salsa::InternId; use chalk_ir::{Goal, GoalData}; use hir_def::{ intern::{impl_internable, InternStorage, Internable, Interned}, TypeAliasId, }; -use crate::GenericArg; use smallvec::SmallVec; use std::{fmt, sync::Arc}; @@ -30,15 +30,6 @@ pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId; pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum; pub(crate) type Variances = chalk_ir::Variances; -#[derive(PartialEq, Eq, Hash, Debug)] -pub struct InternedVariableKindsInner(Vec>); - -#[derive(PartialEq, Eq, Hash, Debug)] -pub struct InternedSubstitutionInner(SmallVec<[GenericArg; 2]>); - -#[derive(PartialEq, Eq, Hash, Debug)] -pub struct InternedTypeInner(chalk_ir::TyData); - #[derive(PartialEq, Eq, Hash, Debug)] pub struct InternedWrapper(T); @@ -51,9 +42,9 @@ impl std::ops::Deref for InternedWrapper { } impl_internable!( - InternedVariableKindsInner, - InternedSubstitutionInner, - InternedTypeInner, + InternedWrapper>>, + InternedWrapper>, + InternedWrapper>, InternedWrapper>, InternedWrapper>, InternedWrapper>>, @@ -63,19 +54,21 @@ impl_internable!( ); impl chalk_ir::interner::Interner for Interner { - type InternedType = Interned; + type InternedType = Interned>>; type InternedLifetime = Interned>>; type InternedConst = Interned>>; type InternedConcreteConst = (); type InternedGenericArg = chalk_ir::GenericArgData; type InternedGoal = Arc>; type InternedGoals = Vec>; - type InternedSubstitution = Interned; + type InternedSubstitution = Interned>>; type InternedProgramClause = chalk_ir::ProgramClauseData; type InternedProgramClauses = Interned>>>; - type InternedQuantifiedWhereClauses = Interned>>>; - type InternedVariableKinds = Interned; - type InternedCanonicalVarKinds = Interned>>>; + type InternedQuantifiedWhereClauses = + Interned>>>; + type InternedVariableKinds = Interned>>>; + type InternedCanonicalVarKinds = + Interned>>>; type InternedConstraints = Vec>>; type InternedVariances = Interned>>; type DefId = InternId; @@ -230,7 +223,7 @@ impl chalk_ir::interner::Interner for Interner { fn intern_ty(&self, kind: chalk_ir::TyKind) -> Self::InternedType { let flags = kind.compute_flags(self); - Interned::new(InternedTypeInner(chalk_ir::TyData { kind, flags })) + Interned::new(InternedWrapper(chalk_ir::TyData { kind, flags })) } fn ty_data<'a>(&self, ty: &'a Self::InternedType) -> &'a chalk_ir::TyData { @@ -302,7 +295,7 @@ impl chalk_ir::interner::Interner for Interner { &self, data: impl IntoIterator>, ) -> Result { - Ok(Interned::new(InternedSubstitutionInner(data.into_iter().collect::, _>>()?))) + Ok(Interned::new(InternedWrapper(data.into_iter().collect::>()?))) } fn substitution_data<'a>( @@ -358,9 +351,7 @@ impl chalk_ir::interner::Interner for Interner { &self, data: impl IntoIterator, E>>, ) -> Result { - Ok(Interned::new(InternedVariableKindsInner( - data.into_iter().collect::, E>>()?, - ))) + Ok(Interned::new(InternedWrapper(data.into_iter().collect::>()?))) } fn variable_kinds_data<'a>( -- cgit v1.2.3