Skip to content

Commit

Permalink
Auto merge of #65293 - tmandry:turbo-expander, r=<try>
Browse files Browse the repository at this point in the history
Optimize `try_expand_impl_trait_type`

A lot of time was being spent expanding some large `impl Future` types in fuchsia. This PR takes the number of types being visited in one expansion from >3 billion to about a thousand, and eliminates the compile time regression in #65147 (in fact, compile times are better than they were before).

Thanks to @matthewjasper for suggesting this change.

Fixes #65147.
r? @matthewjasper,@nikomatsakis
  • Loading branch information
bors committed Oct 14, 2019
2 parents e413dc3 + 6493e6b commit 0c36cd3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/librustc/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,9 @@ impl<'tcx> TyCtxt<'tcx> {
// that type, and when we finish expanding that type we remove the
// its DefId.
seen_opaque_tys: FxHashSet<DefId>,
// Cache of all expansions we've seen so far. This is a critical
// optimization for some large types produced by async fn trees.
expanded_cache: FxHashMap<DefId, Ty<'tcx>>,
primary_def_id: DefId,
found_recursion: bool,
tcx: TyCtxt<'tcx>,
Expand All @@ -713,10 +716,17 @@ impl<'tcx> TyCtxt<'tcx> {
}
let substs = substs.fold_with(self);
if self.seen_opaque_tys.insert(def_id) {
let generic_ty = self.tcx.type_of(def_id);
let concrete_ty = generic_ty.subst(self.tcx, substs);
let expanded_ty = self.fold_ty(concrete_ty);
self.seen_opaque_tys.remove(&def_id);
let expanded_ty = match self.expanded_cache.get(&def_id) {
Some(expanded_ty) => expanded_ty,
None => {
let generic_ty = self.tcx.type_of(def_id);
let concrete_ty = generic_ty.subst(self.tcx, substs);
let expanded_ty = self.fold_ty(concrete_ty);
self.seen_opaque_tys.remove(&def_id);
self.expanded_cache.insert(def_id, expanded_ty);
expanded_ty
}
};
Some(expanded_ty)
} else {
// If another opaque type that we contain is recursive, then it
Expand All @@ -735,14 +745,17 @@ impl<'tcx> TyCtxt<'tcx> {
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
if let ty::Opaque(def_id, substs) = t.kind {
self.expand_opaque_ty(def_id, substs).unwrap_or(t)
} else {
} else if t.has_projections() {
t.super_fold_with(self)
} else {
t
}
}
}

let mut visitor = OpaqueTypeExpander {
seen_opaque_tys: FxHashSet::default(),
expanded_cache: FxHashMap::default(),
primary_def_id: def_id,
found_recursion: false,
tcx: self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ error[E0720]: opaque type expands to a recursive type
LL | fn recursive_wrap() -> impl Sized {
| ^^^^^^^^^^ expands to a recursive type
|
= note: expanded type is `((impl Sized,),)`
= note: expanded type is `(impl Sized,)`

error[E0720]: opaque type expands to a recursive type
--> $DIR/recursive-impl-trait-type--through-non-recursize.rs:21:25
|
LL | fn recursive_wrap2() -> impl Sized {
| ^^^^^^^^^^ expands to a recursive type
|
= note: expanded type is `((impl Sized,),)`
= note: expanded type is `(impl Sized,)`

error: aborting due to 4 previous errors

Expand Down

0 comments on commit 0c36cd3

Please sign in to comment.