From 95b9dc1beebab3e2ac2b7375be6f9b7f5b81a52f Mon Sep 17 00:00:00 2001 From: lcnr Date: Thu, 26 Sep 2024 09:59:14 +0200 Subject: [PATCH] improve caching --- .../src/infer/relate/type_relating.rs | 6 +++-- .../rustc_next_trait_solver/src/resolve.rs | 22 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_infer/src/infer/relate/type_relating.rs b/compiler/rustc_infer/src/infer/relate/type_relating.rs index d4ad9c383db3..aafa9cef09f7 100644 --- a/compiler/rustc_infer/src/infer/relate/type_relating.rs +++ b/compiler/rustc_infer/src/infer/relate/type_relating.rs @@ -85,7 +85,7 @@ impl<'tcx> TypeRelation> for TypeRelating<'_, '_, 'tcx> { let a = infcx.shallow_resolve(a); let b = infcx.shallow_resolve(b); - if self.cache.contains(&(self.ambient_variance, a, b)) { + if infcx.next_trait_solver() && self.cache.contains(&(self.ambient_variance, a, b)) { return Ok(a); } @@ -171,7 +171,9 @@ impl<'tcx> TypeRelation> for TypeRelating<'_, '_, 'tcx> { } } - assert!(self.cache.insert((self.ambient_variance, a, b))); + if infcx.next_trait_solver() { + assert!(self.cache.insert((self.ambient_variance, a, b))); + } Ok(a) } diff --git a/compiler/rustc_next_trait_solver/src/resolve.rs b/compiler/rustc_next_trait_solver/src/resolve.rs index f0fd79f6e481..08baf66971be 100644 --- a/compiler/rustc_next_trait_solver/src/resolve.rs +++ b/compiler/rustc_next_trait_solver/src/resolve.rs @@ -1,4 +1,4 @@ -use rustc_data_structures::sso::SsoHashMap; +use rustc_type_ir::data_structures::HashMap; use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable}; use rustc_type_ir::inherent::*; use rustc_type_ir::visit::TypeVisitableExt; @@ -16,7 +16,7 @@ where I: Interner, { delegate: &'a D, - cache: SsoHashMap, + cache: HashMap, } impl<'a, D: SolverDelegate> EagerResolver<'a, D> { @@ -31,11 +31,7 @@ impl, I: Interner> TypeFolder for EagerResolv } fn fold_ty(&mut self, t: I::Ty) -> I::Ty { - if let Some(&ty) = self.cache.get(&t) { - return ty; - } - - let res = match t.kind() { + match t.kind() { ty::Infer(ty::TyVar(vid)) => { let resolved = self.delegate.opportunistic_resolve_ty_var(vid); if t != resolved && resolved.has_infer() { @@ -48,15 +44,17 @@ impl, I: Interner> TypeFolder for EagerResolv ty::Infer(ty::FloatVar(vid)) => self.delegate.opportunistic_resolve_float_var(vid), _ => { if t.has_infer() { - t.super_fold_with(self) + if let Some(&ty) = self.cache.get(&t) { + return ty; + } + let res = t.super_fold_with(self); + assert!(self.cache.insert(t, res).is_none()); + res } else { t } } - }; - - assert!(self.cache.insert(t, res).is_none()); - res + } } fn fold_region(&mut self, r: I::Region) -> I::Region {