Skip to content

Commit 02ad572

Browse files
author
Ariel Ben-Yehuda
committed
avoid calling mk_region unnecessarily
this improves typeck & trans performance by 1%. This looked hotter on callgrind than it is on a CPU.
1 parent 64c6978 commit 02ad572

File tree

27 files changed

+85
-55
lines changed

27 files changed

+85
-55
lines changed

src/librustc/infer/freshen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
9999
ty::ReEmpty |
100100
ty::ReErased => {
101101
// replace all free regions with 'erased
102-
self.tcx().mk_region(ty::ReErased)
102+
self.tcx().types.re_erased
103103
}
104104
}
105105
}

src/librustc/infer/region_inference/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
948948
} else {
949949
// otherwise, we don't know what the free region is,
950950
// so we must conservatively say the LUB is static:
951-
self.tcx.mk_region(ReStatic)
951+
self.tcx.types.re_static
952952
}
953953
}
954954

@@ -971,7 +971,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
971971
if a == b {
972972
a
973973
} else {
974-
self.tcx.mk_region(ReStatic)
974+
self.tcx.types.re_static
975975
}
976976
}
977977
}
@@ -1018,7 +1018,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
10181018

10191019
fn construct_var_data(&self) -> Vec<VarValue<'tcx>> {
10201020
(0..self.num_vars() as usize)
1021-
.map(|_| Value(self.tcx.mk_region(ty::ReEmpty)))
1021+
.map(|_| Value(self.tcx.types.re_empty))
10221022
.collect()
10231023
}
10241024

@@ -1493,7 +1493,7 @@ fn lookup<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
14931493
-> &'tcx ty::Region {
14941494
match values[rid.index as usize] {
14951495
Value(r) => r,
1496-
ErrorValue => tcx.mk_region(ReStatic), // Previously reported error.
1496+
ErrorValue => tcx.types.re_static, // Previously reported error.
14971497
}
14981498
}
14991499

src/librustc/middle/expr_use_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
426426

427427
hir::ExprMatch(ref discr, ref arms, _) => {
428428
let discr_cmt = return_if_err!(self.mc.cat_expr(&discr));
429-
let r = self.tcx().mk_region(ty::ReEmpty);
429+
let r = self.tcx().types.re_empty;
430430
self.borrow_expr(&discr, r, ty::ImmBorrow, MatchDiscriminant);
431431

432432
// treatment of the discriminant is handled while walking the arms.

src/librustc/middle/mem_categorization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,8 +871,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
871871
// we can promote to a constant, otherwise equal to enclosing temp
872872
// lifetime.
873873
let (re, old_re) = if promotable {
874-
(self.tcx().mk_region(ty::ReStatic),
875-
self.tcx().mk_region(ty::ReStatic))
874+
(self.tcx().types.re_static,
875+
self.tcx().types.re_static)
876876
} else {
877877
self.temporary_scope(id)
878878
};

src/librustc/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ fn process_predicate<'a, 'gcx, 'tcx>(
433433
// Otherwise, we have something of the form
434434
// `for<'a> T: 'a where 'a not in T`, which we can treat as `T: 'static`.
435435
Some(t_a) => {
436-
let r_static = selcx.tcx().mk_region(ty::ReStatic);
436+
let r_static = selcx.tcx().types.re_static;
437437
register_region_obligation(t_a, r_static,
438438
obligation.cause.clone(),
439439
region_obligations);

src/librustc/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ pub fn get_vtable_methods<'a, 'tcx>(
628628
// the method may have some early-bound lifetimes, add
629629
// regions for those
630630
let substs = Substs::for_item(tcx, def_id,
631-
|_, _| tcx.mk_region(ty::ReErased),
631+
|_, _| tcx.types.re_erased,
632632
|def, _| trait_ref.substs().type_for_def(def));
633633

634634
// the trait type may have higher-ranked lifetimes in it;

src/librustc/ty/context.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ pub struct CommonTypes<'tcx> {
190190
pub f64: Ty<'tcx>,
191191
pub never: Ty<'tcx>,
192192
pub err: Ty<'tcx>,
193+
194+
pub re_empty: &'tcx Region,
195+
pub re_static: &'tcx Region,
196+
pub re_erased: &'tcx Region,
193197
}
194198

195199
#[derive(RustcEncodable, RustcDecodable)]
@@ -360,6 +364,14 @@ impl<'tcx> TypeckTables<'tcx> {
360364
impl<'tcx> CommonTypes<'tcx> {
361365
fn new(interners: &CtxtInterners<'tcx>) -> CommonTypes<'tcx> {
362366
let mk = |sty| interners.intern_ty(sty, None);
367+
let mk_region = |r| {
368+
if let Some(r) = interners.region.borrow().get(&r) {
369+
return r.0;
370+
}
371+
let r = interners.arena.alloc(r);
372+
interners.region.borrow_mut().insert(Interned(r));
373+
&*r
374+
};
363375
CommonTypes {
364376
bool: mk(TyBool),
365377
char: mk(TyChar),
@@ -379,6 +391,10 @@ impl<'tcx> CommonTypes<'tcx> {
379391
u128: mk(TyUint(ast::UintTy::U128)),
380392
f32: mk(TyFloat(ast::FloatTy::F32)),
381393
f64: mk(TyFloat(ast::FloatTy::F64)),
394+
395+
re_empty: mk_region(Region::ReEmpty),
396+
re_static: mk_region(Region::ReStatic),
397+
re_erased: mk_region(Region::ReErased),
382398
}
383399
}
384400
}
@@ -1233,7 +1249,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12331249
}
12341250

12351251
pub fn mk_static_str(self) -> Ty<'tcx> {
1236-
self.mk_imm_ref(self.mk_region(ty::ReStatic), self.mk_str())
1252+
self.mk_imm_ref(self.types.re_static, self.mk_str())
12371253
}
12381254

12391255
pub fn mk_adt(self, def: &'tcx AdtDef, substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {

src/librustc/ty/fold.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
410410
pub fn erase_late_bound_regions<T>(self, value: &Binder<T>) -> T
411411
where T : TypeFoldable<'tcx>
412412
{
413-
self.replace_late_bound_regions(value, |_| self.mk_region(ty::ReErased)).0
413+
self.replace_late_bound_regions(value, |_| self.types.re_erased).0
414414
}
415415

416416
/// Rewrite any late-bound regions so that they are anonymous. Region numbers are
@@ -538,7 +538,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
538538
// whenever a substitution occurs.
539539
match *r {
540540
ty::ReLateBound(..) => r,
541-
_ => self.tcx().mk_region(ty::ReErased)
541+
_ => self.tcx().types.re_erased
542542
}
543543
}
544544
}
@@ -565,6 +565,22 @@ pub fn shift_region(region: ty::Region, amount: u32) -> ty::Region {
565565
}
566566
}
567567

568+
pub fn shift_region_ref<'a, 'gcx, 'tcx>(
569+
tcx: TyCtxt<'a, 'gcx, 'tcx>,
570+
region: &'tcx ty::Region,
571+
amount: u32)
572+
-> &'tcx ty::Region
573+
{
574+
match region {
575+
&ty::ReLateBound(debruijn, br) if amount > 0 => {
576+
tcx.mk_region(ty::ReLateBound(debruijn.shifted(amount), br))
577+
}
578+
_ => {
579+
region
580+
}
581+
}
582+
}
583+
568584
pub fn shift_regions<'a, 'gcx, 'tcx, T>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
569585
amount: u32, value: &T) -> T
570586
where T: TypeFoldable<'tcx>
@@ -573,7 +589,7 @@ pub fn shift_regions<'a, 'gcx, 'tcx, T>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
573589
value, amount);
574590

