Skip to content

Commit edee9c3

Browse files
committedJan 11, 2020
Lift using interners instead of in_arena
1 parent 4b19c80 commit edee9c3

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed
 

‎src/librustc/ty/context.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use rustc_hir::{ItemKind, ItemLocalId, ItemLocalMap, ItemLocalSet};
5353
use arena::SyncDroplessArena;
5454
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5555
use rustc_data_structures::profiling::SelfProfilerRef;
56-
use rustc_data_structures::sharded::ShardedHashMap;
56+
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
5757
use rustc_data_structures::stable_hasher::{
5858
hash_stable_hashmap, HashStable, StableHasher, StableVec,
5959
};
@@ -1560,11 +1560,11 @@ pub trait Lift<'tcx>: fmt::Debug {
15601560
}
15611561

15621562
macro_rules! nop_lift {
1563-
($ty:ty => $lifted:ty) => {
1563+
($set:ident; $ty:ty => $lifted:ty) => {
15641564
impl<'a, 'tcx> Lift<'tcx> for $ty {
15651565
type Lifted = $lifted;
15661566
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
1567-
if tcx.interners.arena.in_arena(*self as *const _) {
1567+
if tcx.interners.$set.contains_pointer_to(&Interned(*self)) {
15681568
Some(unsafe { mem::transmute(*self) })
15691569
} else {
15701570
None
@@ -1575,14 +1575,14 @@ macro_rules! nop_lift {
15751575
}
15761576

15771577
macro_rules! nop_list_lift {
1578-
($ty:ty => $lifted:ty) => {
1578+
($set:ident; $ty:ty => $lifted:ty) => {
15791579
impl<'a, 'tcx> Lift<'tcx> for &'a List<$ty> {
15801580
type Lifted = &'tcx List<$lifted>;
15811581
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
15821582
if self.is_empty() {
15831583
return Some(List::empty());
15841584
}
1585-
if tcx.interners.arena.in_arena(*self as *const _) {
1585+
if tcx.interners.$set.contains_pointer_to(&Interned(*self)) {
15861586
Some(unsafe { mem::transmute(*self) })
15871587
} else {
15881588
None
@@ -1592,21 +1592,21 @@ macro_rules! nop_list_lift {
15921592
};
15931593
}
15941594

1595-
nop_lift! {Ty<'a> => Ty<'tcx>}
1596-
nop_lift! {Region<'a> => Region<'tcx>}
1597-
nop_lift! {Goal<'a> => Goal<'tcx>}
1598-
nop_lift! {&'a Const<'a> => &'tcx Const<'tcx>}
1595+
nop_lift! {type_; Ty<'a> => Ty<'tcx>}
1596+
nop_lift! {region; Region<'a> => Region<'tcx>}
1597+
nop_lift! {goal; Goal<'a> => Goal<'tcx>}
1598+
nop_lift! {const_; &'a Const<'a> => &'tcx Const<'tcx>}
15991599

1600-
nop_list_lift! {Goal<'a> => Goal<'tcx>}
1601-
nop_list_lift! {Clause<'a> => Clause<'tcx>}
1602-
nop_list_lift! {Ty<'a> => Ty<'tcx>}
1603-
nop_list_lift! {ExistentialPredicate<'a> => ExistentialPredicate<'tcx>}
1604-
nop_list_lift! {Predicate<'a> => Predicate<'tcx>}
1605-
nop_list_lift! {CanonicalVarInfo => CanonicalVarInfo}
1606-
nop_list_lift! {ProjectionKind => ProjectionKind}
1600+
nop_list_lift! {goal_list; Goal<'a> => Goal<'tcx>}
1601+
nop_list_lift! {clauses; Clause<'a> => Clause<'tcx>}
1602+
nop_list_lift! {type_list; Ty<'a> => Ty<'tcx>}
1603+
nop_list_lift! {existential_predicates; ExistentialPredicate<'a> => ExistentialPredicate<'tcx>}
1604+
nop_list_lift! {predicates; Predicate<'a> => Predicate<'tcx>}
1605+
nop_list_lift! {canonical_var_infos; CanonicalVarInfo => CanonicalVarInfo}
1606+
nop_list_lift! {projs; ProjectionKind => ProjectionKind}
16071607

16081608
// This is the impl for `&'a InternalSubsts<'a>`.
1609-
nop_list_lift! {GenericArg<'a> => GenericArg<'tcx>}
1609+
nop_list_lift! {substs; GenericArg<'a> => GenericArg<'tcx>}
16101610

16111611
pub mod tls {
16121612
use super::{ptr_eq, GlobalCtxt, TyCtxt};
@@ -1930,6 +1930,11 @@ impl<'tcx, T: 'tcx + ?Sized> Clone for Interned<'tcx, T> {
19301930
}
19311931
impl<'tcx, T: 'tcx + ?Sized> Copy for Interned<'tcx, T> {}
19321932

1933+
impl<'tcx, T: 'tcx + ?Sized> IntoPointer for Interned<'tcx, T> {
1934+
fn into_pointer(&self) -> *const () {
1935+
self.0 as *const _ as *const ()
1936+
}
1937+
}
19331938
// N.B., an `Interned<Ty>` compares and hashes as a `TyKind`.
19341939
impl<'tcx> PartialEq for Interned<'tcx, TyS<'tcx>> {
19351940
fn eq(&self, other: &Interned<'tcx, TyS<'tcx>>) -> bool {

‎src/librustc_data_structures/sharded.rs

+14
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ impl<K: Eq + Hash + Copy> ShardedHashMap<K, ()> {
137137
}
138138
}
139139

140+
pub trait IntoPointer {
141+
/// Returns a pointer which outlives `self`.
142+
fn into_pointer(&self) -> *const ();
143+
}
144+
145+
impl<K: Eq + Hash + Copy + IntoPointer> ShardedHashMap<K, ()> {
146+
pub fn contains_pointer_to<T: Hash + IntoPointer>(&self, value: &T) -> bool {
147+
let hash = make_hash(&value);
148+
let shard = self.get_shard_by_hash(hash).lock();
149+
let value = value.into_pointer();
150+
shard.raw_entry().from_hash(hash, |entry| entry.into_pointer() == value).is_some()
151+
}
152+
}
153+
140154
#[inline]
141155
fn make_hash<K: Hash + ?Sized>(val: &K) -> u64 {
142156
let mut state = FxHasher::default();

0 commit comments

Comments
 (0)
Please sign in to comment.