about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2014-12-03 22:20:51 -0800
committerHuon Wilson <dbau.pp+github@gmail.com>2014-12-29 23:55:24 +1100
commita548f8917b21c1932f568576b9604119156f37d6 (patch)
treeab9b8d7dd029de8d0c0c2c99052bd76f62c80fbf /src
parent4f2b0f032a2b479b6cf728646283bf6e46b32098 (diff)
downloadrust-a548f8917b21c1932f568576b9604119156f37d6.tar.gz
rust-a548f8917b21c1932f568576b9604119156f37d6.zip
Intern substs before storing them in the tcx.
This cuts memory use dramatically from the previous commit, and reduces
use overall. E.g. the memory usage of `rustc -O librustc/lib.rs` seems
to drop 100MB from 1.98GB to 1.88GB (on one run anyway).
Diffstat (limited to 'src')
-rw-r--r--src/librustc/middle/ty.rs16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs
index bd2cc24f0b2..370aa43a252 100644
--- a/src/librustc/middle/ty.rs
+++ b/src/librustc/middle/ty.rs
@@ -617,6 +617,9 @@ pub struct ctxt<'tcx> {
     // FIXME(eddyb) use a FnvHashSet<InternedTy<'tcx>> when equivalent keys can
     // queried from a HashSet.
     interner: RefCell<FnvHashMap<InternedTy<'tcx>, Ty<'tcx>>>,
+    // FIXME as above, use a hashset if equivalent elements can be queried.
+    substs_interner: RefCell<FnvHashMap<&'tcx Substs<'tcx>, &'tcx Substs<'tcx>>>,
+
     pub sess: Session,
     pub def_map: DefMap,
 
@@ -848,6 +851,8 @@ impl<'tcx> ctxt<'tcx> {
             self,
             ty_enum, ty_uniq, ty_vec, ty_ptr, ty_rptr, ty_bare_fn, ty_closure, ty_trait,
             ty_struct, ty_unboxed_closure, ty_tup, ty_param, ty_open, ty_infer);
+
+        println!("Substs interner: #{}", self.substs_interner.borrow().len());
     }
 }
 
@@ -2063,6 +2068,7 @@ pub fn mk_ctxt<'tcx>(s: Session,
         type_arena: type_arena,
         substs_arena: substs_arena,
         interner: RefCell::new(FnvHashMap::new()),
+        substs_interner: RefCell::new(FnvHashMap::new()),
         named_region_map: named_region_map,
         item_variance_map: RefCell::new(DefIdMap::new()),
         variance_computed: Cell::new(false),
@@ -2123,8 +2129,14 @@ pub fn mk_ctxt<'tcx>(s: Session,
 // Type constructors
 
 impl<'tcx> ctxt<'tcx> {
-    pub fn mk_substs(&self, subst: Substs<'tcx>) -> &'tcx Substs<'tcx> {
-        self.substs_arena.alloc(subst)
+    pub fn mk_substs(&self, substs: Substs<'tcx>) -> &'tcx Substs<'tcx> {
+        if let Some(substs) = self.substs_interner.borrow().get(&substs) {
+            return *substs;
+        }
+
+        let substs = self.substs_arena.alloc(substs);
+        self.substs_interner.borrow_mut().insert(substs, substs);
+        substs
     }
 }