Skip to content

Commit 6248bbb

Browse files
committed
Pre-intern some commonly used type variables.
This requires some rearrangement of plumbing, such as adding `mk_fresh_{,int_,float_}ty` and removing `mk_ty_infer`.
1 parent 5b8f284 commit 6248bbb

File tree

6 files changed

+79
-30
lines changed

6 files changed

+79
-30
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
273273
ct_op: |c| c,
274274
ty_op: |t| match *t.kind() {
275275
ty::Infer(ty::TyVar(_)) => self.tcx.mk_ty_var(ty::TyVid::from_u32(0)),
276-
ty::Infer(ty::IntVar(_)) => {
277-
self.tcx.mk_ty_infer(ty::IntVar(ty::IntVid { index: 0 }))
278-
}
279-
ty::Infer(ty::FloatVar(_)) => {
280-
self.tcx.mk_ty_infer(ty::FloatVar(ty::FloatVid { index: 0 }))
281-
}
276+
ty::Infer(ty::IntVar(_)) => self.tcx.mk_int_var(ty::IntVid { index: 0 }),
277+
ty::Infer(ty::FloatVar(_)) => self.tcx.mk_float_var(ty::FloatVid { index: 0 }),
282278
_ => t,
283279
},
284280
};

compiler/rustc_infer/src/infer/freshen.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,9 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
5858
}
5959
}
6060

61-
fn freshen_ty<F>(
62-
&mut self,
63-
opt_ty: Option<Ty<'tcx>>,
64-
key: ty::InferTy,
65-
freshener: F,
66-
) -> Ty<'tcx>
61+
fn freshen_ty<F>(&mut self, opt_ty: Option<Ty<'tcx>>, key: ty::InferTy, mk_fresh: F) -> Ty<'tcx>
6762
where
68-
F: FnOnce(u32) -> ty::InferTy,
63+
F: FnOnce(u32) -> Ty<'tcx>,
6964
{
7065
if let Some(ty) = opt_ty {
7166
return ty.fold_with(self);
@@ -76,7 +71,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
7671
Entry::Vacant(entry) => {
7772
let index = self.ty_freshen_count;
7873
self.ty_freshen_count += 1;
79-
let t = self.infcx.tcx.mk_ty_infer(freshener(index));
74+
let t = mk_fresh(index);
8075
entry.insert(t);
8176
t
8277
}
@@ -204,7 +199,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
204199
match v {
205200
ty::TyVar(v) => {
206201
let opt_ty = self.infcx.inner.borrow_mut().type_variables().probe(v).known();
207-
Some(self.freshen_ty(opt_ty, ty::TyVar(v), ty::FreshTy))
202+
Some(self.freshen_ty(opt_ty, ty::TyVar(v), |n| self.infcx.tcx.mk_fresh_ty(n)))
208203
}
209204

210205
ty::IntVar(v) => Some(
@@ -216,7 +211,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
216211
.probe_value(v)
217212
.map(|v| v.to_type(self.infcx.tcx)),
218213
ty::IntVar(v),
219-
ty::FreshIntTy,
214+
|n| self.infcx.tcx.mk_fresh_int_ty(n),
220215
),
221216
),
222217

@@ -229,7 +224,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
229224
.probe_value(v)
230225
.map(|v| v.to_type(self.infcx.tcx)),
231226
ty::FloatVar(v),
232-
ty::FreshFloatTy,
227+
|n| self.infcx.tcx.mk_fresh_float_ty(n),
233228
),
234229
),
235230

compiler/rustc_middle/src/ty/context.rs

+67-9
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ impl<'tcx> CtxtInterners<'tcx> {
241241
}
242242
}
243243

