Skip to content

Commit 35d98da

Browse files
committed
Auto merge of rust-lang#139240 - Zoxc:erase-region-state, r=<try>
Use a more lightweight cache for `erase_regions` This changes `erase_regions` to use a global hashmap instead of a query to cache types with erased lifetimes. `erase_regions_ty` only depends on the input parameter so it doesn't need to be a query. This is a rebase of rust-lang#59505. <table><tr><td rowspan="2">Benchmark</td><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th></tr><tr><td align="right">Time</td><td align="right">Time</td><td align="right">%</th><td align="right">Physical Memory</td><td align="right">Physical Memory</td><td align="right">%</th><td align="right">Committed Memory</td><td align="right">Committed Memory</td><td align="right">%</th></tr><tr><td>🟣 <b>clap</b>:check</td><td align="right">1.3960s</td><td align="right">1.3883s</td><td align="right"> -0.55%</td><td align="right">147.66 MiB</td><td align="right">147.76 MiB</td><td align="right"> 0.07%</td><td align="right">199.83 MiB</td><td align="right">199.86 MiB</td><td align="right"> 0.01%</td></tr><tr><td>🟣 <b>hyper</b>:check</td><td align="right">0.2346s</td><td align="right">0.2337s</td><td align="right"> -0.38%</td><td align="right">80.35 MiB</td><td align="right">80.54 MiB</td><td align="right"> 0.24%</td><td align="right">129.99 MiB</td><td align="right">130.12 MiB</td><td align="right"> 0.10%</td></tr><tr><td>🟣 <b>regex</b>:check</td><td align="right">0.7811s</td><td align="right">0.7788s</td><td align="right"> -0.30%</td><td align="right">107.75 MiB</td><td align="right">108.07 MiB</td><td align="right"> 0.29%</td><td align="right">153.67 MiB</td><td align="right">153.99 MiB</td><td align="right"> 0.21%</td></tr><tr><td>🟣 <b>syn</b>:check</td><td align="right">1.2999s</td><td align="right">1.2978s</td><td align="right"> -0.17%</td><td align="right">140.24 MiB</td><td align="right">140.35 MiB</td><td align="right"> 0.08%</td><td align="right">188.59 MiB</td><td align="right">188.58 MiB</td><td align="right"> -0.01%</td></tr><tr><td>Total</td><td align="right">3.7117s</td><td align="right">3.6986s</td><td align="right"> -0.35%</td><td align="right">476.00 MiB</td><td align="right">476.72 MiB</td><td align="right"> 0.15%</td><td align="right">672.08 MiB</td><td align="right">672.55 MiB</td><td align="right"> 0.07%</td></tr><tr><td>Summary</td><td align="right">1.0000s</td><td align="right">0.9965s</td><td align="right"> -0.35%</td><td align="right">1 byte</td><td align="right">1.00 bytes</td><td align="right"> 0.17%</td><td align="right">1 byte</td><td align="right">1.00 bytes</td><td align="right"> 0.08%</td></tr></table>
2 parents 79de6c0 + bb40db8 commit 35d98da

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

Diff for: 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" }

Diff for: 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,

Diff for: 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)..

Diff for: 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);

Diff for: 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)