Skip to content

Commit 0703de0

Browse files
committed
Auto merge of #150859 - nnethercote:opt-Canonicalizer-flag-checks, r=lcnr
Optimize canonicalizer flag checks. The most important change here relates to type folding: we now check the flags up front, instead of doing it in `inner_fold_ty` after checking the cache and doing a match. This is a small perf win, and matches other similar folders (e.g. `CanonicalInstantiator`). Likewise for const folding, we now check the flags first. (There is no cache for const folding.) Elsewhere we don't check flags before folding a predicate (unnecessary, because `fold_predicate` already checks the flags itself before doing anything else), and invert the flag checks in a couple of methods to match the standard order. r? @lcnr
2 parents 2f1bd3f + 35ad170 commit 0703de0

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

compiler/rustc_next_trait_solver/src/canonical/canonicalizer.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,12 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
221221
};
222222

223223
let predicate = input.goal.predicate;
224-
let predicate = if predicate.has_type_flags(NEEDS_CANONICAL) {
225-
predicate.fold_with(&mut rest_canonicalizer)
226-
} else {
227-
predicate
228-
};
224+
let predicate = predicate.fold_with(&mut rest_canonicalizer);
229225
let goal = Goal { param_env, predicate };
230226

231227
let predefined_opaques_in_body = input.predefined_opaques_in_body;
232228
let predefined_opaques_in_body =
233-
if input.predefined_opaques_in_body.has_type_flags(NEEDS_CANONICAL) {
229+
if predefined_opaques_in_body.has_type_flags(NEEDS_CANONICAL) {
234230
predefined_opaques_in_body.fold_with(&mut rest_canonicalizer)
235231
} else {
236232
predefined_opaques_in_body
@@ -397,11 +393,7 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
397393
| ty::Alias(_, _)
398394
| ty::Bound(_, _)
399395
| ty::Error(_) => {
400-
return if t.has_type_flags(NEEDS_CANONICAL) {
401-
ensure_sufficient_stack(|| t.super_fold_with(self))
402-
} else {
403-
t
404-
};
396+
return ensure_sufficient_stack(|| t.super_fold_with(self));
405397
}
406398
};
407399

@@ -483,7 +475,9 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
483475
}
484476

485477
fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
486-
if let Some(&ty) = self.cache.get(&t) {
478+
if !t.flags().intersects(NEEDS_CANONICAL) {
479+
t
480+
} else if let Some(&ty) = self.cache.get(&t) {
487481
ty
488482
} else {
489483
let res = self.inner_fold_ty(t);
@@ -494,6 +488,10 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
494488
}
495489

496490
fn fold_const(&mut self, c: I::Const) -> I::Const {
491+
if !c.flags().intersects(NEEDS_CANONICAL) {
492+
return c;
493+
}
494+
497495
let kind = match c.kind() {
498496
ty::ConstKind::Infer(i) => match i {
499497
ty::InferConst::Var(vid) => {
@@ -533,9 +531,7 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
533531
| ty::ConstKind::Unevaluated(_)
534532
| ty::ConstKind::Value(_)
535533
| ty::ConstKind::Error(_)
536-
| ty::ConstKind::Expr(_) => {
537-
return if c.has_type_flags(NEEDS_CANONICAL) { c.super_fold_with(self) } else { c };
538-
}
534+
| ty::ConstKind::Expr(_) => return c.super_fold_with(self),
539535
};
540536

541537
let var = self.get_or_insert_bound_var(c, kind);
@@ -544,7 +540,7 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
544540
}
545541

546542
fn fold_predicate(&mut self, p: I::Predicate) -> I::Predicate {
547-
if p.flags().intersects(NEEDS_CANONICAL) { p.super_fold_with(self) } else { p }
543+
if !p.flags().intersects(NEEDS_CANONICAL) { p } else { p.super_fold_with(self) }
548544
}
549545

550546
fn fold_clauses(&mut self, c: I::Clauses) -> I::Clauses {
@@ -555,6 +551,6 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
555551
panic!("erasing 'static in env")
556552
}
557553
}
558-
if c.flags().intersects(NEEDS_CANONICAL) { c.super_fold_with(self) } else { c }
554+
if !c.flags().intersects(NEEDS_CANONICAL) { c } else { c.super_fold_with(self) }
559555
}
560556
}

0 commit comments

Comments
 (0)