Skip to content

Commit 35ad170

Browse files
committed
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.
1 parent e29fcf4 commit 35ad170

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

compiler/rustc_next_trait_solver/src/canonical/canonicalizer.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
109109
let (max_universe, variables) = canonicalizer.finalize();
110110
Canonical { max_universe, variables, value }
111111
}
112+
112113
fn canonicalize_param_env(
113114
delegate: &'a D,
114115
variables: &'a mut Vec<I::GenericArg>,
@@ -215,16 +216,12 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
215216
};
216217

217218
let predicate = input.goal.predicate;
218-
let predicate = if predicate.has_type_flags(NEEDS_CANONICAL) {
219-
predicate.fold_with(&mut rest_canonicalizer)
220-
} else {
221-
predicate
222-
};
219+
let predicate = predicate.fold_with(&mut rest_canonicalizer);
223220
let goal = Goal { param_env, predicate };
224221

225222
let predefined_opaques_in_body = input.predefined_opaques_in_body;
226223
let predefined_opaques_in_body =
227-
if input.predefined_opaques_in_body.has_type_flags(NEEDS_CANONICAL) {
224+
if predefined_opaques_in_body.has_type_flags(NEEDS_CANONICAL) {
228225
predefined_opaques_in_body.fold_with(&mut rest_canonicalizer)
229226
} else {
230227
predefined_opaques_in_body
@@ -391,11 +388,7 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
391388
| ty::Alias(_, _)
392389
| ty::Bound(_, _)
393390
| ty::Error(_) => {
394-
return if t.has_type_flags(NEEDS_CANONICAL) {
395-
ensure_sufficient_stack(|| t.super_fold_with(self))
396-
} else {
397-
t
398-
};
391+
return ensure_sufficient_stack(|| t.super_fold_with(self));
399392
}
400393
};
401394

@@ -477,7 +470,9 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
477470
}
478471

479472
fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
480-
if let Some(&ty) = self.cache.get(&t) {
473+
if !t.flags().intersects(NEEDS_CANONICAL) {
474+
t
475+
} else if let Some(&ty) = self.cache.get(&t) {
481476
ty
482477
} else {
483478
let res = self.inner_fold_ty(t);
@@ -488,6 +483,10 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
488483
}
489484

490485
fn fold_const(&mut self, c: I::Const) -> I::Const {
486+
if !c.flags().intersects(NEEDS_CANONICAL) {
487+
return c;
488+
}
489+
491490
let kind = match c.kind() {
492491
ty::ConstKind::Infer(i) => match i {
493492
ty::InferConst::Var(vid) => {
@@ -527,9 +526,7 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
527526
| ty::ConstKind::Unevaluated(_)
528527
| ty::ConstKind::Value(_)
529528
| ty::ConstKind::Error(_)
530-
| ty::ConstKind::Expr(_) => {
531-
return if c.has_type_flags(NEEDS_CANONICAL) { c.super_fold_with(self) } else { c };
532-
}
529+
| ty::ConstKind::Expr(_) => return c.super_fold_with(self),
533530
};
534531

535532
let var = self.get_or_insert_bound_var(c, kind);
@@ -538,7 +535,7 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
538535
}
539536

540537
fn fold_predicate(&mut self, p: I::Predicate) -> I::Predicate {
541-
if p.flags().intersects(NEEDS_CANONICAL) { p.super_fold_with(self) } else { p }
538+
if !p.flags().intersects(NEEDS_CANONICAL) { p } else { p.super_fold_with(self) }
542539
}
543540

544541
fn fold_clauses(&mut self, c: I::Clauses) -> I::Clauses {
@@ -549,6 +546,6 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
549546
panic!("erasing 'static in env")
550547
}
551548
}
552-
if c.flags().intersects(NEEDS_CANONICAL) { c.super_fold_with(self) } else { c }
549+
if !c.flags().intersects(NEEDS_CANONICAL) { c } else { c.super_fold_with(self) }
553550
}
554551
}

0 commit comments

Comments
 (0)