Skip to content

Commit

Permalink
improve caching
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Sep 26, 2024
1 parent f666aac commit 95b9dc1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
6 changes: 4 additions & 2 deletions compiler/rustc_infer/src/infer/relate/type_relating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> 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);
}

Expand Down Expand Up @@ -171,7 +171,9 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> 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)
}
Expand Down
22 changes: 10 additions & 12 deletions compiler/rustc_next_trait_solver/src/resolve.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +16,7 @@ where
I: Interner,
{
delegate: &'a D,
cache: SsoHashMap<I::Ty, I::Ty>,
cache: HashMap<I::Ty, I::Ty>,
}

impl<'a, D: SolverDelegate> EagerResolver<'a, D> {
Expand All @@ -31,11 +31,7 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> 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() {
Expand All @@ -48,15 +44,17 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> 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 {
Expand Down

0 comments on commit 95b9dc1

Please sign in to comment.