From f1ff0415506e6b0e8798464dc93958cb8f43041a Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 24 May 2025 12:19:44 +0000 Subject: [PATCH 1/2] Use Ty::new_var quick path --- compiler/rustc_borrowck/src/type_check/relate_tys.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_borrowck/src/type_check/relate_tys.rs b/compiler/rustc_borrowck/src/type_check/relate_tys.rs index 02a41469c97f4..583bd49ab4a59 100644 --- a/compiler/rustc_borrowck/src/type_check/relate_tys.rs +++ b/compiler/rustc_borrowck/src/type_check/relate_tys.rs @@ -139,7 +139,7 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> { variance, ty, )?; - Ok(infcx.resolve_vars_if_possible(Ty::new_infer(infcx.tcx, ty::TyVar(ty_vid)))) + Ok(infcx.resolve_vars_if_possible(Ty::new_var(infcx.tcx, ty_vid))) }; let (a, b) = match (a.kind(), b.kind()) { From 577147170e229b2c79e142146de7b1122c33ca6f Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 24 May 2025 12:35:37 +0000 Subject: [PATCH 2/2] Fast path pre-intern const infer vars --- compiler/rustc_middle/src/ty/consts.rs | 9 +++++++-- compiler/rustc_middle/src/ty/context.rs | 9 +++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 455ac66041261..8f0c4506d55e3 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -75,8 +75,13 @@ impl<'tcx> Const<'tcx> { } #[inline] - pub fn new_var(tcx: TyCtxt<'tcx>, infer: ty::ConstVid) -> Const<'tcx> { - Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Var(infer))) + pub fn new_var(tcx: TyCtxt<'tcx>, v: ty::ConstVid) -> Const<'tcx> { + // Use a pre-interned one when possible. + tcx.consts + .ct_vars + .get(v.as_usize()) + .copied() + .unwrap_or_else(|| Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Var(v)))) } #[inline] diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index c205d53b93f1f..37b1fd478e694 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1014,6 +1014,8 @@ const NUM_PREINTERNED_FRESH_TYS: u32 = 20; const NUM_PREINTERNED_FRESH_INT_TYS: u32 = 3; const NUM_PREINTERNED_FRESH_FLOAT_TYS: u32 = 3; +const NUM_PREINTERNED_CT_VARS: u32 = 100; + // This number may seem high, but it is reached in all but the smallest crates. const NUM_PREINTERNED_RE_VARS: u32 = 500; const NUM_PREINTERNED_RE_LATE_BOUNDS_I: u32 = 2; @@ -1084,6 +1086,8 @@ pub struct CommonConsts<'tcx> { pub false_: Const<'tcx>, /// Use [`ty::ValTree::zst`] instead. pub(crate) valtree_zst: ValTree<'tcx>, + /// Pre-interned `ConstKind::Infer(InferConst::Var(n))` for small values of `n`. + pub ct_vars: Vec>, } impl<'tcx> CommonTypes<'tcx> { @@ -1197,6 +1201,10 @@ impl<'tcx> CommonConsts<'tcx> { let valtree_true = mk_valtree(ty::ValTreeKind::Leaf(ty::ScalarInt::TRUE)); let valtree_false = mk_valtree(ty::ValTreeKind::Leaf(ty::ScalarInt::FALSE)); + let ct_vars = (0..NUM_PREINTERNED_CT_VARS) + .map(|n| mk_const(ty::ConstKind::Infer(ty::InferConst::Var(ty::ConstVid::from(n))))) + .collect(); + CommonConsts { unit: mk_const(ty::ConstKind::Value(ty::Value { ty: types.unit, @@ -1210,6 +1218,7 @@ impl<'tcx> CommonConsts<'tcx> { ty: types.bool, valtree: valtree_false, })), + ct_vars, valtree_zst, } }