Skip to content

Commit 95b9dc1

Browse files
committed
improve caching
1 parent f666aac commit 95b9dc1

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

compiler/rustc_infer/src/infer/relate/type_relating.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for TypeRelating<'_, '_, 'tcx> {
8585
let a = infcx.shallow_resolve(a);
8686
let b = infcx.shallow_resolve(b);
8787

88-
if self.cache.contains(&(self.ambient_variance, a, b)) {
88+
if infcx.next_trait_solver() && self.cache.contains(&(self.ambient_variance, a, b)) {
8989
return Ok(a);
9090
}
9191

@@ -171,7 +171,9 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for TypeRelating<'_, '_, 'tcx> {
171171
}
172172
}
173173

174-
assert!(self.cache.insert((self.ambient_variance, a, b)));
174+
if infcx.next_trait_solver() {
175+
assert!(self.cache.insert((self.ambient_variance, a, b)));
176+
}
175177

176178
Ok(a)
177179
}

compiler/rustc_next_trait_solver/src/resolve.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_data_structures::sso::SsoHashMap;
1+
use rustc_type_ir::data_structures::HashMap;
22
use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
33
use rustc_type_ir::inherent::*;
44
use rustc_type_ir::visit::TypeVisitableExt;
@@ -16,7 +16,7 @@ where
1616
I: Interner,
1717
{
1818
delegate: &'a D,
19-
cache: SsoHashMap<I::Ty, I::Ty>,
19+
cache: HashMap<I::Ty, I::Ty>,
2020
}
2121

2222
impl<'a, D: SolverDelegate> EagerResolver<'a, D> {
@@ -31,11 +31,7 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for EagerResolv
3131
}
3232

3333
fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
34-
if let Some(&ty) = self.cache.get(&t) {
35-
return ty;
36-
}
37-
38-
let res = match t.kind() {
34+
match t.kind() {
3935
ty::Infer(ty::TyVar(vid)) => {
4036
let resolved = self.delegate.opportunistic_resolve_ty_var(vid);
4137
if t != resolved && resolved.has_infer() {
@@ -48,15 +44,17 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for EagerResolv
4844
ty::Infer(ty::FloatVar(vid)) => self.delegate.opportunistic_resolve_float_var(vid),
4945
_ => {
5046
if t.has_infer() {
51-
t.super_fold_with(self)
47+
if let Some(&ty) = self.cache.get(&t) {
48+
return ty;
49+
}
50+
let res = t.super_fold_with(self);
51+
assert!(self.cache.insert(t, res).is_none());
52+
res
5253
} else {
5354
t
5455
}
5556
}
56-
};
57-
58-
assert!(self.cache.insert(t, res).is_none());
59-
res
57+
}
6058
}
6159

6260
fn fold_region(&mut self, r: I::Region) -> I::Region {

0 commit comments

Comments
 (0)