Skip to content

Commit

Permalink
Rollup merge of rust-lang#65260 - nnethercote:optimize-LexicalResolve…
Browse files Browse the repository at this point in the history
…-expansion, r=nikomatsakis

Optimize `LexicalResolve::expansion`.

A win for `unicode_normalization`.

r? @nikomatsakis
  • Loading branch information
tmandry committed Oct 15, 2019
2 parents d2f87e3 + 8cd25e7 commit a9a4d40
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/librustc/infer/lexical_region_resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
}

fn expansion(&self, var_values: &mut LexicalRegionResolutions<'tcx>) {
self.iterate_until_fixed_point("Expansion", |constraint| {
self.iterate_until_fixed_point(|constraint| {
debug!("expansion: constraint={:?}", constraint);
let (a_region, b_vid, b_data, retain) = match *constraint {
Constraint::RegSubVar(a_region, b_vid) => {
Expand Down Expand Up @@ -360,13 +360,21 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
match *b_data {
VarValue::Value(cur_region) => {
// Identical scopes can show up quite often, if the fixed point
// iteration converges slowly, skip them
// iteration converges slowly. Skip them. This is purely an
// optimization.
if let (ReScope(a_scope), ReScope(cur_scope)) = (a_region, cur_region) {
if a_scope == cur_scope {
return false;
}
}

// This is a specialized version of the `lub_concrete_regions`
// check below for a common case, here purely as an
// optimization.
if let ReEmpty = a_region {
return false;
}

let mut lub = self.lub_concrete_regions(a_region, cur_region);
if lub == cur_region {
return false;
Expand Down Expand Up @@ -407,8 +415,6 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {

/// Returns the smallest region `c` such that `a <= c` and `b <= c`.
fn lub_concrete_regions(&self, a: Region<'tcx>, b: Region<'tcx>) -> Region<'tcx> {
let tcx = self.tcx();

match (a, b) {
(&ty::ReClosureBound(..), _)
| (_, &ty::ReClosureBound(..))
Expand Down Expand Up @@ -468,15 +474,15 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {

// otherwise, we don't know what the free region is,
// so we must conservatively say the LUB is static:
tcx.lifetimes.re_static
self.tcx().lifetimes.re_static
}

(&ReScope(a_id), &ReScope(b_id)) => {
// The region corresponding to an outer block is a
// subtype of the region corresponding to an inner
// block.
let lub = self.region_rels.region_scope_tree.nearest_common_ancestor(a_id, b_id);
tcx.mk_region(ReScope(lub))
self.tcx().mk_region(ReScope(lub))
}

(&ReEarlyBound(_), &ReEarlyBound(_))
Expand All @@ -490,7 +496,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
if a == b {
a
} else {
tcx.lifetimes.re_static
self.tcx().lifetimes.re_static
}
}
}
Expand Down Expand Up @@ -860,7 +866,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
}
}

fn iterate_until_fixed_point<F>(&self, tag: &str, mut body: F)
fn iterate_until_fixed_point<F>(&self, mut body: F)
where
F: FnMut(&Constraint<'tcx>) -> (bool, bool),
{
Expand All @@ -870,7 +876,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
while changed {
changed = false;
iteration += 1;
debug!("---- {} Iteration {}{}", "#", tag, iteration);
debug!("---- Expansion iteration {}", iteration);
constraints.retain(|constraint| {
let (edge_changed, retain) = body(constraint);
if edge_changed {
Expand All @@ -880,7 +886,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
retain
});
}
debug!("---- {} Complete after {} iteration(s)", tag, iteration);
debug!("---- Expansion complete after {} iteration(s)", iteration);
}

fn bound_is_met(
Expand Down

0 comments on commit a9a4d40

Please sign in to comment.