244+
const NUM_PREINTERNED_TY_VARS: u32 = 100;
245+
const NUM_PREINTERNED_FRESH_TYS: u32 = 20;
246+
const NUM_PREINTERNED_FRESH_INT_TYS: u32 = 3;
247+
const NUM_PREINTERNED_FRESH_FLOAT_TYS: u32 = 3;
248+
244249
pub struct CommonTypes<'tcx> {
245250
pub unit: Ty<'tcx>,
246251
pub bool: Ty<'tcx>,
@@ -266,7 +271,20 @@ pub struct CommonTypes<'tcx> {
266271
/// Dummy type used for the `Self` of a `TraitRef` created for converting
267272
/// a trait object, and which gets removed in `ExistentialTraitRef`.
268273
/// This type must not appear anywhere in other converted types.
274+
/// `Infer(ty::FreshTy(0))` does the job.
269275
pub trait_object_dummy_self: Ty<'tcx>,
276+
277+
/// Pre-interned `Infer(ty::TyVar(n))` for small values of `n`.
278+
pub ty_vars: Vec<Ty<'tcx>>,
279+
280+
/// Pre-interned `Infer(ty::FreshTy(n))` for small values of `n`.
281+
pub fresh_tys: Vec<Ty<'tcx>>,
282+
283+
/// Pre-interned `Infer(ty::FreshIntTy(n))` for small values of `n`.
284+
pub fresh_int_tys: Vec<Ty<'tcx>>,
285+
286+
/// Pre-interned `Infer(ty::FreshFloatTy(n))` for small values of `n`.
287+
pub fresh_float_tys: Vec<Ty<'tcx>>,
270288
}
271289

272290
pub struct CommonLifetimes<'tcx> {
@@ -289,6 +307,15 @@ impl<'tcx> CommonTypes<'tcx> {
289307
) -> CommonTypes<'tcx> {
290308
let mk = |ty| interners.intern_ty(ty, sess, untracked);
291309

310+
let ty_vars =
311+
(0..NUM_PREINTERNED_TY_VARS).map(|n| mk(Infer(ty::TyVar(TyVid::from(n))))).collect();
312+
let fresh_tys: Vec<_> =
313+
(0..NUM_PREINTERNED_FRESH_TYS).map(|n| mk(Infer(ty::FreshTy(n)))).collect();
314+
let fresh_int_tys: Vec<_> =
315+
(0..NUM_PREINTERNED_FRESH_INT_TYS).map(|n| mk(Infer(ty::FreshIntTy(n)))).collect();
316+
let fresh_float_tys: Vec<_> =
317+
(0..NUM_PREINTERNED_FRESH_FLOAT_TYS).map(|n| mk(Infer(ty::FreshFloatTy(n)))).collect();
318+
292319
CommonTypes {
293320
unit: mk(Tuple(List::empty())),
294321
bool: mk(Bool),
@@ -311,7 +338,12 @@ impl<'tcx> CommonTypes<'tcx> {
311338
str_: mk(Str),
312339
self_param: mk(ty::Param(ty::ParamTy { index: 0, name: kw::SelfUpper })),
313340

314-
trait_object_dummy_self: mk(Infer(ty::FreshTy(0))),
341+
trait_object_dummy_self: fresh_tys[0],
342+
343+
ty_vars,
344+
fresh_tys,
345+
fresh_int_tys,
346+
fresh_float_tys,
315347
}
316348
}
317349
}
@@ -1868,28 +1900,54 @@ impl<'tcx> TyCtxt<'tcx> {
18681900
}
18691901

18701902
#[inline]
1871-
pub fn mk_ty_var(self, v: TyVid) -> Ty<'tcx> {
1872-
self.mk_ty_infer(TyVar(v))
1903+
pub fn mk_const(self, kind: impl Into<ty::ConstKind<'tcx>>, ty: Ty<'tcx>) -> Const<'tcx> {
1904+
self.mk_const_internal(ty::ConstData { kind: kind.into(), ty })
18731905
}
18741906

18751907
#[inline]
1876-
pub fn mk_const(self, kind: impl Into<ty::ConstKind<'tcx>>, ty: Ty<'tcx>) -> Const<'tcx> {
1877-
self.mk_const_internal(ty::ConstData { kind: kind.into(), ty })
1908+
pub fn mk_ty_var(self, v: TyVid) -> Ty<'tcx> {
1909+
// Use a pre-interned one when possible.
1910+
self.types.ty_vars.get(v.as_usize()).copied().unwrap_or_else(|| self.mk_ty(Infer(TyVar(v))))
18781911
}
18791912

