Skip to content

Commit d637c85

Browse files
committed
[WIP] rustc: experiment with ParamEnv-aware caching in traits::select.
1 parent 04e69e4 commit d637c85

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/librustc/traits/select.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ struct TraitObligationStack<'prev, 'tcx> {
204204
#[derive(Clone, Default)]
205205
pub struct SelectionCache<'tcx> {
206206
hashmap: Lock<
207-
FxHashMap<ty::TraitRef<'tcx>, WithDepNode<SelectionResult<'tcx, SelectionCandidate<'tcx>>>>,
207+
FxHashMap<
208+
ty::ParamEnvAnd<'tcx, ty::TraitRef<'tcx>>,
209+
WithDepNode<SelectionResult<'tcx, SelectionCandidate<'tcx>>>,
210+
>,
208211
>,
209212
}
210213

@@ -490,7 +493,9 @@ impl<'tcx> From<OverflowError> for SelectionError<'tcx> {
490493

491494
#[derive(Clone, Default)]
492495
pub struct EvaluationCache<'tcx> {
493-
hashmap: Lock<FxHashMap<ty::PolyTraitRef<'tcx>, WithDepNode<EvaluationResult>>>,
496+
hashmap: Lock<
497+
FxHashMap<ty::ParamEnvAnd<'tcx, ty::PolyTraitRef<'tcx>>, WithDepNode<EvaluationResult>>,
498+
>,
494499
}
495500

496501
impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
@@ -1143,15 +1148,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11431148
let tcx = self.tcx();
11441149
if self.can_use_global_caches(param_env) {
11451150
let cache = tcx.evaluation_cache.hashmap.borrow();
1146-
if let Some(cached) = cache.get(&trait_ref) {
1151+
if let Some(cached) = cache.get(&param_env.and(trait_ref)) {
11471152
return Some(cached.get(tcx));
11481153
}
11491154
}
11501155
self.infcx
11511156
.evaluation_cache
11521157
.hashmap
11531158
.borrow()
1154-
.get(&trait_ref)
1159+
.get(&param_env.and(trait_ref))
11551160
.map(|v| v.get(tcx))
11561161
}
11571162

@@ -1182,7 +1187,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11821187
.evaluation_cache
11831188
.hashmap
11841189
.borrow_mut()
1185-
.insert(trait_ref, WithDepNode::new(dep_node, result));
1190+
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, result));
11861191
return;
11871192
}
11881193
}
@@ -1195,7 +1200,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11951200
.evaluation_cache
11961201
.hashmap
11971202
.borrow_mut()
1198-
.insert(trait_ref, WithDepNode::new(dep_node, result));
1203+
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, result));
11991204
}
12001205

12011206
/// For various reasons, it's possible for a subobligation
@@ -1575,7 +1580,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15751580
// rule seems to be pretty clearly safe and also still retains
15761581
// a very high hit rate (~95% when compiling rustc).
15771582
if !param_env.caller_bounds.is_empty() {
1578-
return false;
1583+
//return false;
15791584
}
15801585

15811586
// Avoid using the master cache during coherence and just rely
@@ -1602,15 +1607,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16021607
let trait_ref = &cache_fresh_trait_pred.skip_binder().trait_ref;
16031608
if self.can_use_global_caches(param_env) {
16041609
let cache = tcx.selection_cache.hashmap.borrow();
1605-
if let Some(cached) = cache.get(&trait_ref) {
1610+
if let Some(cached) = cache.get(&param_env.and(*trait_ref)) {
16061611
return Some(cached.get(tcx));
16071612
}
16081613
}
16091614
self.infcx
16101615
.selection_cache
16111616
.hashmap
16121617
.borrow()
1613-
.get(trait_ref)
1618+
.get(&param_env.and(*trait_ref))
16141619
.map(|v| v.get(tcx))
16151620
}
16161621

@@ -1671,7 +1676,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16711676
tcx.selection_cache
16721677
.hashmap
16731678
.borrow_mut()
1674-
.insert(trait_ref, WithDepNode::new(dep_node, candidate));
1679+
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, candidate));
16751680
return;
16761681
}
16771682
}
@@ -1685,7 +1690,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16851690
.selection_cache
16861691
.hashmap
16871692
.borrow_mut()
1688-
.insert(trait_ref, WithDepNode::new(dep_node, candidate));
1693+
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, candidate));
16891694
}
16901695

16911696
fn assemble_candidates<'o>(

0 commit comments

Comments
 (0)