about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2016-01-06 10:16:58 -0500
committerNiko Matsakis <niko@alum.mit.edu>2016-01-06 10:16:58 -0500
commit0c8ee65020a9d6ef60506741d9db1582dc8f6834 (patch)
treebec29a9918c84e68fa29e049f886a23ba307b44a
parenta9d7e366681c0157a557ff0083aa56fae9469fbf (diff)
downloadrust-0c8ee65020a9d6ef60506741d9db1582dc8f6834.tar.gz
rust-0c8ee65020a9d6ef60506741d9db1582dc8f6834.zip
Use `memoized` helper more often.
-rw-r--r--src/librustc/middle/ty/context.rs39
-rw-r--r--src/librustc/middle/ty/maps.rs3
2 files changed, 15 insertions, 27 deletions
diff --git a/src/librustc/middle/ty/context.rs b/src/librustc/middle/ty/context.rs
index 8f313120c9d..d1504d25288 100644
--- a/src/librustc/middle/ty/context.rs
+++ b/src/librustc/middle/ty/context.rs
@@ -13,7 +13,7 @@
 // FIXME: (@jroesch) @eddyb should remove this when he renames ctxt
 #![allow(non_camel_case_types)]
 
-use dep_graph::{DepGraph, DepNode, DepTrackingMap};
+use dep_graph::{DepGraph, DepTrackingMap};
 use front::map as ast_map;
 use session::Session;
 use lint;
@@ -256,9 +256,8 @@ pub struct ctxt<'tcx> {
     pub trait_item_def_ids: RefCell<DepTrackingMap<maps::TraitItemDefIds<'tcx>>>,
 
     /// A cache for the trait_items() routine; note that the routine
-    /// itself pushes the `TraitItems` dependency node. This cache is
-    /// "encapsulated" and thus does not need to be itself tracked.
-    trait_items_cache: RefCell<DefIdMap<Rc<Vec<ty::ImplOrTraitItem<'tcx>>>>>,
+    /// itself pushes the `TraitItems` dependency node.
+    trait_items_cache: RefCell<DepTrackingMap<maps::TraitItems<'tcx>>>,
 
     pub impl_trait_refs: RefCell<DepTrackingMap<maps::ImplTraitRefs<'tcx>>>,
     pub trait_defs: RefCell<DepTrackingMap<maps::TraitDefs<'tcx>>>,
@@ -371,9 +370,7 @@ pub struct ctxt<'tcx> {
     pub fulfilled_predicates: RefCell<traits::FulfilledPredicates<'tcx>>,
 
     /// Caches the representation hints for struct definitions.
-    ///
-    /// This is encapsulated by the `ReprHints` task and hence is not tracked.
-    repr_hint_cache: RefCell<DefIdMap<Rc<Vec<attr::ReprAttr>>>>,
+    repr_hint_cache: RefCell<DepTrackingMap<maps::ReprHints<'tcx>>>,
 
     /// Maps Expr NodeId's to their constant qualification.
     pub const_qualif_map: RefCell<NodeMap<middle::check_const::ConstQualif>>,
@@ -544,7 +541,7 @@ impl<'tcx> ctxt<'tcx> {
             ast_ty_to_ty_cache: RefCell::new(NodeMap()),
             impl_or_trait_items: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
             trait_item_def_ids: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
-            trait_items_cache: RefCell::new(DefIdMap()),
+            trait_items_cache: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
             ty_param_defs: RefCell::new(NodeMap()),
             normalized_cache: RefCell::new(FnvHashMap()),
             lang_items: lang_items,
@@ -561,7 +558,7 @@ impl<'tcx> ctxt<'tcx> {
             stability: RefCell::new(stability),
             selection_cache: traits::SelectionCache::new(),
             evaluation_cache: traits::EvaluationCache::new(),
-            repr_hint_cache: RefCell::new(DefIdMap()),
+            repr_hint_cache: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
             const_qualif_map: RefCell::new(NodeMap()),
             custom_coerce_unsized_kinds: RefCell::new(DefIdMap()),
             cast_kinds: RefCell::new(NodeMap()),
@@ -1032,28 +1029,16 @@ impl<'tcx> ctxt<'tcx> {
     }
 
     pub fn trait_items(&self, trait_did: DefId) -> Rc<Vec<ty::ImplOrTraitItem<'tcx>>> {
-        // since this is cached, pushing a dep-node for the
-        // computation yields the correct dependencies.
-        let _task = self.dep_graph.in_task(DepNode::TraitItems(trait_did));
-
-        let mut trait_items = self.trait_items_cache.borrow_mut();
-        match trait_items.get(&trait_did).cloned() {
-            Some(trait_items) => trait_items,
-            None => {
-                let def_ids = self.trait_item_def_ids(trait_did);
-                let items: Rc<Vec<_>> =
-                    Rc::new(def_ids.iter()
-                                   .map(|d| self.impl_or_trait_item(d.def_id()))
-                                   .collect());
-                trait_items.insert(trait_did, items.clone());
-                items
-            }
-        }
+        self.trait_items_cache.memoize(trait_did, || {
+            let def_ids = self.trait_item_def_ids(trait_did);
+            Rc::new(def_ids.iter()
+                           .map(|d| self.impl_or_trait_item(d.def_id()))
+                           .collect())
+        })
     }
 
     /// Obtain the representation annotation for a struct definition.
     pub fn lookup_repr_hints(&self, did: DefId) -> Rc<Vec<attr::ReprAttr>> {
-        let _task = self.dep_graph.in_task(DepNode::ReprHints(did));
         self.repr_hint_cache.memoize(did, || {
             Rc::new(if did.is_local() {
                 self.get_attrs(did).iter().flat_map(|meta| {
diff --git a/src/librustc/middle/ty/maps.rs b/src/librustc/middle/ty/maps.rs
index 924d420613c..7d5276f379f 100644
--- a/src/librustc/middle/ty/maps.rs
+++ b/src/librustc/middle/ty/maps.rs
@@ -13,6 +13,7 @@ use middle::def_id::DefId;
 use middle::ty;
 use std::marker::PhantomData;
 use std::rc::Rc;
+use syntax::attr;
 
 macro_rules! dep_map_ty {
     ($ty_name:ident : $node_name:ident ($key:ty) -> $value:ty) => {
@@ -39,3 +40,5 @@ dep_map_ty! { AdtDefs: ItemSignature(DefId) -> ty::AdtDefMaster<'tcx> }
 dep_map_ty! { ItemVariances: ItemSignature(DefId) -> Rc<ty::ItemVariances> }
 dep_map_ty! { InherentImpls: InherentImpls(DefId) -> Rc<Vec<DefId>> }
 dep_map_ty! { ImplItems: ImplItems(DefId) -> Vec<ty::ImplOrTraitItemId> }
+dep_map_ty! { TraitItems: TraitItems(DefId) -> Rc<Vec<ty::ImplOrTraitItem<'tcx>>> }
+dep_map_ty! { ReprHints: ReprHints(DefId) -> Rc<Vec<attr::ReprAttr>> }