Skip to content

Commit e5daab6

Browse files
Use a full cache for erasing regions
1 parent 292ecd5 commit e5daab6

File tree

4 files changed

+11
-26
lines changed

4 files changed

+11
-26
lines changed

compiler/rustc_middle/src/query/mod.rs

-14
Original file line numberDiff line numberDiff line change
@@ -678,20 +678,6 @@ rustc_queries! {
678678
separate_provide_extern
679679
}
680680

681-
/// Erases regions from `ty` to yield a new type.
682-
/// Normally you would just use `tcx.erase_regions(value)`,
683-
/// however, which uses this query as a kind of cache.
684-
query erase_regions_ty(ty: Ty<'tcx>) -> Ty<'tcx> {
685-
// This query is not expected to have input -- as a result, it
686-
// is not a good candidates for "replay" because it is essentially a
687-
// pure function of its input (and hence the expectation is that
688-
// no caller would be green **apart** from just these
689-
// queries). Making it anonymous avoids hashing the result, which
690-
// may save a bit of time.
691-
anon
692-
desc { "erasing regions from `{}`", ty }
693-
}
694-
695681
query wasm_import_module_map(_: CrateNum) -> &'tcx DefIdMap<String> {
696682
arena_cache
697683
desc { "getting wasm import module map" }

compiler/rustc_middle/src/ty/context.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,8 @@ pub struct GlobalCtxt<'tcx> {
14541454

14551455
/// A jobserver reference used to release then acquire a token while waiting on a query.
14561456
pub jobserver_proxy: Arc<Proxy>,
1457+
1458+
pub erased_ty_cache: Lock<FxHashMap<Ty<'tcx>, Ty<'tcx>>>,
14571459
}
14581460

14591461
impl<'tcx> GlobalCtxt<'tcx> {
@@ -1694,6 +1696,7 @@ impl<'tcx> TyCtxt<'tcx> {
16941696
alloc_map: interpret::AllocMap::new(),
16951697
current_gcx,
16961698
jobserver_proxy,
1699+
erased_ty_cache: Default::default(),
16971700
});
16981701

16991702
// This is a separate function to work around a crash with parallel rustc (#135870)

compiler/rustc_middle/src/ty/erase_regions.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
use tracing::debug;
22

3-
use crate::query::Providers;
43
use crate::ty::{
54
self, Ty, TyCtxt, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
65
};
76

8-
pub(super) fn provide(providers: &mut Providers) {
9-
*providers = Providers { erase_regions_ty, ..*providers };
10-
}
11-
127
fn erase_regions_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
13-
// N.B., use `super_fold_with` here. If we used `fold_with`, it
14-
// could invoke the `erase_regions_ty` query recursively.
15-
ty.super_fold_with(&mut RegionEraserVisitor { tcx })
8+
if let Some(erased) = tcx.erased_ty_cache.lock().get(&ty) {
9+
*erased
10+
} else {
11+
let erased = ty.super_fold_with(&mut RegionEraserVisitor { tcx });
12+
tcx.erased_ty_cache.lock().insert(ty, erased);
13+
erased
14+
}
1615
}
1716

1817
impl<'tcx> TyCtxt<'tcx> {
@@ -46,10 +45,8 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RegionEraserVisitor<'tcx> {
4645
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
4746
if !ty.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
4847
ty
49-
} else if ty.has_infer() {
50-
ty.super_fold_with(self)
5148
} else {
52-
self.tcx.erase_regions_ty(ty)
49+
erase_regions_ty(self.tcx, ty)
5350
}
5451
}
5552

compiler/rustc_middle/src/ty/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2242,7 +2242,6 @@ pub fn ast_uint_ty(uty: UintTy) -> ast::UintTy {
22422242
pub fn provide(providers: &mut Providers) {
22432243
closure::provide(providers);
22442244
context::provide(providers);
2245-
erase_regions::provide(providers);
22462245
inhabitedness::provide(providers);
22472246
util::provide(providers);
22482247
print::provide(providers);

0 commit comments

Comments
 (0)