avoid calling `mk_region` unnecessarily

this improves typeck & trans performance by 1%. This looked hotter on
callgrind than it is on a CPU.
This commit is contained in:
Ariel Ben-Yehuda 2017-04-20 01:58:12 +03:00 committed by Ariel Ben-Yehuda
parent 3bf00450cb
commit e1377a4f47
27 changed files with 85 additions and 55 deletions

View File

@ -99,7 +99,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
ty::ReEmpty |
ty::ReErased => {
// replace all free regions with 'erased
self.tcx().mk_region(ty::ReErased)
self.tcx().types.re_erased
}
}
}

View File

@ -948,7 +948,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
} else {
// otherwise, we don't know what the free region is,
// so we must conservatively say the LUB is static:
self.tcx.mk_region(ReStatic)
self.tcx.types.re_static
}
}
@ -971,7 +971,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
if a == b {
a
} else {
self.tcx.mk_region(ReStatic)
self.tcx.types.re_static
}
}
}
@ -1018,7 +1018,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
fn construct_var_data(&self) -> Vec<VarValue<'tcx>> {
(0..self.num_vars() as usize)
.map(|_| Value(self.tcx.mk_region(ty::ReEmpty)))
.map(|_| Value(self.tcx.types.re_empty))
.collect()
}
@ -1493,7 +1493,7 @@ fn lookup<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
-> &'tcx ty::Region {
match values[rid.index as usize] {
Value(r) => r,
ErrorValue => tcx.mk_region(ReStatic), // Previously reported error.
ErrorValue => tcx.types.re_static, // Previously reported error.
}
}

View File

