提交 7c62461c 编写于 作者: N Niko Matsakis

introduce `trivial_noop` to accommodate micro-optimizations

上级 9b4fe442
......@@ -736,6 +736,10 @@ fn fully_perform_op<R>(
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 @@ fn sub_types(
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 @@ fn sub_types(
}
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),
......
......@@ -16,6 +16,9 @@
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 @@ impl<'gcx, 'tcx, F, R> TypeOp<'gcx, 'tcx> for CustomTypeOp<F>
{
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 @@ pub(super) fn new(sub: Ty<'tcx>, sup: Ty<'tcx>) -> Self {
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 @@ pub(super) fn new(a: Ty<'tcx>, b: Ty<'tcx>) -> Self {
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)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册