18801913
#[inline]
18811914
pub fn mk_int_var(self, v: IntVid) -> Ty<'tcx> {
1882-
self.mk_ty_infer(IntVar(v))
1915+
self.mk_ty(Infer(IntVar(v)))
18831916
}
18841917

18851918
#[inline]
18861919
pub fn mk_float_var(self, v: FloatVid) -> Ty<'tcx> {
1887-
self.mk_ty_infer(FloatVar(v))
1920+
self.mk_ty(Infer(FloatVar(v)))
1921+
}
1922+
1923+
#[inline]
1924+
pub fn mk_fresh_ty(self, n: u32) -> Ty<'tcx> {
1925+
// Use a pre-interned one when possible.
1926+
self.types
1927+
.fresh_tys
1928+
.get(n as usize)
1929+
.copied()
1930+
.unwrap_or_else(|| self.mk_ty(Infer(ty::FreshTy(n))))
1931+
}
1932+
1933+
#[inline]
1934+
pub fn mk_fresh_int_ty(self, n: u32) -> Ty<'tcx> {
1935+
// Use a pre-interned one when possible.
1936+
self.types
1937+
.fresh_int_tys
1938+
.get(n as usize)
1939+
.copied()
1940+
.unwrap_or_else(|| self.mk_ty(Infer(ty::FreshIntTy(n))))
18881941
}
18891942

18901943
#[inline]
1891-
pub fn mk_ty_infer(self, it: InferTy) -> Ty<'tcx> {
1892-
self.mk_ty(Infer(it))
1944+
pub fn mk_fresh_float_ty(self, n: u32) -> Ty<'tcx> {
1945+
// Use a pre-interned one when possible.
1946+
self.types
1947+
.fresh_float_tys
1948+
.get(n as usize)
1949+
.copied()
1950+
.unwrap_or_else(|| self.mk_ty(Infer(ty::FreshFloatTy(n))))
18931951
}
18941952

18951953
#[inline]

compiler/rustc_middle/src/ty/print/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ pub trait PrettyPrinter<'tcx>:
12091209
// in order to place the projections inside the `<...>`.
12101210
if !resugared {
12111211
// Use a type that can't appear in defaults of type parameters.
1212-
let dummy_cx = cx.tcx().mk_ty_infer(ty::FreshTy(0));
1212+
let dummy_cx = cx.tcx().mk_fresh_ty(0);
12131213
let principal = principal.with_self_ty(cx.tcx(), dummy_cx);
12141214

12151215
let args = cx
@@ -2696,7 +2696,7 @@ define_print_and_forward_display! {
26962696

26972697
ty::ExistentialTraitRef<'tcx> {
26982698
// Use a type that can't appear in defaults of type parameters.
2699-
let dummy_self = cx.tcx().mk_ty_infer(ty::FreshTy(0));
2699+
let dummy_self = cx.tcx().mk_fresh_ty(0);
27002700
let trait_ref = self.with_self_ty(cx.tcx(), dummy_self);
27012701
p!(print(trait_ref.print_only_trait_path()))
27022702
}

compiler/rustc_symbol_mangling/src/v0.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
540540
match predicate.as_ref().skip_binder() {
541541
ty::ExistentialPredicate::Trait(trait_ref) => {
542542
// Use a type that can't appear in defaults of type parameters.
543-
let dummy_self = cx.tcx.mk_ty_infer(ty::FreshTy(0));
543+
let dummy_self = cx.tcx.mk_fresh_ty(0);
544544
let trait_ref = trait_ref.with_self_ty(cx.tcx, dummy_self);
545545
cx = cx.print_def_path(trait_ref.def_id, trait_ref.substs)?;
546546
}

compiler/rustc_trait_selection/src/traits/wf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ pub fn object_region_bounds<'tcx>(
879879
// Since we don't actually *know* the self type for an object,
880880
// this "open(err)" serves as a kind of dummy standin -- basically
881881
// a placeholder type.
882-
let open_ty = tcx.mk_ty_infer(ty::FreshTy(0));
882+
let open_ty = tcx.mk_fresh_ty(0);
883883

884884
let predicates = existential_predicates.iter().filter_map(|predicate| {
885885
if let ty::ExistentialPredicate::Projection(_) = predicate.skip_binder() {

0 commit comments

Comments
 (0)