@ -426,7 +426,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
hir::ExprMatch(ref discr, ref arms, _) => {
let discr_cmt = return_if_err!(self.mc.cat_expr(&discr));
let r = self.tcx().mk_region(ty::ReEmpty);
let r = self.tcx().types.re_empty;
self.borrow_expr(&discr, r, ty::ImmBorrow, MatchDiscriminant);
// treatment of the discriminant is handled while walking the arms.

View File

@ -871,8 +871,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
// we can promote to a constant, otherwise equal to enclosing temp
// lifetime.
let (re, old_re) = if promotable {
(self.tcx().mk_region(ty::ReStatic),
self.tcx().mk_region(ty::ReStatic))
(self.tcx().types.re_static,
self.tcx().types.re_static)
} else {
self.temporary_scope(id)
};

View File

@ -443,7 +443,7 @@ fn process_predicate<'a, 'gcx, 'tcx>(
// Otherwise, we have something of the form
// `for<'a> T: 'a where 'a not in T`, which we can treat as `T: 'static`.
Some(t_a) => {
let r_static = selcx.tcx().mk_region(ty::ReStatic);
let r_static = selcx.tcx().types.re_static;
register_region_obligation(t_a, r_static,
obligation.cause.clone(),
region_obligations);

View File

@ -629,7 +629,7 @@ pub fn get_vtable_methods<'a, 'tcx>(
// the method may have some early-bound lifetimes, add
// regions for those
let substs = Substs::for_item(tcx, def_id,
|_, _| tcx.mk_region(ty::ReErased),
|_, _| tcx.types.re_erased,
|def, _| trait_ref.substs().type_for_def(def));
// the trait type may have higher-ranked lifetimes in it;

View File

@ -190,6 +190,10 @@ pub struct CommonTypes<'tcx> {
pub f64: Ty<'tcx>,
pub never: Ty<'tcx>,
pub err: Ty<'tcx>,
pub re_empty: &'tcx Region,
pub re_static: &'tcx Region,
pub re_erased: &'tcx Region,
}
#[derive(RustcEncodable, RustcDecodable)]
@ -360,6 +364,14 @@ impl<'tcx> TypeckTables<'tcx> {
impl<'tcx> CommonTypes<'tcx> {
fn new(interners: &CtxtInterners<'tcx>) -> CommonTypes<'tcx> {
let mk = |sty| interners.intern_ty(sty, None);
let mk_region = |r| {
if let Some(r) = interners.region.borrow().get(&r) {
return r.0;
}
let r = interners.arena.alloc(r);
interners.region.borrow_mut().insert(Interned(r));
&*r
};
CommonTypes {
bool: mk(TyBool),
char: mk(TyChar),
@ -379,6 +391,10 @@ impl<'tcx> CommonTypes<'tcx> {
u128: mk(TyUint(ast::UintTy::U128)),
f32: mk(TyFloat(ast::FloatTy::F32)),
f64: mk(TyFloat(ast::FloatTy::F64)),
re_empty: mk_region(Region::ReEmpty),
re_static: mk_region(Region::ReStatic),
re_erased: mk_region(Region::ReErased),
}
}
}
@ -1232,7 +1248,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
pub fn mk_static_str(self) -> Ty<'tcx> {
self.mk_imm_ref(self.mk_region(ty::ReStatic), self.mk_str())
self.mk_imm_ref(self.types.re_static, self.mk_str())
}
pub fn mk_adt(self, def: &'tcx AdtDef, substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {

View File

@ -410,7 +410,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn erase_late_bound_regions<T>(self, value: &Binder<T>) -> T
where T : TypeFoldable<'tcx>
{
self.replace_late_bound_regions(value, |_| self.mk_region(ty::ReErased)).0
self.replace_late_bound_regions(value, |_| self.types.re_erased).0
}
/// 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> {
// whenever a substitution occurs.
match *r {
ty::ReLateBound(..) => r,
_ => self.tcx().mk_region(ty::ReErased)
_ => self.tcx().types.re_erased
}
}
}
@ -565,6 +565,22 @@ pub fn shift_region(region: ty::Region, amount: u32) -> ty::Region {
}
}
pub fn shift_region_ref<'a, 'gcx, 'tcx>(
tcx: TyCtxt<'a, 'gcx, 'tcx>,
region: &'tcx ty::Region,
amount: u32)
-> &'tcx ty::Region
{
match region {
&ty::ReLateBound(debruijn, br) if amount > 0 => {
tcx.mk_region(ty::ReLateBound(debruijn.shifted(amount), br))
}
_ => {
region
}
}
}
pub fn shift_regions<'a, 'gcx, 'tcx, T>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
amount: u32, value: &T) -> T
where T: TypeFoldable<'tcx>
@ -573,7 +589,7 @@ pub fn shift_regions<'a, 'gcx, 'tcx, T>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
value, amount);
value.fold_with(&mut RegionFolder::new(tcx, &mut false, &mut |region, _current_depth| {
tcx.mk_region(shift_region(*region, amount))
shift_region_ref(tcx, region, amount)
}))
}

View File

