From 0e8c4503bf4754f4437d8bd45a110b9d0ec671e0 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 1 Apr 2021 21:45:44 +0200 Subject: Don't recheck obligations if we have learned nothing new This is just the most trivial check: If no inference variables have been updated, and there are no new obligations, we can just skip trying to solve them again. We could be smarter about it, but this already helps quite a bit, and I don't want to touch this too much before we replace the inference table by Chalk's. Fixes #8263 (well, improves it quite a bit). --- crates/hir_ty/src/infer.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'crates/hir_ty/src/infer.rs') diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index e4407ff50..497a1beb7 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs @@ -210,6 +210,7 @@ struct InferenceContext<'a> { table: unify::InferenceTable, trait_env: Arc, obligations: Vec, + last_obligations_check: Option, result: InferenceResult, /// The return type of the function being inferred, or the closure if we're /// currently within one. @@ -245,6 +246,7 @@ impl<'a> InferenceContext<'a> { result: InferenceResult::default(), table: unify::InferenceTable::new(), obligations: Vec::default(), + last_obligations_check: None, return_ty: TyKind::Unknown.intern(&Interner), // set in collect_fn_signature trait_env: owner .as_generic_def_id() @@ -334,6 +336,11 @@ impl<'a> InferenceContext<'a> { } fn resolve_obligations_as_possible(&mut self) { + if self.last_obligations_check == Some(self.table.revision) { + // no change + return; + } + self.last_obligations_check = Some(self.table.revision); let obligations = mem::replace(&mut self.obligations, Vec::new()); for obligation in obligations { let in_env = InEnvironment::new(self.trait_env.env.clone(), obligation.clone()); @@ -360,6 +367,11 @@ impl<'a> InferenceContext<'a> { } } + fn push_obligation(&mut self, o: DomainGoal) { + self.obligations.push(o); + self.last_obligations_check = None; + } + fn unify(&mut self, ty1: &Ty, ty2: &Ty) -> bool { self.table.unify(ty1, ty2) } @@ -408,8 +420,8 @@ impl<'a> InferenceContext<'a> { }), ty: ty.clone(), }; - self.obligations.push(trait_ref.cast(&Interner)); - self.obligations.push(alias_eq.cast(&Interner)); + self.push_obligation(trait_ref.cast(&Interner)); + self.push_obligation(alias_eq.cast(&Interner)); self.resolve_ty_as_possible(ty) } None => self.err_ty(), @@ -436,7 +448,7 @@ impl<'a> InferenceContext<'a> { let var = self.table.new_type_var(); let alias_eq = AliasEq { alias: AliasTy::Projection(proj_ty), ty: var.clone() }; let obligation = alias_eq.cast(&Interner); - self.obligations.push(obligation); + self.push_obligation(obligation); var } -- cgit v1.2.3