Skip to content

Commit 84bbf0f

Browse files
committed
Auto merge of #160885 - nnethercote:opt-probing-in-shallow_resolve, r=<try>
Optimize probing in `shallow_resolve`
2 parents fdda4c6 + 92272be commit 84bbf0f

1 file changed

Lines changed: 26 additions & 17 deletions

File tree

  • compiler/rustc_infer/src/infer

‎compiler/rustc_infer/src/infer/mod.rs‎

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,24 +1235,33 @@ impl<'tcx> InferCtxt<'tcx> {
12351235
if let ty::Infer(v) = *ty.kind() {
12361236
match v {
12371237
ty::TyVar(v) => {
1238-
// Not entirely obvious: if `typ` is a type variable,
1239-
// it can be resolved to an int/float variable, which
1240-
// can then be recursively resolved, hence the
1241-
// recursion. Note though that we prevent type
1242-
// variables from unifying to other type variables
1243-
// directly (though they may be embedded
1244-
// structurally), and we prevent cycles in any case,
1245-
// so this recursion should always be of very limited
1246-
// depth.
1238+
// Not entirely obvious: if `ty` is a type variable, it can be resolved to an
1239+
// int/float variable, which can then be recursively resolved, hence the
1240+
// recursion. Note though that we prevent type variables from unifying to other
1241+
// type variables directly (though they may be embedded structurally), and we
1242+
// prevent cycles in any case, so this recursion should always be of very
1243+
// limited depth.
12471244
//
1248-
// Note: if these two lines are combined into one we get
1249-
// dynamic borrow errors on `self.inner`.
1250-
let (root_vid, value) =
1251-
self.inner.borrow_mut().type_variables().probe_with_root_vid(v);
1252-
value.known().map_or_else(
1253-
|| if root_vid == v { ty } else { Ty::new_var(self.tcx, root_vid) },
1254-
|t| self.shallow_resolve(t),
1255-
)
1245+
// It's a small perf win to do a fast lookup first in the hope that `v` is the
1246+
// root, and then fall back if not. (Note: the same fast/slow split isn't worth
1247+
// doing for the `IntVar`/`FloatVar` cases below.)
1248+
let value = self.inner.borrow().try_type_variables_probe_ref(v).copied();
1249+
match value {
1250+
Some(value) => {
1251+
// `v` is the root.
1252+
value.known().map_or(ty, |t| self.shallow_resolve(t))
1253+
}
1254+
None => {
1255+
// The slower case. Note: the borrow must be completed before the
1256+
// recursive call occurs.
1257+
let (root_vid, value) =
1258+
self.inner.borrow_mut().type_variables().probe_with_root_vid(v);
1259+
value.known().map_or_else(
1260+
|| Ty::new_var(self.tcx, root_vid),
1261+
|t| self.shallow_resolve(t),
1262+
)
1263+
}
1264+
}
12561265
}
12571266

12581267
ty::IntVar(v) => {

0 commit comments

Comments
 (0)