@ -2499,15 +2499,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
/// Construct a parameter environment suitable for static contexts or other contexts where there
/// are no free type/lifetime parameters in scope.
pub fn empty_parameter_environment(self) -> ParameterEnvironment<'tcx> {
// for an empty parameter environment, there ARE no free
// regions, so it shouldn't matter what we use for the free id
let free_id_outlive = self.region_maps.node_extent(ast::DUMMY_NODE_ID);
ty::ParameterEnvironment {
free_substs: self.intern_substs(&[]),
caller_bounds: Vec::new(),
implicit_region_bound: self.mk_region(ty::ReEmpty),
free_id_outlive: free_id_outlive,
implicit_region_bound: self.types.re_empty,
// for an empty parameter environment, there ARE no free
// regions, so it shouldn't matter what we use for the free id
free_id_outlive: ROOT_CODE_EXTENT,
is_copy_cache: RefCell::new(FxHashMap()),
is_sized_cache: RefCell::new(FxHashMap()),
is_freeze_cache: RefCell::new(FxHashMap()),
@ -2760,4 +2758,3 @@ pub fn provide_extern(providers: &mut ty::maps::Providers) {
pub struct CrateInherentImpls {
pub inherent_impls: DefIdMap<Rc<Vec<DefId>>>,
}

View File

@ -539,6 +539,9 @@ impl<'a, 'gcx, 'tcx> SubstFolder<'a, 'gcx, 'tcx> {
}
fn shift_region_through_binders(&self, region: &'tcx ty::Region) -> &'tcx ty::Region {
if self.region_binders_passed == 0 || !region.has_escaping_regions() {
return region;
}
self.tcx().mk_region(ty::fold::shift_region(*region, self.region_binders_passed))
}
}

View File

@ -412,7 +412,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
/// a suitable "empty substs" for it.
pub fn empty_substs_for_def_id(self, item_def_id: DefId) -> &'tcx ty::Substs<'tcx> {
ty::Substs::for_item(self, item_def_id,
|_, _| self.mk_region(ty::ReErased),
|_, _| self.types.re_erased,
|_, _| {
bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
})

View File

@ -120,7 +120,7 @@ impl<'a, 'tcx> GuaranteeLifetimeContext<'a, 'tcx> {
}
Categorization::StaticItem |
Categorization::Deref(.., mc::UnsafePtr(..)) => {
self.bccx.tcx.mk_region(ty::ReStatic)
self.bccx.tcx.types.re_static
}
Categorization::Deref(.., mc::BorrowedPtr(_, r)) |
Categorization::Deref(.., mc::Implicit(_, r)) => {

View File

@ -343,12 +343,12 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
}
pub fn t_rptr_static(&self) -> Ty<'tcx> {
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(ty::ReStatic),
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.types.re_static,
self.tcx().types.isize)
}
pub fn t_rptr_empty(&self) -> Ty<'tcx> {
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.mk_region(ty::ReEmpty),
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.types.re_empty,
self.tcx().types.isize)
}

View File

@ -280,7 +280,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
assert!(ty.is_slice());
let array_ty = tcx.mk_array(tcx.types.u8, bytes.len());
let array_ref = tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), array_ty);
let array_ref = tcx.mk_imm_ref(tcx.types.re_static, array_ty);
let array = self.literal_operand(test.span, array_ref, Literal::Value {
value: value.clone()
});

View File

