Skip to content

Commit 3eebe05

Browse files
committed
Auto merge of #67079 - nnethercote:optimize-shallow_resolve_changed, r=nikomatsakis
Optimize `shallow_resolve_changed` r? @nikomatsakis
2 parents f284f8b + 21f35bc commit 3eebe05

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

src/librustc/infer/mod.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -1618,37 +1618,37 @@ impl<'a, 'tcx> ShallowResolver<'a, 'tcx> {
16181618
// inlined, despite being large, because it has only two call sites that
16191619
// are extremely hot.
16201620
#[inline(always)]
1621-
pub fn shallow_resolve_changed(&mut self, typ: Ty<'tcx>) -> bool {
1622-
match typ.kind {
1623-
ty::Infer(ty::TyVar(v)) => {
1621+
pub fn shallow_resolve_changed(&self, infer: ty::InferTy) -> bool {
1622+
match infer {
1623+
ty::TyVar(v) => {
16241624
use self::type_variable::TypeVariableValue;
16251625

1626-
// See the comment in `shallow_resolve()`.
1626+
// If `inlined_probe` returns a `Known` value its `kind` never
1627+
// matches `infer`.
16271628
match self.infcx.type_variables.borrow_mut().inlined_probe(v) {
1628-
TypeVariableValue::Known { value: t } => self.fold_ty(t) != typ,
16291629
TypeVariableValue::Unknown { .. } => false,
1630+
TypeVariableValue::Known { .. } => true,
16301631
}
16311632
}
16321633

1633-
ty::Infer(ty::IntVar(v)) => {
1634-
match self.infcx.int_unification_table.borrow_mut().inlined_probe_value(v) {
1635-
Some(v) => v.to_type(self.infcx.tcx) != typ,
1636-
None => false,
1637-
}
1634+
ty::IntVar(v) => {
1635+
// If inlined_probe_value returns a value it's always a
1636+
// `ty::Int(_)` or `ty::UInt(_)`, which nevers matches a
1637+
// `ty::Infer(_)`.
1638+
self.infcx.int_unification_table.borrow_mut().inlined_probe_value(v).is_some()
16381639
}
16391640

1640-
ty::Infer(ty::FloatVar(v)) => {
1641+
ty::FloatVar(v) => {
1642+
// If inlined_probe_value returns a value it's always a
1643+
// `ty::Float(_)`, which nevers matches a `ty::Infer(_)`.
1644+
//
16411645
// Not `inlined_probe_value(v)` because this call site is colder.
1642-
match self.infcx.float_unification_table.borrow_mut().probe_value(v) {
1643-
Some(v) => v.to_type(self.infcx.tcx) != typ,
1644-
None => false,
1645-
}
1646+
self.infcx.float_unification_table.borrow_mut().probe_value(v).is_some()
16461647
}
16471648

1648-
_ => false,
1649+
_ => unreachable!(),
16491650
}
16501651
}
1651-
16521652
}
16531653

16541654
impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> {

src/librustc/traits/fulfill.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub struct FulfillmentContext<'tcx> {
6565
#[derive(Clone, Debug)]
6666
pub struct PendingPredicateObligation<'tcx> {
6767
pub obligation: PredicateObligation<'tcx>,
68-
pub stalled_on: Vec<Ty<'tcx>>,
68+
pub stalled_on: Vec<ty::InferTy>,
6969
}
7070

7171
// `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
@@ -263,8 +263,8 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
263263
// Match arms are in order of frequency, which matters because this
264264
// code is so hot. 1 and 0 dominate; 2+ is fairly rare.
265265
1 => {
266-
let ty = pending_obligation.stalled_on[0];
267-
ShallowResolver::new(self.selcx.infcx()).shallow_resolve_changed(ty)
266+
let infer = pending_obligation.stalled_on[0];
267+
ShallowResolver::new(self.selcx.infcx()).shallow_resolve_changed(infer)
268268
}
269269
0 => {
270270
// In this case we haven't changed, but wish to make a change.
@@ -274,8 +274,8 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
274274
// This `for` loop was once a call to `all()`, but this lower-level
275275
// form was a perf win. See #64545 for details.
276276
(|| {
277-
for &ty in &pending_obligation.stalled_on {
278-
if ShallowResolver::new(self.selcx.infcx()).shallow_resolve_changed(ty) {
277+
for &infer in &pending_obligation.stalled_on {
278+
if ShallowResolver::new(self.selcx.infcx()).shallow_resolve_changed(infer) {
279279
return true;
280280
}
281281
}
@@ -305,6 +305,13 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
305305

306306
debug!("process_obligation: obligation = {:?} cause = {:?}", obligation, obligation.cause);
307307

308+
fn infer_ty(ty: Ty<'tcx>) -> ty::InferTy {
309+
match ty.kind {
310+
ty::Infer(infer) => infer,
311+
_ => panic!(),
312+
}
313+
}
314+
308315
match obligation.predicate {
309316
ty::Predicate::Trait(ref data) => {
310317
let trait_obligation = obligation.with(data.clone());
@@ -459,7 +466,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
459466
obligation.cause.span,
460467
) {
461468
None => {
462-
pending_obligation.stalled_on = vec![ty];
469+
pending_obligation.stalled_on = vec![infer_ty(ty)];
463470
ProcessResult::Unchanged
464471
}
465472
Some(os) => ProcessResult::Changed(mk_pending(os))
@@ -472,8 +479,8 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
472479
subtype) {
473480
None => {
474481
// None means that both are unresolved.
475-
pending_obligation.stalled_on = vec![subtype.skip_binder().a,
476-
subtype.skip_binder().b];
482+
pending_obligation.stalled_on = vec![infer_ty(subtype.skip_binder().a),
483+
infer_ty(subtype.skip_binder().b)];
477484
ProcessResult::Unchanged
478485
}
479486
Some(Ok(ok)) => {
@@ -517,7 +524,8 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
517524
))
518525
}
519526
} else {
520-
pending_obligation.stalled_on = substs.types().collect();
527+
pending_obligation.stalled_on =
528+
substs.types().map(|ty| infer_ty(ty)).collect();
521529
ProcessResult::Unchanged
522530
}
523531
}
@@ -542,13 +550,13 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
542550
fn trait_ref_type_vars<'a, 'tcx>(
543551
selcx: &mut SelectionContext<'a, 'tcx>,
544552
t: ty::PolyTraitRef<'tcx>,
545-
) -> Vec<Ty<'tcx>> {
553+
) -> Vec<ty::InferTy> {
546554
t.skip_binder() // ok b/c this check doesn't care about regions
547555
.input_types()
548556
.map(|t| selcx.infcx().resolve_vars_if_possible(&t))
549557
.filter(|t| t.has_infer_types())
550558
.flat_map(|t| t.walk())
551-
.filter(|t| match t.kind { ty::Infer(_) => true, _ => false })
559+
.filter_map(|t| match t.kind { ty::Infer(infer) => Some(infer), _ => None })
552560
.collect()
553561
}
554562

0 commit comments

Comments
 (0)