Skip to content

Commit a548f89

Browse files
committed
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).
1 parent 4f2b0f0 commit a548f89

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/librustc/middle/ty.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,9 @@ pub struct ctxt<'tcx> {
617617
// FIXME(eddyb) use a FnvHashSet<InternedTy<'tcx>> when equivalent keys can
618618
// queried from a HashSet.
619619
interner: RefCell<FnvHashMap<InternedTy<'tcx>, Ty<'tcx>>>,
620+
// FIXME as above, use a hashset if equivalent elements can be queried.
621+
substs_interner: RefCell<FnvHashMap<&'tcx Substs<'tcx>, &'tcx Substs<'tcx>>>,
622+
620623
pub sess: Session,
621624
pub def_map: DefMap,
622625

@@ -848,6 +851,8 @@ impl<'tcx> ctxt<'tcx> {
848851
self,
849852
ty_enum, ty_uniq, ty_vec, ty_ptr, ty_rptr, ty_bare_fn, ty_closure, ty_trait,
850853
ty_struct, ty_unboxed_closure, ty_tup, ty_param, ty_open, ty_infer);
854+
855+
println!("Substs interner: #{}", self.substs_interner.borrow().len());
851856
}
852857
}
853858

@@ -2063,6 +2068,7 @@ pub fn mk_ctxt<'tcx>(s: Session,
20632068
type_arena: type_arena,
20642069
substs_arena: substs_arena,
20652070
interner: RefCell::new(FnvHashMap::new()),
2071+
substs_interner: RefCell::new(FnvHashMap::new()),
20662072
named_region_map: named_region_map,
20672073
item_variance_map: RefCell::new(DefIdMap::new()),
20682074
variance_computed: Cell::new(false),
@@ -2123,8 +2129,14 @@ pub fn mk_ctxt<'tcx>(s: Session,
21232129
// Type constructors
21242130

21252131
impl<'tcx> ctxt<'tcx> {
2126-
pub fn mk_substs(&self, subst: Substs<'tcx>) -> &'tcx Substs<'tcx> {
2127-
self.substs_arena.alloc(subst)
2132+
pub fn mk_substs(&self, substs: Substs<'tcx>) -> &'tcx Substs<'tcx> {
2133+
if let Some(substs) = self.substs_interner.borrow().get(&substs) {
2134+
return *substs;
2135+
}
2136+
2137+
let substs = self.substs_arena.alloc(substs);
2138+
self.substs_interner.borrow_mut().insert(substs, substs);
2139+
substs
21282140
}
21292141
}
21302142

0 commit comments

Comments
 (0)