introduce trivial_noop to accommodate micro-optimizations

This commit is contained in:
Niko Matsakis 2018-06-06 09:39:49 -04:00
parent 9b4fe44280
commit 7c62461c39
2 changed files with 27 additions and 12 deletions

View File

@ -736,6 +736,10 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
describe_op: impl Fn() -> String,
op: impl TypeOp<'gcx, 'tcx, Output = R>,
) -> Result<R, TypeError<'tcx>> {
if let Some(r) = op.trivial_noop() {
return Ok(r);
}
let (r, opt_data) = self.fully_perform_op_and_get_region_constraint_data(
|| format!("{} at {:?}", describe_op(), locations),
op,
@ -818,11 +822,6 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
sup: Ty<'tcx>,
locations: Locations,
) -> UnitResult<'tcx> {
// Micro-optimization.
if sub == sup {
return Ok(());
}
self.fully_perform_op(
locations,
|| format!("sub_types({:?} <: {:?})", sub, sup),
@ -831,11 +830,6 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
}
fn eq_types(&mut self, a: Ty<'tcx>, b: Ty<'tcx>, locations: Locations) -> UnitResult<'tcx> {
// Micro-optimization.
if a == b {
return Ok(());
}
self.fully_perform_op(
locations,
|| format!("eq_types({:?} = {:?})", a, b),

View File

@ -16,6 +16,9 @@ use rustc::ty::Ty;
pub(super) trait TypeOp<'gcx, 'tcx> {
type Output;
/// Micro-optimization point: true if this is trivially true.
fn trivial_noop(&self) -> Option<Self::Output>;
fn perform(
self,
type_checker: &mut TypeChecker<'_, 'gcx, 'tcx>,
@ -41,6 +44,10 @@ where
{
type Output = R;
fn trivial_noop(&self) -> Option<Self::Output> {
None
}
fn perform(self, type_checker: &mut TypeChecker<'_, 'gcx, 'tcx>) -> InferResult<'tcx, R> {
(self.closure)(type_checker)
}
@ -60,6 +67,14 @@ impl<'tcx> Subtype<'tcx> {
impl<'gcx, 'tcx> TypeOp<'gcx, 'tcx> for Subtype<'tcx> {
type Output = ();
fn trivial_noop(&self) -> Option<Self::Output> {
if self.sub == self.sup {
Some(())
} else {
None
}
}
fn perform(self, type_checker: &mut TypeChecker<'_, 'gcx, 'tcx>) -> InferResult<'tcx, Self::Output> {
type_checker.infcx
.at(&ObligationCause::dummy(), type_checker.param_env)
@ -81,11 +96,17 @@ impl<'tcx> Eq<'tcx> {
impl<'gcx, 'tcx> TypeOp<'gcx, 'tcx> for Eq<'tcx> {
type Output = ();
fn trivial_noop(&self) -> Option<Self::Output> {
if self.a == self.b {
Some(())
} else {
None
}
}
fn perform(self, type_checker: &mut TypeChecker<'_, 'gcx, 'tcx>) -> InferResult<'tcx, Self::Output> {
type_checker.infcx
.at(&ObligationCause::dummy(), type_checker.param_env)
.eq(self.a, self.b)
}
}