Skip to content

Commit 2fbbaf2

Browse files
committed
rustc: Use set recovery APIs in the TyCtxt interners.
1 parent 513d392 commit 2fbbaf2

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

src/librustc/ty/context.rs

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use ty::layout::{Layout, TargetDataLayout};
3535
use ty::maps;
3636
use util::common::MemoizationMap;
3737
use util::nodemap::{NodeMap, NodeSet, DefIdMap, DefIdSet};
38-
use util::nodemap::FnvHashMap;
38+
use util::nodemap::{FnvHashMap, FnvHashSet};
3939

4040
use arena::TypedArena;
4141
use std::borrow::Borrow;
@@ -191,7 +191,7 @@ impl<'a, 'tcx> Tables<'tcx> {
191191

192192
impl<'tcx> CommonTypes<'tcx> {
193193
fn new(arena: &'tcx TypedArena<TyS<'tcx>>,
194-
interner: &RefCell<FnvHashMap<InternedTy<'tcx>, Ty<'tcx>>>)
194+
interner: &RefCell<FnvHashSet<InternedTy<'tcx>>>)
195195
-> CommonTypes<'tcx>
196196
{
197197
let mk = |sty| TyCtxt::intern_ty(arena, interner, sty);
@@ -220,7 +220,8 @@ impl<'tcx> CommonTypes<'tcx> {
220220
/// later on.
221221
#[derive(Copy, Clone)]
222222
pub struct TyCtxt<'a, 'tcx: 'a> {
223-
gcx: &'a GlobalCtxt<'tcx>
223+
gcx: &'a GlobalCtxt<'tcx>,
224+
224225
}
225226

226227
impl<'a, 'tcx> Deref for TyCtxt<'a, 'tcx> {
@@ -236,16 +237,12 @@ pub struct GlobalCtxt<'tcx> {
236237

237238
/// Specifically use a speedy hash algorithm for this hash map, it's used
238239
/// quite often.
239-
// FIXME(eddyb) use a FnvHashSet<InternedTy<'tcx>> when equivalent keys can
240-
// queried from a HashSet.
241-
interner: RefCell<FnvHashMap<InternedTy<'tcx>, Ty<'tcx>>>,
242-
243-
// FIXME as above, use a hashset if equivalent elements can be queried.
244-
substs_interner: RefCell<FnvHashMap<&'tcx Substs<'tcx>, &'tcx Substs<'tcx>>>,
245-
bare_fn_interner: RefCell<FnvHashMap<&'tcx BareFnTy<'tcx>, &'tcx BareFnTy<'tcx>>>,
246-
region_interner: RefCell<FnvHashMap<&'tcx Region, &'tcx Region>>,
247-
stability_interner: RefCell<FnvHashMap<&'tcx attr::Stability, &'tcx attr::Stability>>,
248-
layout_interner: RefCell<FnvHashMap<&'tcx Layout, &'tcx Layout>>,
240+
interner: RefCell<FnvHashSet<InternedTy<'tcx>>>,
241+
substs_interner: RefCell<FnvHashSet<InternedSubsts<'tcx>>>,
242+
bare_fn_interner: RefCell<FnvHashSet<&'tcx BareFnTy<'tcx>>>,
243+
region_interner: RefCell<FnvHashSet<&'tcx Region>>,
244+
stability_interner: RefCell<FnvHashSet<&'tcx attr::Stability>>,
245+
layout_interner: RefCell<FnvHashSet<&'tcx Layout>>,
249246

250247
pub dep_graph: DepGraph,
251248

@@ -518,7 +515,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
518515
let interned = self.arenas.stability.alloc(stab);
519516
if let Some(prev) = self.stability_interner
520517
.borrow_mut()
521-
.insert(interned, interned) {
518+
.replace(interned) {
522519
bug!("Tried to overwrite interned Stability: {:?}", prev)
523520
}
524521
interned
@@ -532,7 +529,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
532529
let interned = self.arenas.layout.alloc(layout);
533530
if let Some(prev) = self.layout_interner
534531
.borrow_mut()
535-
.insert(interned, interned) {
532+
.replace(interned) {
536533
bug!("Tried to overwrite interned Layout: {:?}", prev)
537534
}
538535
interned
@@ -571,18 +568,18 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
571568
where F: for<'b> FnOnce(TyCtxt<'b, 'tcx>) -> R
572569
{
573570
let data_layout = TargetDataLayout::parse(s);
574-
let interner = RefCell::new(FnvHashMap());
571+
let interner = RefCell::new(FnvHashSet());
575572
let common_types = CommonTypes::new(&arenas.type_, &interner);
576573
let dep_graph = map.dep_graph.clone();
577574
let fulfilled_predicates = traits::GlobalFulfilledPredicates::new(dep_graph.clone());
578575
tls::enter(GlobalCtxt {
579576
arenas: arenas,
580577
interner: interner,
581-
substs_interner: RefCell::new(FnvHashMap()),
582-
bare_fn_interner: RefCell::new(FnvHashMap()),
583-
region_interner: RefCell::new(FnvHashMap()),
584-
stability_interner: RefCell::new(FnvHashMap()),
585-
layout_interner: RefCell::new(FnvHashMap()),
578+
substs_interner: RefCell::new(FnvHashSet()),
579+
bare_fn_interner: RefCell::new(FnvHashSet()),
580+
region_interner: RefCell::new(FnvHashSet()),
581+
stability_interner: RefCell::new(FnvHashSet()),
582+
layout_interner: RefCell::new(FnvHashSet()),
586583
dep_graph: dep_graph.clone(),
587584
types: common_types,
588585
named_region_map: named_region_map,
@@ -656,7 +653,7 @@ pub trait Lift<'tcx> {
656653
impl<'a, 'tcx> Lift<'tcx> for Ty<'a> {
657654
type Lifted = Ty<'tcx>;
658655
fn lift_to_tcx<'b>(&self, tcx: TyCtxt<'b, 'tcx>) -> Option<Ty<'tcx>> {
659-
if let Some(&ty) = tcx.interner.borrow().get(&self.sty) {
656+
if let Some(&InternedTy { ty }) = tcx.interner.borrow().get(&self.sty) {
660657
if *self as *const _ == ty as *const _ {
661658
return Some(ty);
662659
}
@@ -668,7 +665,7 @@ impl<'a, 'tcx> Lift<'tcx> for Ty<'a> {
668665
impl<'a, 'tcx> Lift<'tcx> for &'a Substs<'a> {
669666
type Lifted = &'tcx Substs<'tcx>;
670667
fn lift_to_tcx<'b>(&self, tcx: TyCtxt<'b, 'tcx>) -> Option<&'tcx Substs<'tcx>> {
671-
if let Some(&substs) = tcx.substs_interner.borrow().get(*self) {
668+
if let Some(&InternedSubsts { substs }) = tcx.substs_interner.borrow().get(*self) {
672669
if *self as *const _ == substs as *const _ {
673670
return Some(substs);
674671
}
@@ -745,6 +742,8 @@ macro_rules! sty_debug_print {
745742
#[allow(non_snake_case)]
746743
mod inner {
747744
use ty::{self, TyCtxt};
745+
use ty::context::InternedTy;
746+
748747
#[derive(Copy, Clone)]
749748
struct DebugStat {
750749
total: usize,
@@ -761,7 +760,7 @@ macro_rules! sty_debug_print {
761760
$(let mut $variant = total;)*
762761

763762

764-
for (_, t) in tcx.interner.borrow().iter() {
763+
for &InternedTy { ty: t } in tcx.interner.borrow().iter() {
765764
let variant = match t.sty {
766765
ty::TyBool | ty::TyChar | ty::TyInt(..) | ty::TyUint(..) |
767766
ty::TyFloat(..) | ty::TyStr => continue,
@@ -817,7 +816,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
817816

818817

819818
/// An entry in the type interner.
820-
pub struct InternedTy<'tcx> {
819+
struct InternedTy<'tcx> {
821820
ty: Ty<'tcx>
822821
}
823822

@@ -836,12 +835,24 @@ impl<'tcx> Hash for InternedTy<'tcx> {
836835
}
837836
}
838837

839-
impl<'tcx> Borrow<TypeVariants<'tcx>> for InternedTy<'tcx> {
840-
fn borrow<'a>(&'a self) -> &'a TypeVariants<'tcx> {
838+
impl<'tcx: 'lcx, 'lcx> Borrow<TypeVariants<'lcx>> for InternedTy<'tcx> {
839+
fn borrow<'a>(&'a self) -> &'a TypeVariants<'lcx> {
841840
&self.ty.sty
842841
}
843842
}
844843

844+
/// An entry in the substs interner.
845+
#[derive(PartialEq, Eq, Hash)]
846+
struct InternedSubsts<'tcx> {
847+
substs: &'tcx Substs<'tcx>
848+
}
849+
850+
impl<'tcx: 'lcx, 'lcx> Borrow<Substs<'lcx>> for InternedSubsts<'tcx> {
851+
fn borrow<'a>(&'a self) -> &'a Substs<'lcx> {
852+
self.substs
853+
}
854+
}
855+
845856
fn bound_list_is_sorted(bounds: &[ty::PolyProjectionPredicate]) -> bool {
846857
bounds.is_empty() ||
847858
bounds[1..].iter().enumerate().all(
@@ -851,12 +862,14 @@ fn bound_list_is_sorted(bounds: &[ty::PolyProjectionPredicate]) -> bool {
851862
impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
852863
// Type constructors
853864
pub fn mk_substs(self, substs: Substs<'tcx>) -> &'tcx Substs<'tcx> {
854-
if let Some(substs) = self.substs_interner.borrow().get(&substs) {
855-
return *substs;
865+
if let Some(interned) = self.substs_interner.borrow().get(&substs) {
866+
return interned.substs;
856867
}
857868

858869
let substs = self.arenas.substs.alloc(substs);
859-
self.substs_interner.borrow_mut().insert(substs, substs);
870+
self.substs_interner.borrow_mut().insert(InternedSubsts {
871+
substs: substs
872+
});
860873
substs
861874
}
862875

@@ -876,7 +889,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
876889
}
877890

878891
let bare_fn = self.arenas.bare_fn.alloc(bare_fn);
879-
self.bare_fn_interner.borrow_mut().insert(bare_fn, bare_fn);
892+
self.bare_fn_interner.borrow_mut().insert(bare_fn);
880893
bare_fn
881894
}
882895

@@ -886,19 +899,19 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
886899
}
887900

888901
let region = self.arenas.region.alloc(region);
889-
self.region_interner.borrow_mut().insert(region, region);
902+
self.region_interner.borrow_mut().insert(region);
890903
region
891904
}
892905

893906
fn intern_ty(type_arena: &'tcx TypedArena<TyS<'tcx>>,
894-
interner: &RefCell<FnvHashMap<InternedTy<'tcx>, Ty<'tcx>>>,
907+
interner: &RefCell<FnvHashSet<InternedTy<'tcx>>>,
895908
st: TypeVariants<'tcx>)
896909
-> Ty<'tcx> {
897910
let ty: Ty /* don't be &mut TyS */ = {
898911
let mut interner = interner.borrow_mut();
899912
match interner.get(&st) {
900-
Some(ty) => return *ty,
901-
_ => ()
913+
Some(&InternedTy { ty }) => return ty,
914+
None => ()
902915
}
903916

904917
let flags = super::flags::FlagComputation::for_sty(&st);
@@ -909,7 +922,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx> {
909922
region_depth: flags.depth, }),
910923
};
911924

912-
interner.insert(InternedTy { ty: ty }, ty);
925+
interner.insert(InternedTy { ty: ty });
913926
ty
914927
};
915928

0 commit comments

Comments
 (0)