Skip to content

Commit bb40db8

Browse files
committed
Use a more lightweight cache for erase_regions
1 parent 70dab5a commit bb40db8

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

compiler/rustc_middle/src/query/mod.rs

-14
Original file line numberDiff line numberDiff line change
@@ -660,20 +660,6 @@ rustc_queries! {
660660
separate_provide_extern
661661
}
662662

663-
/// Erases regions from `ty` to yield a new type.
664-
/// Normally you would just use `tcx.erase_regions(value)`,
665-
/// however, which uses this query as a kind of cache.
666-
query erase_regions_ty(ty: Ty<'tcx>) -> Ty<'tcx> {
667-
// This query is not expected to have input -- as a result, it
668-
// is not a good candidates for "replay" because it is essentially a
669-
// pure function of its input (and hence the expectation is that
670-
// no caller would be green **apart** from just these
671-
// queries). Making it anonymous avoids hashing the result, which
672-
// may save a bit of time.
673-
anon
674-
desc { "erasing regions from `{}`", ty }
675-
}
676-
677663
query wasm_import_module_map(_: CrateNum) -> &'tcx DefIdMap<String> {
678664
arena_cache
679665
desc { "getting wasm import module map" }

compiler/rustc_middle/src/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,9 @@ pub struct GlobalCtxt<'tcx> {
13761376
/// Common consts, pre-interned for your convenience.
13771377
pub consts: CommonConsts<'tcx>,
13781378

1379+
/// A cache for the `erase_regions` function.
1380+
pub(in crate::ty) erased_region_cache: ShardedHashMap<Ty<'tcx>, Ty<'tcx>>,
1381+
13791382
/// Hooks to be able to register functions in other crates that can then still
13801383
/// be called from rustc_middle.
13811384
pub(crate) hooks: crate::hooks::Providers,
@@ -1617,6 +1620,7 @@ impl<'tcx> TyCtxt<'tcx> {
16171620
types: common_types,
16181621
lifetimes: common_lifetimes,
16191622
consts: common_consts,
1623+
erased_region_cache: Default::default(),
16201624
untracked,
16211625
query_system,
16221626
query_kinds,

compiler/rustc_middle/src/ty/erase_regions.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
use tracing::debug;
22

3-
use crate::query::Providers;
3+
use crate::dep_graph::DepGraph;
44
use crate::ty::{
55
self, Ty, TyCtxt, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
66
};
77

8-
pub(super) fn provide(providers: &mut Providers) {
9-
*providers = Providers { erase_regions_ty, ..*providers };
10-
}
8+
impl<'tcx> TyCtxt<'tcx> {
9+
/// Erases regions from `ty` to yield a new type.
10+
pub fn erase_regions_ty(self, ty: Ty<'tcx>) -> Ty<'tcx> {
11+
if let Some(ty) = self.erased_region_cache.get(&ty) {
12+
return ty;
13+
}
1114

12-
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 })
16-
}
15+
let result = DepGraph::debug_assert_no_deps(|| {
16+
// N.B., use `super_fold_with` here. If we used `fold_with`, it
17+
// could invoke the `erase_regions_ty` function recursively.
18+
ty.super_fold_with(&mut RegionEraserVisitor { tcx: self })
19+
});
20+
21+
self.erased_region_cache.insert(ty, result);
22+
result
23+
}
1724

18-
impl<'tcx> TyCtxt<'tcx> {
1925
/// Returns an equivalent value with all free regions removed (note
2026
/// that late-bound regions remain, because they are important for
2127
/// subtyping, but they are anonymized and normalized as well)..

compiler/rustc_middle/src/ty/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2129,7 +2129,6 @@ pub fn ast_uint_ty(uty: UintTy) -> ast::UintTy {
21292129
pub fn provide(providers: &mut Providers) {
21302130
closure::provide(providers);
21312131
context::provide(providers);
2132-
erase_regions::provide(providers);
21332132
inhabitedness::provide(providers);
21342133
util::provide(providers);
21352134
print::provide(providers);

compiler/rustc_query_system/src/dep_graph/graph.rs

+8
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ impl<D: Deps> DepGraph<D> {
268268
D::with_deps(TaskDepsRef::Forbid, op)
269269
}
270270

271+
/// This checks that no dependencies are registered in `op` if debug assertions are enabled.
272+
pub fn debug_assert_no_deps<OP, R>(op: OP) -> R
273+
where
274+
OP: FnOnce() -> R,
275+
{
276+
if cfg!(debug_assertions) { D::with_deps(TaskDepsRef::Forbid, op) } else { op() }
277+
}
278+
271279
#[inline(always)]
272280
pub fn with_task<Ctxt: HasDepContext<Deps = D>, A: Debug, R>(
273281
&self,

0 commit comments

Comments
 (0)