Skip to content

Commit c778a3d

Browse files
Auto merge of #142435 - compiler-errors:perf-cache-norm, r=<try>
[perf] Cache structural resolution in typeck 🤷
2 parents 6c8138d + b835a90 commit c778a3d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,14 +1439,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14391439
if self.next_trait_solver()
14401440
&& let ty::Alias(..) = ty.kind()
14411441
{
1442+
if let Some(&normalized_ty) = self.normalization_cache.borrow().get(&ty) {
1443+
return normalized_ty;
1444+
}
14421445
// We need to use a separate variable here as otherwise the temporary for
14431446
// `self.fulfillment_cx.borrow_mut()` is alive in the `Err` branch, resulting
14441447
// in a reentrant borrow, causing an ICE.
14451448
let result = self
14461449
.at(&self.misc(sp), self.param_env)
14471450
.structurally_normalize_ty(ty, &mut **self.fulfillment_cx.borrow_mut());
14481451
match result {
1449-
Ok(normalized_ty) => normalized_ty,
1452+
Ok(normalized_ty) => {
1453+
self.normalization_cache.borrow_mut().insert(ty, normalized_ty);
1454+
self.resolve_vars_with_obligations(normalized_ty)
1455+
}
14501456
Err(errors) => {
14511457
let guar = self.err_ctxt().report_fulfillment_errors(errors);
14521458
return Ty::new_error(self.tcx, guar);

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::cell::{Cell, RefCell};
99
use std::ops::Deref;
1010

1111
use hir::def_id::CRATE_DEF_ID;
12+
use rustc_data_structures::fx::FxHashMap;
1213
use rustc_errors::DiagCtxtHandle;
1314
use rustc_hir::def_id::{DefId, LocalDefId};
1415
use rustc_hir::{self as hir, HirId, ItemLocalMap};
@@ -122,6 +123,8 @@ pub(crate) struct FnCtxt<'a, 'tcx> {
122123
/// These are stored here so we may collect them when canonicalizing user
123124
/// type ascriptions later.
124125
pub(super) trait_ascriptions: RefCell<ItemLocalMap<Vec<ty::Clause<'tcx>>>>,
126+
127+
pub(super) normalization_cache: RefCell<FxHashMap<Ty<'tcx>, Ty<'tcx>>>,
125128
}
126129

127130
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -150,6 +153,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
150153
diverging_fallback_behavior,
151154
diverging_block_behavior,
152155
trait_ascriptions: Default::default(),
156+
normalization_cache: Default::default(),
153157
}
154158
}
155159

0 commit comments

Comments
 (0)