Skip to content

Commit 9e91e50

Browse files
committed
Auto merge of #136593 - lukas-code:ty-value-perf, r=<try>
[perf] try to mitigate regression in fast reject for `ty::Value` #136318 (comment) r? ghost
2 parents 942db67 + bc51bb2 commit 9e91e50

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

compiler/rustc_middle/src/ty/consts.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ impl<'tcx> rustc_type_ir::inherent::IntoKind for Const<'tcx> {
3434
}
3535
}
3636

37+
impl<'tcx> rustc_type_ir::inherent::IntoKindRef for Const<'tcx> {
38+
fn kind_ref(&self) -> &ConstKind<'tcx> {
39+
(*self).kind_ref()
40+
}
41+
}
42+
3743
impl<'tcx> rustc_type_ir::visit::Flags for Const<'tcx> {
3844
fn flags(&self) -> TypeFlags {
3945
self.0.flags
@@ -47,8 +53,12 @@ impl<'tcx> rustc_type_ir::visit::Flags for Const<'tcx> {
4753
impl<'tcx> Const<'tcx> {
4854
#[inline]
4955
pub fn kind(self) -> ConstKind<'tcx> {
50-
let a: &ConstKind<'tcx> = self.0.0;
51-
*a
56+
*self.kind_ref()
57+
}
58+
59+
#[inline]
60+
pub fn kind_ref(self) -> &'tcx ConstKind<'tcx> {
61+
self.0.0
5262
}
5363

5464
// FIXME(compiler-errors): Think about removing this.

compiler/rustc_middle/src/ty/consts/valtree.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ impl<'tcx> Value<'tcx> {
145145
}
146146

147147
impl<'tcx> rustc_type_ir::inherent::ValueConst<TyCtxt<'tcx>> for Value<'tcx> {
148-
fn ty(self) -> Ty<'tcx> {
148+
fn ty(&self) -> Ty<'tcx> {
149149
self.ty
150150
}
151151

152-
fn valtree(self) -> ValTree<'tcx> {
153-
self.valtree
152+
fn valtree(&self) -> &ValTree<'tcx> {
153+
&self.valtree
154154
}
155155
}

compiler/rustc_type_ir/src/fast_reject.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
464464
// Unlike `types_may_unify_inner`, this does not take a depth as
465465
// we never recurse from this function.
466466
fn consts_may_unify_inner(self, lhs: I::Const, rhs: I::Const) -> bool {
467-
match rhs.kind() {
467+
let rhs_kind = rhs.kind_ref();
468+
match *rhs_kind {
468469
ty::ConstKind::Param(_) => {
469470
if INSTANTIATE_RHS_WITH_INFER {
470471
return true;
@@ -482,23 +483,24 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
482483
ty::ConstKind::Value(..) | ty::ConstKind::Placeholder(_) => {}
483484
};
484485

485-
match lhs.kind() {
486-
ty::ConstKind::Value(lhs_val) => match rhs.kind() {
487-
ty::ConstKind::Value(rhs_val) => lhs_val.valtree() == rhs_val.valtree(),
486+
let lhs_kind = lhs.kind_ref();
487+
match *lhs_kind {
488+
ty::ConstKind::Value(ref lhs_val) => match *rhs_kind {
489+
ty::ConstKind::Value(ref rhs_val) => lhs_val.valtree() == rhs_val.valtree(),
488490
_ => false,
489491
},
490492

491493
ty::ConstKind::Param(lhs) => {
492494
INSTANTIATE_LHS_WITH_INFER
493-
|| match rhs.kind() {
495+
|| match *rhs_kind {
494496
ty::ConstKind::Param(rhs) => lhs == rhs,
495497
_ => false,
496498
}
497499
}
498500

499501
// Placeholder consts don't unify with anything on their own
500502
ty::ConstKind::Placeholder(lhs) => {
501-
matches!(rhs.kind(), ty::ConstKind::Placeholder(rhs) if lhs == rhs)
503+
matches!(*rhs_kind, ty::ConstKind::Placeholder(rhs) if lhs == rhs)
502504
}
503505

504506
// As we don't necessarily eagerly evaluate constants,

compiler/rustc_type_ir/src/inherent.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ pub trait Const<I: Interner<Const = Self>>:
257257
+ Eq
258258
+ Into<I::GenericArg>
259259
+ Into<I::Term>
260-
+ IntoKind<Kind = ty::ConstKind<I>>
260+
+ IntoKindRef<Kind = ty::ConstKind<I>>
261261
+ TypeSuperVisitable<I>
262262
+ TypeSuperFoldable<I>
263263
+ Relate<I>
@@ -287,8 +287,8 @@ pub trait Const<I: Interner<Const = Self>>:
287287
}
288288

289289
pub trait ValueConst<I: Interner<ValueConst = Self>>: Copy + Debug + Hash + Eq {
290-
fn ty(self) -> I::Ty;
291-
fn valtree(self) -> I::ValTree;
290+
fn ty(&self) -> I::Ty;
291+
fn valtree(&self) -> &I::ValTree;
292292
}
293293

294294
pub trait ExprConst<I: Interner<ExprConst = Self>>: Copy + Debug + Hash + Eq + Relate<I> {
@@ -518,6 +518,10 @@ pub trait IntoKind {
518518
fn kind(self) -> Self::Kind;
519519
}
520520

521+
pub trait IntoKindRef: IntoKind {
522+
fn kind_ref(&self) -> &Self::Kind;
523+
}
524+
521525
pub trait BoundVarLike<I: Interner> {
522526
fn var(self) -> ty::BoundVar;
523527

0 commit comments

Comments
 (0)