Skip to content

Pre-intern some const infer vars #141499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/type_check/relate_tys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_middle/src/ty/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Const<'tcx>>,
}

impl<'tcx> CommonTypes<'tcx> {
Expand Down Expand Up @@ -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,
Expand All @@ -1210,6 +1218,7 @@ impl<'tcx> CommonConsts<'tcx> {
ty: types.bool,
valtree: valtree_false,
})),
ct_vars,
valtree_zst,
}
}
Expand Down
Loading