Skip to content

Commit ad0f316

Browse files
committed
Make &Slice a thin pointer
1 parent 56e541d commit ad0f316

File tree

5 files changed

+131
-43
lines changed

5 files changed

+131
-43
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
@@ -2124,9 +2124,8 @@ for Interned<'tcx, Slice<Goal<'tcx>>> {
21242124

21252125
macro_rules! intern_method {
21262126
($lt_tcx:tt, $name:ident: $method:ident($alloc:ty,
2127-
$alloc_method:ident,
2127+
$alloc_method:expr,
21282128
$alloc_to_key:expr,
2129-
$alloc_to_ret:expr,
21302129
$keep_in_local_tcx:expr) -> $ty:ty) => {
21312130
impl<'a, 'gcx, $lt_tcx> TyCtxt<'a, 'gcx, $lt_tcx> {
21322131
pub fn $method(self, v: $alloc) -> &$lt_tcx $ty {
@@ -2149,7 +2148,7 @@ macro_rules! intern_method {
21492148
v);
21502149
}
21512150

2152-
let i = ($alloc_to_ret)(self.interners.arena.$alloc_method(v));
2151+
let i = $alloc_method(&self.interners.arena, v);
21532152
interner.insert(Interned(i));
21542153
i
21552154
} else {
@@ -2162,7 +2161,9 @@ macro_rules! intern_method {
21622161
let v = unsafe {
21632162
mem::transmute(v)
21642163
};
2165-
let i = ($alloc_to_ret)(self.global_interners.arena.$alloc_method(v));
2164+
let i: &$lt_tcx $ty = $alloc_method(&self.global_interners.arena, v);
2165+
// Cast to 'gcx
2166+
let i = unsafe { mem::transmute(i) };
21662167
interner.insert(Interned(i));
21672168
i
21682169
}
@@ -2189,8 +2190,10 @@ macro_rules! direct_interners {
21892190

21902191
intern_method!(
21912192
$lt_tcx,
2192-
$name: $method($ty, alloc, |x| x, |x| x, $keep_in_local_tcx) -> $ty
2193-
);)+
2193+
$name: $method($ty,
2194+
|a: &$lt_tcx SyncDroplessArena, v| -> &$lt_tcx $ty { a.alloc(v) },
2195+
|x| x,
2196+
$keep_in_local_tcx) -> $ty);)+
21942197
}
21952198
}
21962199

@@ -2205,10 +2208,11 @@ direct_interners!('tcx,
22052208

22062209
macro_rules! slice_interners {
22072210
($($field:ident: $method:ident($ty:ident)),+) => (
2208-
$(intern_method!('tcx, $field: $method(&[$ty<'tcx>], alloc_slice, Deref::deref,
2209-
|xs: &[$ty]| -> &Slice<$ty> {
2210-
unsafe { mem::transmute(xs) }
2211-
}, |xs: &[$ty]| xs.iter().any(keep_local)) -> Slice<$ty<'tcx>>);)+
2211+
$(intern_method!( 'tcx, $field: $method(
2212+
&[$ty<'tcx>],
2213+
|a, v| Slice::from_arena(a, v),
2214+
Deref::deref,
2215+
|xs: &[$ty]| xs.iter().any(keep_local)) -> Slice<$ty<'tcx>>);)+
22122216
)
22132217
}
22142218

@@ -2230,9 +2234,8 @@ intern_method! {
22302234
'tcx,
22312235
canonical_var_infos: _intern_canonical_var_infos(
22322236
&[CanonicalVarInfo],
2233-
alloc_slice,
2237+
|a, v| Slice::from_arena(a, v),
22342238
Deref::deref,
2235-
|xs: &[CanonicalVarInfo]| -> &Slice<CanonicalVarInfo> { unsafe { mem::transmute(xs) } },
22362239
|_xs: &[CanonicalVarInfo]| -> bool { false }
22372240
) -> Slice<CanonicalVarInfo>
22382241
}

src/librustc/ty/mod.rs

+80-5
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;
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;
@@ -570,38 +572,110 @@ impl <'gcx: 'tcx, 'tcx> Canonicalize<'gcx, 'tcx> for Ty<'tcx> {
570572
}
571573
}
572574

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

581644
impl<T> PartialEq for Slice<T> {
582645
#[inline]
583646
fn eq(&self, other: &Slice<T>) -> bool {
584-
(&self.0 as *const [T]) == (&other.0 as *const [T])
647+
(self as *const _) == (other as *const _)
585648
}
586649
}
587650
impl<T> Eq for Slice<T> {}
588651

589652
impl<T> Hash for Slice<T> {
653+
#[inline]
590654
fn hash<H: Hasher>(&self, s: &mut H) {
591655
(self.as_ptr(), self.len()).hash(s)
592656
}
593657
}
594658

595659
impl<T> Deref for Slice<T> {
596660
type Target = [T];
661+
#[inline(always)]
597662
fn deref(&self) -> &[T] {
598-
&self.0
663+
unsafe {
664+
if self as *const _ as usize == EMPTY_SLICE {
665+
return &[];
666+
}
667+
let raw = self as *const _ as *const u8;
668+
let len = *(raw as *const usize);
669+
let slice = raw.offset(Slice::<T>::offset() as isize);
670+
slice::from_raw_parts(slice as *const T, len)
671+
}
599672
}
600673
}
601674

602675
impl<'a, T> IntoIterator for &'a Slice<T> {
603676
type Item = &'a T;
604677
type IntoIter = <&'a [T] as IntoIterator>::IntoIter;
678+
#[inline(always)]
605679
fn into_iter(self) -> Self::IntoIter {
606680
self[..].iter()
607681
}
@@ -610,9 +684,10 @@ impl<'a, T> IntoIterator for &'a Slice<T> {
610684
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Slice<Ty<'tcx>> {}
611685

612686
impl<T> Slice<T> {
687+
#[inline(always)]
613688
pub fn empty<'a>() -> &'a Slice<T> {
614689
unsafe {
615-
mem::transmute(slice::from_raw_parts(0x1 as *const T, 0))
690+
&*(EMPTY_SLICE as *const _)
616691
}
617692
}
618693
}

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)