575591
value.fold_with(&mut RegionFolder::new(tcx, &mut false, &mut |region, _current_depth| {
576-
tcx.mk_region(shift_region(*region, amount))
592+
shift_region_ref(tcx, region, amount)
577593
}))
578594
}
579595

src/librustc/ty/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,15 +2520,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25202520
/// Construct a parameter environment suitable for static contexts or other contexts where there
25212521
/// are no free type/lifetime parameters in scope.
25222522
pub fn empty_parameter_environment(self) -> ParameterEnvironment<'tcx> {
2523-
2524-
// for an empty parameter environment, there ARE no free
2525-
// regions, so it shouldn't matter what we use for the free id
2526-
let free_id_outlive = self.region_maps.node_extent(ast::DUMMY_NODE_ID);
25272523
ty::ParameterEnvironment {
25282524
free_substs: self.intern_substs(&[]),
25292525
caller_bounds: Vec::new(),
2530-
implicit_region_bound: self.mk_region(ty::ReEmpty),
2531-
free_id_outlive: free_id_outlive,
2526+
implicit_region_bound: self.types.re_empty,
2527+
// for an empty parameter environment, there ARE no free
2528+
// regions, so it shouldn't matter what we use for the free id
2529+
free_id_outlive: ROOT_CODE_EXTENT,
25322530
is_copy_cache: RefCell::new(FxHashMap()),
25332531
is_sized_cache: RefCell::new(FxHashMap()),
25342532
}
@@ -2779,4 +2777,3 @@ pub fn provide_extern(providers: &mut ty::maps::Providers) {
27792777
pub struct CrateInherentImpls {
27802778
pub inherent_impls: DefIdMap<Rc<Vec<DefId>>>,
27812779
}
2782-

src/librustc/ty/subst.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ impl<'a, 'gcx, 'tcx> SubstFolder<'a, 'gcx, 'tcx> {
539539
}
540540

541541
fn shift_region_through_binders(&self, region: &'tcx ty::Region) -> &'tcx ty::Region {
542+
if self.region_binders_passed == 0 || !region.has_escaping_regions() {
543+
return region;
544+
}
542545
self.tcx().mk_region(ty::fold::shift_region(*region, self.region_binders_passed))
543546
}
544547
}

0 commit comments

Comments
 (0)