Skip to content

Commit 06fbedc

Browse files
Auto merge of #144434 - nnethercote:preintern-ty-bounds, r=<try>
Preintern some `TyKind::Bound` values
2 parents b56aaec + 837b610 commit 06fbedc

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,11 +1041,13 @@ const NUM_PREINTERNED_TY_VARS: u32 = 100;
10411041
const NUM_PREINTERNED_FRESH_TYS: u32 = 20;
10421042
const NUM_PREINTERNED_FRESH_INT_TYS: u32 = 3;
10431043
const NUM_PREINTERNED_FRESH_FLOAT_TYS: u32 = 3;
1044+
const NUM_PREINTERNED_ANON_BOUND_TYS_I: u32 = 3;
1045+
const NUM_PREINTERNED_ANON_BOUND_TYS_V: u32 = 20;
10441046

10451047
// This number may seem high, but it is reached in all but the smallest crates.
10461048
const NUM_PREINTERNED_RE_VARS: u32 = 500;
1047-
const NUM_PREINTERNED_RE_LATE_BOUNDS_I: u32 = 2;
1048-
const NUM_PREINTERNED_RE_LATE_BOUNDS_V: u32 = 20;
1049+
const NUM_PREINTERNED_ANON_RE_BOUNDS_I: u32 = 3;
1050+
const NUM_PREINTERNED_ANON_RE_BOUNDS_V: u32 = 20;
10491051

10501052
pub struct CommonTypes<'tcx> {
10511053
pub unit: Ty<'tcx>,
@@ -1088,6 +1090,11 @@ pub struct CommonTypes<'tcx> {
10881090

10891091
/// Pre-interned `Infer(ty::FreshFloatTy(n))` for small values of `n`.
10901092
pub fresh_float_tys: Vec<Ty<'tcx>>,
1093+
1094+
/// Pre-interned values of the form:
1095+
/// `Bound(DebruijnIndex(i), BoundTy { var: v, kind: BoundTyKind::Anon})`
1096+
/// for small values of `i` and `v`.
1097+
pub anon_bound_tys: Vec<Vec<Ty<'tcx>>>,
10911098
}
10921099

10931100
pub struct CommonLifetimes<'tcx> {
@@ -1101,9 +1108,9 @@ pub struct CommonLifetimes<'tcx> {
11011108
pub re_vars: Vec<Region<'tcx>>,
11021109

11031110
/// Pre-interned values of the form:
1104-
/// `ReBound(DebruijnIndex(i), BoundRegion { var: v, kind: BrAnon })`
1111+
/// `ReBound(DebruijnIndex(i), BoundRegion { var: v, kind: BoundRegionKind::Anon })`
11051112
/// for small values of `i` and `v`.
1106-
pub re_late_bounds: Vec<Vec<Region<'tcx>>>,
1113+
pub anon_re_bounds: Vec<Vec<Region<'tcx>>>,
11071114
}
11081115

11091116
pub struct CommonConsts<'tcx> {
@@ -1131,6 +1138,19 @@ impl<'tcx> CommonTypes<'tcx> {
11311138
let fresh_float_tys: Vec<_> =
11321139
(0..NUM_PREINTERNED_FRESH_FLOAT_TYS).map(|n| mk(Infer(ty::FreshFloatTy(n)))).collect();
11331140

1141+
let anon_bound_tys = (0..NUM_PREINTERNED_ANON_BOUND_TYS_I)
1142+
.map(|i| {
1143+
(0..NUM_PREINTERNED_ANON_BOUND_TYS_V)
1144+
.map(|v| {
1145+
mk(ty::Bound(
1146+
ty::DebruijnIndex::from(i),
1147+
ty::BoundTy { var: ty::BoundVar::from(v), kind: ty::BoundTyKind::Anon },
1148+
))
1149+
})
1150+
.collect()
1151+
})
1152+
.collect();
1153+
11341154
CommonTypes {
11351155
unit: mk(Tuple(List::empty())),
11361156
bool: mk(Bool),
@@ -1161,6 +1181,7 @@ impl<'tcx> CommonTypes<'tcx> {
11611181
fresh_tys,
11621182
fresh_int_tys,
11631183
fresh_float_tys,
1184+
anon_bound_tys,
11641185
}
11651186
}
11661187
}
@@ -1176,9 +1197,9 @@ impl<'tcx> CommonLifetimes<'tcx> {
11761197
let re_vars =
11771198
(0..NUM_PREINTERNED_RE_VARS).map(|n| mk(ty::ReVar(ty::RegionVid::from(n)))).collect();
11781199

1179-
let re_late_bounds = (0..NUM_PREINTERNED_RE_LATE_BOUNDS_I)
1200+
let anon_re_bounds = (0..NUM_PREINTERNED_ANON_RE_BOUNDS_I)
11801201
.map(|i| {
1181-
(0..NUM_PREINTERNED_RE_LATE_BOUNDS_V)
1202+
(0..NUM_PREINTERNED_ANON_RE_BOUNDS_V)
11821203
.map(|v| {
11831204
mk(ty::ReBound(
11841205
ty::DebruijnIndex::from(i),
@@ -1196,7 +1217,7 @@ impl<'tcx> CommonLifetimes<'tcx> {
11961217
re_static: mk(ty::ReStatic),
11971218
re_erased: mk(ty::ReErased),
11981219
re_vars,
1199-
re_late_bounds,
1220+
anon_re_bounds,
12001221
}
12011222
}
12021223
}

compiler/rustc_middle/src/ty/region.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'tcx> Region<'tcx> {
5454
) -> Region<'tcx> {
5555
// Use a pre-interned one when possible.
5656
if let ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon } = bound_region
57-
&& let Some(inner) = tcx.lifetimes.re_late_bounds.get(debruijn.as_usize())
57+
&& let Some(inner) = tcx.lifetimes.anon_re_bounds.get(debruijn.as_usize())
5858
&& let Some(re) = inner.get(var.as_usize()).copied()
5959
{
6060
re

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,15 @@ impl<'tcx> Ty<'tcx> {
485485
index: ty::DebruijnIndex,
486486
bound_ty: ty::BoundTy,
487487
) -> Ty<'tcx> {
488-
Ty::new(tcx, Bound(index, bound_ty))
488+
// Use a pre-interned one when possible.
489+
if let ty::BoundTy { var, kind: ty::BoundTyKind::Anon } = bound_ty
490+
&& let Some(inner) = tcx.types.anon_bound_tys.get(index.as_usize())
491+
&& let Some(ty) = inner.get(var.as_usize()).copied()
492+
{
493+
ty
494+
} else {
495+
Ty::new(tcx, Bound(index, bound_ty))
496+
}
489497
}
490498

491499
#[inline]

0 commit comments

Comments
 (0)