Skip to content

Commit 1676bec

Browse files
Utilize new in_arena method instead of hashing for Lifting interned items
1 parent 0e43b37 commit 1676bec

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

src/libarena/lib.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,17 @@ impl DroplessArena {
301301
}
302302
}
303303

304+
pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
305+
let ptr = ptr as *const u8 as *mut u8;
306+
for chunk in &*self.chunks.borrow() {
307+
if chunk.start() <= ptr && ptr < chunk.end() {
308+
return true;
309+
}
310+
}
311+
312+
false
313+
}
314+
304315
fn align_for<T>(&self) {
305316
let align = mem::align_of::<T>();
306317
let final_address = ((self.ptr.get() as usize) + align - 1) & !(align - 1);
@@ -330,12 +341,11 @@ impl DroplessArena {
330341
}
331342
}
332343
} else {
333-
new_capacity = needed_bytes;
344+
new_capacity = cmp::max(needed_bytes, PAGE);
334345
}
335346
chunk = TypedArenaChunk::<u8>::new(new_capacity);
336347
self.ptr.set(chunk.start());
337348
self.end.set(chunk.end());
338-
self.align_for::<T>();
339349
chunks.push(chunk);
340350
}
341351
}

src/librustc/ty/context.rs

+13-25
Original file line numberDiff line numberDiff line change
@@ -817,10 +817,8 @@ pub trait Lift<'tcx> {
817817
impl<'a, 'tcx> Lift<'tcx> for Ty<'a> {
818818
type Lifted = Ty<'tcx>;
819819
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Ty<'tcx>> {
820-
if let Some(&Interned(ty)) = tcx.interners.type_.borrow().get(&self.sty) {
821-
if *self as *const _ == ty as *const _ {
822-
return Some(ty);
823-
}
820+
if tcx.interners.arena.in_arena(*self as *const _) {
821+
return Some(unsafe { mem::transmute(*self) });
824822
}
825823
// Also try in the global tcx if we're not that.
826824
if !tcx.is_global() {
@@ -837,10 +835,8 @@ impl<'a, 'tcx> Lift<'tcx> for &'a Substs<'a> {
837835
if self.len() == 0 {
838836
return Some(Slice::empty());
839837
}
840-
if let Some(&Interned(substs)) = tcx.interners.substs.borrow().get(&self[..]) {
841-
if *self as *const _ == substs as *const _ {
842-
return Some(substs);
843-
}
838+
if tcx.interners.arena.in_arena(&self[..] as *const _) {
839+
return Some(unsafe { mem::transmute(*self) });
844840
}
845841
// Also try in the global tcx if we're not that.
846842
if !tcx.is_global() {
@@ -854,10 +850,8 @@ impl<'a, 'tcx> Lift<'tcx> for &'a Substs<'a> {
854850
impl<'a, 'tcx> Lift<'tcx> for &'a Region {
855851
type Lifted = &'tcx Region;
856852
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<&'tcx Region> {
857-
if let Some(&Interned(region)) = tcx.interners.region.borrow().get(*self) {
858-
if *self as *const _ == region as *const _ {
859-
return Some(region);
860-
}
853+
if tcx.interners.arena.in_arena(*self as *const _) {
854+
return Some(unsafe { mem::transmute(*self) });
861855
}
862856
// Also try in the global tcx if we're not that.
863857
if !tcx.is_global() {
@@ -875,10 +869,8 @@ impl<'a, 'tcx> Lift<'tcx> for &'a Slice<Ty<'a>> {
875869
if self.len() == 0 {
876870
return Some(Slice::empty());
877871
}
878-
if let Some(&Interned(list)) = tcx.interners.type_list.borrow().get(&self[..]) {
879-
if *self as *const _ == list as *const _ {
880-
return Some(list);
881-
}
872+
if tcx.interners.arena.in_arena(*self as *const _) {
873+
return Some(unsafe { mem::transmute(*self) });
882874
}
883875
// Also try in the global tcx if we're not that.
884876
if !tcx.is_global() {
@@ -896,10 +888,8 @@ impl<'a, 'tcx> Lift<'tcx> for &'a Slice<ExistentialPredicate<'a>> {
896888
if self.is_empty() {
897889
return Some(Slice::empty());
898890
}
899-
if let Some(&Interned(eps)) = tcx.interners.existential_predicates.borrow().get(&self[..]) {
900-
if *self as *const _ == eps as *const _ {
901-
return Some(eps);
902-
}
891+
if tcx.interners.arena.in_arena(*self as *const _) {
892+
return Some(unsafe { mem::transmute(*self) });
903893
}
904894
// Also try in the global tcx if we're not that.
905895
if !tcx.is_global() {
@@ -914,10 +904,8 @@ impl<'a, 'tcx> Lift<'tcx> for &'a BareFnTy<'a> {
914904
type Lifted = &'tcx BareFnTy<'tcx>;
915905
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
916906
-> Option<&'tcx BareFnTy<'tcx>> {
917-
if let Some(&Interned(fty)) = tcx.interners.bare_fn.borrow().get(*self) {
918-
if *self as *const _ == fty as *const _ {
919-
return Some(fty);
920-
}
907+
if tcx.interners.arena.in_arena(*self as *const _) {
908+
return Some(unsafe { mem::transmute(*self) });
921909
}
922910
// Also try in the global tcx if we're not that.
923911
if !tcx.is_global() {
@@ -1201,7 +1189,7 @@ macro_rules! intern_method {
12011189
}
12021190
}
12031191

1204-
let i = ($alloc_to_ret)(self.global_interners.arena.$alloc_method(v));
1192+
let i = ($alloc_to_ret)(self.interners.arena.$alloc_method(v));
12051193
self.interners.$name.borrow_mut().insert(Interned(i));
12061194
i
12071195
}

0 commit comments

Comments
 (0)