From a548f8917b21c1932f568576b9604119156f37d6 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Wed, 3 Dec 2014 22:20:51 -0800 Subject: [PATCH] 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). --- src/librustc/middle/ty.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index bd2cc24f0b2a..370aa43a252d 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> when equivalent keys can // queried from a HashSet. interner: RefCell, Ty<'tcx>>>, + // FIXME as above, use a hashset if equivalent elements can be queried. + substs_interner: RefCell, &'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 } }