@ -308,10 +308,9 @@ fn build_call_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
Adjustment::Deref => Operand::Consume(rcvr_l.deref()),
Adjustment::RefMut => {
// let rcvr = &mut rcvr;
let re_erased = tcx.mk_region(ty::ReErased);
let ref_rcvr = local_decls.push(temp_decl(
Mutability::Not,
tcx.mk_ref(re_erased, ty::TypeAndMut {
tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
ty: sig.inputs()[0],
mutbl: hir::Mutability::MutMutable
}),
@ -321,7 +320,7 @@ fn build_call_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
source_info: source_info,
kind: StatementKind::Assign(
Lvalue::Local(ref_rcvr),
Rvalue::Ref(re_erased, BorrowKind::Mut, rcvr_l)
Rvalue::Ref(tcx.types.re_erased, BorrowKind::Mut, rcvr_l)
)
});
Operand::Consume(Lvalue::Local(ref_rcvr))

View File

@ -13,7 +13,7 @@
//! care erasing regions all over the place.
use rustc::ty::subst::Substs;
use rustc::ty::{Ty, TyCtxt, ReErased, ClosureSubsts};
use rustc::ty::{Ty, TyCtxt, ClosureSubsts};
use rustc::mir::*;
use rustc::mir::visit::MutVisitor;
use rustc::mir::transform::{MirPass, MirSource, Pass};
@ -43,7 +43,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> {
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
match *rvalue {
Rvalue::Ref(ref mut r, _, _) => {
*r = self.tcx.mk_region(ReErased);
*r = self.tcx.types.re_erased;
}
Rvalue::Use(..) |
Rvalue::Repeat(..) |

View File

@ -497,7 +497,7 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
let dest = if dest_needs_borrow(&destination.0) {
debug!("Creating temp for return destination");
let dest = Rvalue::Ref(
self.tcx.mk_region(ty::ReErased),
self.tcx.types.re_erased,
BorrowKind::Mut,
destination.0);
@ -582,7 +582,7 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
fn cast_box_free_arg(&self, arg: Lvalue<'tcx>, ptr_ty: Ty<'tcx>,
callsite: &CallSite<'tcx>, caller_mir: &mut Mir<'tcx>) -> Operand<'tcx> {
let arg = Rvalue::Ref(
self.tcx.mk_region(ty::ReErased),
self.tcx.types.re_erased,
BorrowKind::Mut,
arg.deref());

View File

@ -506,8 +506,7 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
let ty = self.lvalue_ty(self.lvalue);
let substs = tcx.mk_substs(iter::once(Kind::from(ty)));
let re_erased = tcx.mk_region(ty::ReErased);
let ref_ty = tcx.mk_ref(re_erased, ty::TypeAndMut {
let ref_ty = tcx.mk_ref(tcx.types.re_erased, ty::TypeAndMut {
ty: ty,
mutbl: hir::Mutability::MutMutable
});
@ -519,7 +518,7 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
source_info: self.source_info,
kind: StatementKind::Assign(
Lvalue::Local(ref_lvalue),
Rvalue::Ref(re_erased, BorrowKind::Mut, self.lvalue.clone())
Rvalue::Ref(tcx.types.re_erased, BorrowKind::Mut, self.lvalue.clone())
)
}],
terminator: Some(Terminator {

View File

@ -708,7 +708,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
let tr_lvalue = self.const_lvalue(lvalue, span)?;
let ty = tr_lvalue.ty;
let ref_ty = tcx.mk_ref(tcx.mk_region(ty::ReErased),
let ref_ty = tcx.mk_ref(tcx.types.re_erased,
ty::TypeAndMut { ty: ty, mutbl: bk.to_mutbl_lossy() });
let base = match tr_lvalue.base {

View File

@ -329,7 +329,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
let ty = tr_lvalue.ty.to_ty(bcx.tcx());
let ref_ty = bcx.tcx().mk_ref(
bcx.tcx().mk_region(ty::ReErased),
bcx.tcx().types.re_erased,
ty::TypeAndMut { ty: ty, mutbl: bk.to_mutbl_lossy() }
);

View File

@ -109,7 +109,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
let tcx = self.tcx();
let r = match tcx.named_region_map.defs.get(&lifetime.id) {
Some(&rl::Region::Static) => {
tcx.mk_region(ty::ReStatic)
tcx.types.re_static
}
Some(&rl::Region::LateBound(debruijn, id)) => {
@ -171,7 +171,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
.emit();
return Substs::for_item(tcx, def_id, |_, _| {
tcx.mk_region(ty::ReStatic)
tcx.types.re_static
}, |_, _| {
tcx.types.err
});
@ -254,7 +254,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
if let Some(lifetime) = lifetimes.get(i) {
self.ast_region_to_region(lifetime, Some(def))
} else {
tcx.mk_region(ty::ReStatic)
tcx.types.re_static
}
}, |def, substs| {
let i = def.index as usize;
@ -715,7 +715,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
span_err!(tcx.sess, span, E0228,
"the lifetime bound for this object type cannot be deduced \
from context; please supply an explicit bound");
tcx.mk_region(ty::ReStatic)
tcx.types.re_static
})
}
})
@ -1357,7 +1357,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
// If any of the derived region bounds are 'static, that is always
// the best choice.
if derived_region_bounds.iter().any(|&r| ty::ReStatic == *r) {
return Some(tcx.mk_region(ty::ReStatic));
return Some(tcx.types.re_static);
}
// Determine whether there is exactly one unique region in the set

View File

@ -55,7 +55,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let expected_ty = self.structurally_resolved_type(pat.span, expected);
if let ty::TyRef(_, mt) = expected_ty.sty {
if let ty::TySlice(_) = mt.ty.sty {
pat_ty = tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic),
pat_ty = tcx.mk_imm_ref(tcx.types.re_static,
tcx.mk_slice(tcx.types.u8))
}
}

View File

@ -626,7 +626,7 @@ fn revise_self_ty<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
let impl_self_orig = self_substs.region_for_def(def);
let r = if let ty::Region::ReEarlyBound(ref ebr) = *impl_self_orig {
if impl_bindings.region_param(ebr).pure_wrt_drop {
tcx.mk_region(ty::ReStatic)
tcx.types.re_static
} else {
r_orig
}

View File

@ -36,7 +36,7 @@ fn equate_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let def_id = tcx.hir.local_def_id(it.id);
let substs = Substs::for_item(tcx, def_id,
|_, _| tcx.mk_region(ty::ReErased),
|_, _| tcx.types.re_erased,
|def, _| tcx.mk_param_from_def(def));
let fty = tcx.mk_fn_def(def_id, substs, ty::Binder(tcx.mk_fn_sig(

View File

@ -1063,7 +1063,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
// In general, during probing we erase regions. See
// `impl_self_ty()` for an explanation.
let region = tcx.mk_region(ty::ReErased);
let region = tcx.types.re_erased;
// Search through mutabilities in order to find one where pick works:
[hir::MutImmutable, hir::MutMutable]
@ -1325,7 +1325,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
} else {
// In general, during probe we erase regions. See
// `impl_self_ty()` for an explanation.
self.tcx.mk_region(ty::ReErased)
self.tcx.types.re_erased
}
}, |def, cur_substs| {
let i = def.index as usize;
@ -1345,7 +1345,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
let substs = Substs::for_item(self.tcx,
impl_def_id,
|_, _| self.tcx.mk_region(ty::ReErased),
|_, _| self.tcx.types.re_erased,
|_, _| self.next_ty_var(
TypeVariableOrigin::SubstitutionPlaceholder(
self.tcx.def_span(impl_def_id))));

View File

@ -1954,7 +1954,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
//
// FIXME(#27579) all uses of this should be migrated to register_wf_obligation eventually
let cause = traits::ObligationCause::new(span, self.body_id, code);
self.register_region_obligation(ty, self.tcx.mk_region(ty::ReEmpty), cause);
self.register_region_obligation(ty, self.tcx.types.re_empty, cause);
}
/// Registers obligations that all types appearing in `substs` are well-formed.
@ -2513,7 +2513,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
match lit.node {
ast::LitKind::Str(..) => tcx.mk_static_str(),
ast::LitKind::ByteStr(ref v) => {
tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic),
tcx.mk_imm_ref(tcx.types.re_static,
tcx.mk_array(tcx.types.u8, v.len()))
}
ast::LitKind::Byte(_) => tcx.types.u8,

View File

@ -288,8 +288,8 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
let outside_ty = gcx.fold_regions(&inside_ty, &mut false, |r, _| {
match *r {
// 'static is valid everywhere.
ty::ReStatic |
ty::ReEmpty => gcx.mk_region(*r),
ty::ReStatic => gcx.types.re_static,
ty::ReEmpty => gcx.types.re_empty,
// Free regions that come from early-bound regions are valid.
ty::ReFree(ty::FreeRegion {
@ -307,7 +307,7 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
span_err!(self.tcx().sess, span, E0564,
"only named lifetimes are allowed in `impl Trait`, \
but `{}` was found in the type `{}`", r, inside_ty);
gcx.mk_region(ty::ReStatic)
gcx.types.re_static
}
ty::ReVar(_) |
@ -526,7 +526,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Resolver<'cx, 'gcx, 'tcx> {
match self.infcx.fully_resolve(&r) {
Ok(r) => r,
Err(_) => {
self.tcx.mk_region(ty::ReStatic)
self.tcx.types.re_static
}
}
}