Skip to content

Commit 6c2d875

Browse files
committed
Make &Slice a thin pointer
1 parent a52b01b commit 6c2d875

File tree

5 files changed

+134
-48
lines changed

5 files changed

+134
-48
lines changed

src/libarena/lib.rs

+34-25
Original file line numberDiff line numberDiff line change
@@ -314,17 +314,15 @@ impl DroplessArena {
314314
false
315315
}
316316

317-
fn align_for<T>(&self) {
318-
let align = mem::align_of::<T>();
317+
fn align(&self, align: usize) {
319318
let final_address = ((self.ptr.get() as usize) + align - 1) & !(align - 1);
320319
self.ptr.set(final_address as *mut u8);
321320
assert!(self.ptr <= self.end);
322321
}
323322

324323
#[inline(never)]
325324
#[cold]
326-
fn grow<T>(&self, n: usize) {
327-
let needed_bytes = n * mem::size_of::<T>();
325+
fn grow(&self, needed_bytes: usize) {
328326
unsafe {
329327
let mut chunks = self.chunks.borrow_mut();
330328
let (chunk, mut new_capacity);
@@ -356,25 +354,38 @@ impl DroplessArena {
356354
}
357355

358356
#[inline]
359-
pub fn alloc<T>(&self, object: T) -> &mut T {
357+
pub fn alloc_raw(&self, bytes: usize, align: usize) -> &mut [u8] {
360358
unsafe {
361-
assert!(!mem::needs_drop::<T>());
362-
assert!(mem::size_of::<T>() != 0);
359+
assert!(bytes != 0);
360+
361+
self.align(align);
363362

364-
self.align_for::<T>();
365-
let future_end = intrinsics::arith_offset(self.ptr.get(), mem::size_of::<T>() as isize);
363+
let future_end = intrinsics::arith_offset(self.ptr.get(), bytes as isize);
366364
if (future_end as *mut u8) >= self.end.get() {
367-
self.grow::<T>(1)
365+
self.grow(bytes);
368366
}
369367

370368
let ptr = self.ptr.get();
371369
// Set the pointer past ourselves
372370
self.ptr.set(
373-
intrinsics::arith_offset(self.ptr.get(), mem::size_of::<T>() as isize) as *mut u8,
371+
intrinsics::arith_offset(self.ptr.get(), bytes as isize) as *mut u8,
374372
);
373+
slice::from_raw_parts_mut(ptr, bytes)
374+
}
375+
}
376+
377+
#[inline]
378+
pub fn alloc<T>(&self, object: T) -> &mut T {
379+
assert!(!mem::needs_drop::<T>());
380+
381+
let mem = self.alloc_raw(
382+
mem::size_of::<T>(),
383+
mem::align_of::<T>()) as *mut _ as *mut T;
384+
385+
unsafe {
375386
// Write into uninitialized memory.
376-
ptr::write(ptr as *mut T, object);
377-
&mut *(ptr as *mut T)
387+
ptr::write(mem, object);
388+
&mut *mem
378389
}
379390
}
380391

@@ -393,21 +404,13 @@ impl DroplessArena {
393404
assert!(!mem::needs_drop::<T>());
394405
assert!(mem::size_of::<T>() != 0);
395406
assert!(slice.len() != 0);
396-
self.align_for::<T>();
397407

398-
let future_end = unsafe {
399-
intrinsics::arith_offset(self.ptr.get(), (slice.len() * mem::size_of::<T>()) as isize)
400-
};
401-
if (future_end as *mut u8) >= self.end.get() {
402-
self.grow::<T>(slice.len());
403-
}
408+
let mem = self.alloc_raw(
409+
slice.len() * mem::size_of::<T>(),
410+
mem::align_of::<T>()) as *mut _ as *mut T;
404411

405412
unsafe {
406-
let arena_slice = slice::from_raw_parts_mut(self.ptr.get() as *mut T, slice.len());
407-
self.ptr.set(intrinsics::arith_offset(
408-
self.ptr.get(),
409-
(slice.len() * mem::size_of::<T>()) as isize,
410-
) as *mut u8);
413+
let arena_slice = slice::from_raw_parts_mut(mem, slice.len());
411414
arena_slice.copy_from_slice(slice);
412415
arena_slice
413416
}
@@ -464,6 +467,12 @@ impl SyncDroplessArena {
464467
self.lock.lock().in_arena(ptr)
465468
}
466469

470+
#[inline(always)]
471+
pub fn alloc_raw(&self, bytes: usize, align: usize) -> &mut [u8] {
472+
// Extend the lifetime of the result since it's limited to the lock guard
473+
unsafe { &mut *(self.lock.lock().alloc_raw(bytes, align) as *mut [u8]) }
474+
}
475+
467476
#[inline(always)]
468477
pub fn alloc<T>(&self, object: T) -> &mut T {
469478
// Extend the lifetime of the result since it's limited to the lock guard

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#![feature(macro_vis_matcher)]
5555
#![feature(never_type)]
5656
#![feature(exhaustive_patterns)]
57+
#![feature(extern_types)]
5758
#![feature(non_exhaustive)]
5859
#![feature(proc_macro_internals)]
5960
#![feature(quote)]

src/librustc/ty/context.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -2056,9 +2056,8 @@ for Interned<'tcx, Slice<Goal<'tcx>>> {
20562056

20572057
macro_rules! intern_method {
20582058
($lt_tcx:tt, $name:ident: $method:ident($alloc:ty,
2059-
$alloc_method:ident,
2059+
$alloc_method:expr,
20602060
$alloc_to_key:expr,
2061-
$alloc_to_ret:expr,
20622061
$keep_in_local_tcx:expr) -> $ty:ty) => {
20632062
impl<'a, 'gcx, $lt_tcx> TyCtxt<'a, 'gcx, $lt_tcx> {
20642063
pub fn $method(self, v: $alloc) -> &$lt_tcx $ty {
@@ -2081,7 +2080,7 @@ macro_rules! intern_method {
20812080
v);
20822081
}
20832082

2084-
let i = ($alloc_to_ret)(self.interners.arena.$alloc_method(v));
2083+
let i = $alloc_method(&self.interners.arena, v);
20852084
interner.insert(Interned(i));
20862085
i
20872086
} else {
@@ -2094,7 +2093,9 @@ macro_rules! intern_method {
20942093
let v = unsafe {
20952094
mem::transmute(v)
20962095
};
2097-
let i = ($alloc_to_ret)(self.global_interners.arena.$alloc_method(v));
2096+
let i: &$lt_tcx $ty = $alloc_method(&self.global_interners.arena, v);
2097+
// Cast to 'gcx
2098+
let i = unsafe { mem::transmute(i) };
20982099
interner.insert(Interned(i));
20992100
i
21002101
}
@@ -2121,8 +2122,10 @@ macro_rules! direct_interners {
21212122

21222123
intern_method!(
21232124
$lt_tcx,
2124-
$name: $method($ty, alloc, |x| x, |x| x, $keep_in_local_tcx) -> $ty
2125-
);)+
2125+
$name: $method($ty,
2126+
|a: &$lt_tcx SyncDroplessArena, v| -> &$lt_tcx $ty { a.alloc(v) },
2127+
|x| x,
2128+
$keep_in_local_tcx) -> $ty);)+
21262129
}
21272130
}
21282131

@@ -2137,10 +2140,11 @@ direct_interners!('tcx,
21372140

21382141
macro_rules! slice_interners {
21392142
($($field:ident: $method:ident($ty:ident)),+) => (
2140-
$(intern_method!('tcx, $field: $method(&[$ty<'tcx>], alloc_slice, Deref::deref,
2141-
|xs: &[$ty]| -> &Slice<$ty> {
2142-
unsafe { mem::transmute(xs) }
2143-
}, |xs: &[$ty]| xs.iter().any(keep_local)) -> Slice<$ty<'tcx>>);)+
2143+
$(intern_method!( 'tcx, $field: $method(
2144+
&[$ty<'tcx>],
2145+
|a, v| Slice::from_arena(a, v),
2146+
Deref::deref,
2147+
|xs: &[$ty]| xs.iter().any(keep_local)) -> Slice<$ty<'tcx>>);)+
21442148
)
21452149
}
21462150

@@ -2162,9 +2166,8 @@ intern_method! {
21622166
'tcx,
21632167
canonical_var_infos: _intern_canonical_var_infos(
21642168
&[CanonicalVarInfo],
2165-
alloc_slice,
2169+
|a, v| Slice::from_arena(a, v),
21662170
Deref::deref,
2167-
|xs: &[CanonicalVarInfo]| -> &Slice<CanonicalVarInfo> { unsafe { mem::transmute(xs) } },
21682171
|_xs: &[CanonicalVarInfo]| -> bool { false }
21692172
) -> Slice<CanonicalVarInfo>
21702173
}

src/librustc/ty/mod.rs

+83-10
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ use ty::util::{IntTypeExt, Discr};
3636
use ty::walk::TypeWalker;
3737
use util::captures::Captures;
3838
use util::nodemap::{NodeSet, DefIdMap, FxHashMap};
39+
use arena::SyncDroplessArena;
3940

4041
use serialize::{self, Encodable, Encoder};
4142
use std::cell::RefCell;
4243
use std::cmp::{self, Ordering};
4344
use std::fmt;
4445
use std::hash::{Hash, Hasher};
46+
use std::marker::PhantomData;
4547
use std::ops::Deref;
4648
use rustc_data_structures::sync::Lrc;
4749
use std::slice;
@@ -582,54 +584,120 @@ impl <'gcx: 'tcx, 'tcx> Canonicalize<'gcx, 'tcx> for Ty<'tcx> {
582584
}
583585
}
584586

587+
extern {
588+
/// A dummy type used to force Slice to by unsized without requiring fat pointers
589+
type OpaqueSliceContents;
590+
}
591+
585592
/// A wrapper for slices with the additional invariant
586593
/// that the slice is interned and no other slice with
587594
/// the same contents can exist in the same context.
588595
/// This means we can use pointer + length for both
589596
/// equality comparisons and hashing.
590-
#[derive(Debug, RustcEncodable)]
591-
pub struct Slice<T>([T]);
597+
pub struct Slice<T>(PhantomData<T>, OpaqueSliceContents);
598+
599+
impl<T> Slice<T> {
600+
/// Returns the offset of the array
601+
#[inline(always)]
602+
fn offset() -> usize {
603+
// Align up the size of the len (usize) field
604+
let align = mem::align_of::<T>();
605+
let align_mask = align - 1;
606+
let offset = mem::size_of::<usize>();
607+
(offset + align_mask) & !align_mask
608+
}
609+
}
610+
611+
impl<T: Copy> Slice<T> {
612+
#[inline]
613+
fn from_arena<'tcx>(arena: &'tcx SyncDroplessArena, slice: &[T]) -> &'tcx Slice<T> {
614+
assert!(!mem::needs_drop::<T>());
615+
assert!(mem::size_of::<T>() != 0);
616+
assert!(slice.len() != 0);
617+
618+
let offset = Slice::<T>::offset();
619+
let size = offset + slice.len() * mem::size_of::<T>();
620+
621+
let mem: *mut u8 = arena.alloc_raw(
622+
size,
623+
cmp::max(mem::align_of::<T>(), mem::align_of::<usize>())).as_mut_ptr();
624+
625+
unsafe {
626+
// Write the length
627+
*(mem as *mut usize) = slice.len();
628+
629+
// Write the elements
630+
let arena_slice = slice::from_raw_parts_mut(
631+
mem.offset(offset as isize) as *mut T,
632+
slice.len());
633+
arena_slice.copy_from_slice(slice);
634+
635+
&*(mem as *const Slice<T>)
636+
}
637+
}
638+
}
639+
640+
impl<T: fmt::Debug> fmt::Debug for Slice<T> {
641+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
642+
(**self).fmt(f)
643+
}
644+
}
645+
646+
impl<T: Encodable> Encodable for Slice<T> {
647+
#[inline]
648+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
649+
(**self).encode(s)
650+
}
651+
}
592652

593653
impl<T> Ord for Slice<T> where T: Ord {
594654
fn cmp(&self, other: &Slice<T>) -> Ordering {
595655
if self == other { Ordering::Equal } else {
596-
<[T] as Ord>::cmp(&self.0, &other.0)
656+
<[T] as Ord>::cmp(&**self, &**other)
597657
}
598658
}
599659
}
600660

601661
impl<T> PartialOrd for Slice<T> where T: PartialOrd {
602662
fn partial_cmp(&self, other: &Slice<T>) -> Option<Ordering> {
603663
if self == other { Some(Ordering::Equal) } else {
604-
<[T] as PartialOrd>::partial_cmp(&self.0, &other.0)
664+
<[T] as PartialOrd>::partial_cmp(&**self, &**other)
605665
}
606666
}
607667
}
608668

609-
impl<T> PartialEq for Slice<T> {
669+
impl<T: PartialEq> PartialEq for Slice<T> {
610670
#[inline]
611671
fn eq(&self, other: &Slice<T>) -> bool {
612-
(&self.0 as *const [T]) == (&other.0 as *const [T])
672+
(self as *const _) == (other as *const _)
613673
}
614674
}
615-
impl<T> Eq for Slice<T> {}
675+
impl<T: Eq> Eq for Slice<T> {}
616676

617677
impl<T> Hash for Slice<T> {
678+
#[inline]
618679
fn hash<H: Hasher>(&self, s: &mut H) {
619-
(self.as_ptr(), self.len()).hash(s)
680+
(self as *const Slice<T>).hash(s)
620681
}
621682
}
622683

623684
impl<T> Deref for Slice<T> {
624685
type Target = [T];
686+
#[inline(always)]
625687
fn deref(&self) -> &[T] {
626-
&self.0
688+
unsafe {
689+
let raw = self as *const _ as *const u8;
690+
let len = *(raw as *const usize);
691+
let slice = raw.offset(Slice::<T>::offset() as isize);
692+
slice::from_raw_parts(slice as *const T, len)
693+
}
627694
}
628695
}
629696

630697
impl<'a, T> IntoIterator for &'a Slice<T> {
631698
type Item = &'a T;
632699
type IntoIter = <&'a [T] as IntoIterator>::IntoIter;
700+
#[inline(always)]
633701
fn into_iter(self) -> Self::IntoIter {
634702
self[..].iter()
635703
}
@@ -638,9 +706,14 @@ impl<'a, T> IntoIterator for &'a Slice<T> {
638706
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Slice<Ty<'tcx>> {}
639707

640708
impl<T> Slice<T> {
709+
#[inline(always)]
641710
pub fn empty<'a>() -> &'a Slice<T> {
711+
#[repr(align(64), C)]
712+
struct EmptySlice([u8; 64]);
713+
static EMPTY_SLICE: EmptySlice = EmptySlice([0; 64]);
714+
assert!(mem::align_of::<T>() <= 64);
642715
unsafe {
643-
mem::transmute(slice::from_raw_parts(0x1 as *const T, 0))
716+
&*(&EMPTY_SLICE as *const _ as *const Slice<T>)
644717
}
645718
}
646719
}

src/test/mir-opt/basic_assignment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn main() {
4848
// _2 = move _3;
4949
// StorageDead(_3);
5050
// StorageLive(_4);
51-
// UserAssertTy(Canonical { variables: Slice([]), value: std::option::Option<std::boxed::Box<u32>> }, _4);
51+
// UserAssertTy(Canonical { variables: [], value: std::option::Option<std::boxed::Box<u32>> }, _4);
5252
// _4 = std::option::Option<std::boxed::Box<u32>>::None;
5353
// StorageLive(_5);
5454
// StorageLive(_6);

0 commit comments

Comments
 (0)