Skip to content

Commit 29dad1a

Browse files
committed
introduce a specializes cache
This query is frequently used during trait selection and caching the result can be a reasonable performance win.
1 parent 5d12502 commit 29dad1a

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/librustc/traits/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub use self::select::{EvaluationCache, SelectionContext, SelectionCache};
3838
pub use self::select::{MethodMatchResult, MethodMatched, MethodAmbiguous, MethodDidNotMatch};
3939
pub use self::select::{MethodMatchedData}; // intentionally don't export variants
4040
pub use self::specialize::{OverlapError, specialization_graph, specializes, translate_substs};
41+
pub use self::specialize::{SpecializesCache};
4142
pub use self::util::elaborate_predicates;
4243
pub use self::util::supertraits;
4344
pub use self::util::Supertraits;

src/librustc/traits/specialize/mod.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use super::{SelectionContext, FulfillmentContext};
2121
use super::util::{fresh_type_vars_for_impl, impl_trait_ref_and_oblig};
2222

23+
use rustc_data_structures::fnv::FnvHashMap;
2324
use hir::def_id::DefId;
2425
use infer::{InferCtxt, TypeOrigin};
2526
use middle::region;
@@ -111,6 +112,10 @@ pub fn translate_substs<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
111112
pub fn specializes<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
112113
impl1_def_id: DefId,
113114
impl2_def_id: DefId) -> bool {
115+
if let Some(r) = tcx.specializes_cache.borrow().check(impl1_def_id, impl2_def_id) {
116+
return r;
117+
}
118+
114119
// The feature gate should prevent introducing new specializations, but not
115120
// taking advantage of upstream ones.
116121
if !tcx.sess.features.borrow().specialization &&
@@ -146,7 +151,7 @@ pub fn specializes<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
146151
.unwrap()
147152
.subst(tcx, &penv.free_substs);
148153

149-
tcx.normalizing_infer_ctxt(ProjectionMode::Topmost).enter(|mut infcx| {
154+
let result = tcx.normalizing_infer_ctxt(ProjectionMode::Topmost).enter(|mut infcx| {
150155
// Normalize the trait reference, adding any obligations
151156
// that arise into the impl1 assumptions.
152157
let Normalized { value: impl1_trait_ref, obligations: normalization_obligations } = {
@@ -167,7 +172,10 @@ pub fn specializes<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
167172

168173
// Attempt to prove that impl2 applies, given all of the above.
169174
fulfill_implication(&infcx, impl1_trait_ref, impl2_def_id).is_ok()
170-
})
175+
});
176+
177+
tcx.specializes_cache.borrow_mut().insert(impl1_def_id, impl2_def_id, result);
178+
result
171179
}
172180

173181
/// Attempt to fulfill all obligations of `target_impl` after unification with
@@ -225,3 +233,23 @@ fn fulfill_implication<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
225233
}
226234
})
227235
}
236+
237+
pub struct SpecializesCache {
238+
map: FnvHashMap<(DefId, DefId), bool>
239+
}
240+
241+
impl SpecializesCache {
242+
pub fn new() -> Self {
243+
SpecializesCache {
244+
map: FnvHashMap()
245+
}
246+
}
247+
248+
pub fn check(&self, a: DefId, b: DefId) -> Option<bool> {
249+
self.map.get(&(a, b)).cloned()
250+
}
251+
252+
pub fn insert(&mut self, a: DefId, b: DefId, result: bool) {
253+
self.map.insert((a, b), result);
254+
}
255+
}

src/librustc/ty/context.rs

+3
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ impl<'a, 'gcx, 'tcx> Deref for TyCtxt<'a, 'gcx, 'tcx> {
291291
pub struct GlobalCtxt<'tcx> {
292292
global_interners: CtxtInterners<'tcx>,
293293

294+
pub specializes_cache: RefCell<traits::SpecializesCache>,
295+
294296
pub dep_graph: DepGraph,
295297

296298
/// Common types, pre-interned for your convenience.
@@ -637,6 +639,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
637639
let dep_graph = map.dep_graph.clone();
638640
let fulfilled_predicates = traits::GlobalFulfilledPredicates::new(dep_graph.clone());
639641
tls::enter_global(GlobalCtxt {
642+
specializes_cache: RefCell::new(traits::SpecializesCache::new()),
640643
global_interners: interners,
641644
dep_graph: dep_graph.clone(),
642645
types: common_types,

0 commit comments

Comments
 (0)