Skip to content

Commit 2e3b079

Browse files
committed
Shrink ObligationCauseCode by boxing IfExpression.
The reduction in `memcpy` calls outweighs the cost of the extra allocations, for a net performance win.
1 parent b972ac8 commit 2e3b079

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ use crate::hir::def_id::DefId;
5555
use crate::hir::Node;
5656
use crate::infer::opaque_types;
5757
use crate::middle::region;
58-
use crate::traits::{MatchExpressionArmCause, ObligationCause, ObligationCauseCode};
58+
use crate::traits::{IfExpressionCause, MatchExpressionArmCause, ObligationCause};
59+
use crate::traits::{ObligationCauseCode};
5960
use crate::ty::error::TypeError;
6061
use crate::ty::{self, subst::{Subst, SubstsRef}, Region, Ty, TyCtxt, TypeFoldable};
6162
use errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
@@ -681,7 +682,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
681682
}
682683
}
683684
},
684-
ObligationCauseCode::IfExpression { then, outer, semicolon } => {
685+
ObligationCauseCode::IfExpression(box IfExpressionCause { then, outer, semicolon }) => {
685686
err.span_label(then, "expected because of this");
686687
outer.map(|sp| err.span_label(sp, "if and else have incompatible types"));
687688
if let Some(sp) = semicolon {

src/librustc/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub struct PendingPredicateObligation<'tcx> {
7070

7171
// `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
7272
#[cfg(target_arch = "x86_64")]
73-
static_assert_size!(PendingPredicateObligation<'_>, 144);
73+
static_assert_size!(PendingPredicateObligation<'_>, 136);
7474

7575
impl<'a, 'tcx> FulfillmentContext<'tcx> {
7676
/// Creates a new fulfillment context.

src/librustc/traits/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub type TraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;
125125

126126
// `PredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
127127
#[cfg(target_arch = "x86_64")]
128-
static_assert_size!(PredicateObligation<'_>, 120);
128+
static_assert_size!(PredicateObligation<'_>, 112);
129129

130130
/// The reason why we incurred this obligation; used for error reporting.
131131
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
@@ -234,11 +234,7 @@ pub enum ObligationCauseCode<'tcx> {
234234
MatchExpressionArmPattern { span: Span, ty: Ty<'tcx> },
235235

236236
/// Computing common supertype in an if expression
237-
IfExpression {
238-
then: Span,
239-
outer: Option<Span>,
240-
semicolon: Option<Span>,
241-
},
237+
IfExpression(Box<IfExpressionCause>),
242238

243239
/// Computing common supertype of an if expression with no else counter-part
244240
IfExpressionWithNoElse,
@@ -270,7 +266,7 @@ pub enum ObligationCauseCode<'tcx> {
270266

271267
// `ObligationCauseCode` is used a lot. Make sure it doesn't unintentionally get bigger.
272268
#[cfg(target_arch = "x86_64")]
273-
static_assert_size!(ObligationCauseCode<'_>, 40);
269+
static_assert_size!(ObligationCauseCode<'_>, 32);
274270

275271
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
276272
pub struct MatchExpressionArmCause<'tcx> {
@@ -281,6 +277,13 @@ pub struct MatchExpressionArmCause<'tcx> {
281277
pub discrim_hir_id: hir::HirId,
282278
}
283279

280+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
281+
pub struct IfExpressionCause {
282+
pub then: Span,
283+
pub outer: Option<Span>,
284+
pub semicolon: Option<Span>,
285+
}
286+
284287
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
285288
pub struct DerivedObligationCause<'tcx> {
286289
/// The trait reference of the parent obligation that led to the

src/librustc/traits/structural_impls.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,13 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
528528
super::MatchExpressionArmPattern { span, ty } => {
529529
tcx.lift(&ty).map(|ty| super::MatchExpressionArmPattern { span, ty })
530530
}
531-
super::IfExpression { then, outer, semicolon } => Some(super::IfExpression {
532-
then,
533-
outer,
534-
semicolon,
535-
}),
531+
super::IfExpression(box super::IfExpressionCause { then, outer, semicolon }) => {
532+
Some(super::IfExpression(box super::IfExpressionCause {
533+
then,
534+
outer,
535+
semicolon,
536+
}))
537+
}
536538
super::IfExpressionWithNoElse => Some(super::IfExpressionWithNoElse),
537539
super::MainFunctionType => Some(super::MainFunctionType),
538540
super::StartFunctionType => Some(super::StartFunctionType),

src/librustc_typeck/check/_match.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use crate::check::{FnCtxt, Expectation, Diverges, Needs};
22
use crate::check::coercion::CoerceMany;
33
use rustc::hir::{self, ExprKind};
44
use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
5-
use rustc::traits::{MatchExpressionArmCause, ObligationCause, ObligationCauseCode};
5+
use rustc::traits::{IfExpressionCause, MatchExpressionArmCause, ObligationCause};
6+
use rustc::traits::{ObligationCauseCode};
67
use rustc::ty::Ty;
78
use syntax_pos::Span;
89

@@ -347,11 +348,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
347348
};
348349

349350
// Finally construct the cause:
350-
self.cause(error_sp, ObligationCauseCode::IfExpression {
351+
self.cause(error_sp, ObligationCauseCode::IfExpression(box IfExpressionCause {
351352
then: then_sp,
352353
outer: outer_sp,
353354
semicolon: remove_semicolon,
354-
})
355+
}))
355356
}
356357

357358
fn demand_discriminant_type(

0 commit comments

Comments
 (0)