Skip to content

Commit

Permalink
Auto merge of rust-lang#126793 - saethlin:mono-rawvec, r=<try>
Browse files Browse the repository at this point in the history
Apply "polymorphization at home" to RawVec

The idea here is to move all the logic in RawVec into functions with explicit size and alignment parameters. This should eliminate all the fussing about how tweaking RawVec code produces large swings in compile times.

This uncovered rust-lang/rust-clippy#12979, so I've modified the relevant test in a way that tries to preserve the spirit of the test without tripping the ICE.
  • Loading branch information
bors committed Jul 15, 2024
2 parents 24d2ac0 + d6bb817 commit 011f462
Show file tree
Hide file tree
Showing 9 changed files with 443 additions and 294 deletions.
545 changes: 343 additions & 202 deletions library/alloc/src/raw_vec.rs

Large diffs are not rendered by default.

27 changes: 8 additions & 19 deletions library/alloc/src/raw_vec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ fn allocator_param() {

let a = BoundedAlloc { fuel: Cell::new(500) };
let mut v: RawVec<u8, _> = RawVec::with_capacity_in(50, a);
assert_eq!(v.alloc.fuel.get(), 450);
assert_eq!(v.inner.alloc.fuel.get(), 450);
v.reserve(50, 150); // (causes a realloc, thus using 50 + 150 = 200 units of fuel)
assert_eq!(v.alloc.fuel.get(), 250);
assert_eq!(v.inner.alloc.fuel.get(), 250);
}

#[test]
Expand Down Expand Up @@ -85,7 +85,7 @@ struct ZST;
fn zst_sanity<T>(v: &RawVec<T>) {
assert_eq!(v.capacity(), usize::MAX);
assert_eq!(v.ptr(), core::ptr::Unique::<T>::dangling().as_ptr());
assert_eq!(v.current_memory(), None);
assert_eq!(v.inner.current_memory(T::LAYOUT), None);
}

#[test]
Expand All @@ -105,22 +105,11 @@ fn zst() {
let v: RawVec<ZST> = RawVec::with_capacity_in(100, Global);
zst_sanity(&v);

let v: RawVec<ZST> = RawVec::try_allocate_in(0, AllocInit::Uninitialized, Global).unwrap();
zst_sanity(&v);

let v: RawVec<ZST> = RawVec::try_allocate_in(100, AllocInit::Uninitialized, Global).unwrap();
zst_sanity(&v);

let mut v: RawVec<ZST> =
RawVec::try_allocate_in(usize::MAX, AllocInit::Uninitialized, Global).unwrap();
let mut v: RawVec<ZST> = RawVec::with_capacity_in(usize::MAX, Global);
zst_sanity(&v);

// Check all these operations work as expected with zero-sized elements.

assert!(!v.needs_to_grow(100, usize::MAX - 100));
assert!(v.needs_to_grow(101, usize::MAX - 100));
zst_sanity(&v);

v.reserve(100, usize::MAX - 100);
//v.reserve(101, usize::MAX - 100); // panics, in `zst_reserve_panic` below
zst_sanity(&v);
Expand All @@ -137,12 +126,12 @@ fn zst() {
assert_eq!(v.try_reserve_exact(101, usize::MAX - 100), cap_err);
zst_sanity(&v);

assert_eq!(v.grow_amortized(100, usize::MAX - 100), cap_err);
assert_eq!(v.grow_amortized(101, usize::MAX - 100), cap_err);
assert_eq!(v.inner.grow_amortized(100, usize::MAX - 100, ZST::LAYOUT), cap_err);
assert_eq!(v.inner.grow_amortized(101, usize::MAX - 100, ZST::LAYOUT), cap_err);
zst_sanity(&v);

assert_eq!(v.grow_exact(100, usize::MAX - 100), cap_err);
assert_eq!(v.grow_exact(101, usize::MAX - 100), cap_err);
assert_eq!(v.inner.grow_exact(100, usize::MAX - 100, ZST::LAYOUT), cap_err);
assert_eq!(v.inner.grow_exact(101, usize::MAX - 100, ZST::LAYOUT), cap_err);
zst_sanity(&v);
}

Expand Down
5 changes: 5 additions & 0 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![stable(feature = "rust1", since = "1.0.0")]

use crate::alloc::Layout;
use crate::clone;
use crate::cmp;
use crate::fmt;
Expand Down Expand Up @@ -1235,6 +1236,10 @@ pub trait SizedTypeProperties: Sized {
#[doc(hidden)]
#[unstable(feature = "sized_type_properties", issue = "none")]
const IS_ZST: bool = size_of::<Self>() == 0;

#[doc(hidden)]
#[unstable(feature = "sized_type_properties", issue = "none")]
const LAYOUT: Layout = Layout::new::<Self>();
}
#[doc(hidden)]
#[unstable(feature = "sized_type_properties", issue = "none")]
Expand Down
17 changes: 12 additions & 5 deletions src/etc/gdb_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(self, valobj):
self._valobj = valobj
vec = valobj["vec"]
self._length = int(vec["len"])
self._data_ptr = unwrap_unique_or_non_null(vec["buf"]["ptr"])
self._data_ptr = unwrap_unique_or_non_null(vec["buf"]["inner"]["ptr"])

def to_string(self):
return self._data_ptr.lazy_string(encoding="utf-8", length=self._length)
Expand All @@ -74,7 +74,7 @@ def __init__(self, valobj):
vec = buf[ZERO_FIELD] if is_windows else buf

self._length = int(vec["len"])
self._data_ptr = unwrap_unique_or_non_null(vec["buf"]["ptr"])
self._data_ptr = unwrap_unique_or_non_null(vec["buf"]["inner"]["ptr"])

def to_string(self):
return self._data_ptr.lazy_string(encoding="utf-8", length=self._length)
Expand All @@ -96,6 +96,7 @@ def to_string(self):
def display_hint():
return "string"


def _enumerate_array_elements(element_ptrs):
for (i, element_ptr) in enumerate(element_ptrs):
key = "[{}]".format(i)
Expand All @@ -112,6 +113,7 @@ def _enumerate_array_elements(element_ptrs):

yield key, element


class StdSliceProvider(printer_base):
def __init__(self, valobj):
self._valobj = valobj
Expand All @@ -130,11 +132,14 @@ def children(self):
def display_hint():
return "array"


class StdVecProvider(printer_base):
def __init__(self, valobj):
self._valobj = valobj
self._length = int(valobj["len"])
self._data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"])
self._data_ptr = unwrap_unique_or_non_null(valobj["buf"]["inner"]["ptr"])
ptr_ty = gdb.Type.pointer(valobj.type.template_argument(0))
self._data_ptr = self._data_ptr.reinterpret_cast(ptr_ty)

def to_string(self):
return "Vec(size={})".format(self._length)
Expand All @@ -155,11 +160,13 @@ def __init__(self, valobj):
self._head = int(valobj["head"])
self._size = int(valobj["len"])
# BACKCOMPAT: rust 1.75
cap = valobj["buf"]["cap"]
cap = valobj["buf"]["inner"]["cap"]
if cap.type.code != gdb.TYPE_CODE_INT:
cap = cap[ZERO_FIELD]
self._cap = int(cap)
self._data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"])
self._data_ptr = unwrap_unique_or_non_null(valobj["buf"]["inner"]["ptr"])
ptr_ty = gdb.Type.pointer(valobj.type.template_argument(0))
self._data_ptr = self._data_ptr.reinterpret_cast(ptr_ty)

def to_string(self):
return "VecDeque(size={})".format(self._size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::Once;

const ATOMIC: AtomicUsize = AtomicUsize::new(5);
const CELL: Cell<usize> = Cell::new(6);
const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
const ATOMIC_TUPLE: ([AtomicUsize; 1], Option<Box<AtomicUsize>>, u8) = ([ATOMIC], None, 7);
const INTEGER: u8 = 8;
const STRING: String = String::new();
const STR: &str = "012345";
Expand Down Expand Up @@ -74,7 +74,6 @@ fn main() {
let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR: interior mutability
let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR: interior mutability
let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR: interior mutability
let _ = &*ATOMIC_TUPLE.1;
let _ = &ATOMIC_TUPLE.2;
let _ = (&&&&ATOMIC_TUPLE).0;
let _ = (&&&&ATOMIC_TUPLE).2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,23 @@ LL | let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst);
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:82:13
--> tests/ui/borrow_interior_mutable_const/others.rs:81:13
|
LL | let _ = ATOMIC_TUPLE.0[0];
| ^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:87:5
--> tests/ui/borrow_interior_mutable_const/others.rs:86:5
|
LL | CELL.set(2);
| ^^^^
|
= help: assign this const to a local or static variable, and use the variable here

error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:88:16
--> tests/ui/borrow_interior_mutable_const/others.rs:87:16
|
LL | assert_eq!(CELL.get(), 6);
| ^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/debuginfo/strings-and-strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// gdb-command:run

// gdb-command:print plain_string
// gdbr-check:$1 = alloc::string::String {vec: alloc::vec::Vec<u8, alloc::alloc::Global> {buf: alloc::raw_vec::RawVec<u8, alloc::alloc::Global> {ptr: core::ptr::unique::Unique<u8> {pointer: core::ptr::non_null::NonNull<u8> {pointer: 0x[...]}, _marker: core::marker::PhantomData<u8>}, cap: alloc::raw_vec::Cap (5), alloc: alloc::alloc::Global}, len: 5}}
// gdbr-check:$1 = alloc::string::String {vec: alloc::vec::Vec<u8, alloc::alloc::Global> {buf: alloc::raw_vec::RawVec<u8, alloc::alloc::Global> {inner: alloc::raw_vec::RawVecInner<alloc::alloc::Global> {ptr: core::ptr::unique::Unique<u8> {pointer: core::ptr::non_null::NonNull<u8> {pointer: 0x[...]}, _marker: core::marker::PhantomData<u8>}, cap: alloc::raw_vec::Cap (5), alloc: alloc::alloc::Global}, _marker: core::marker::PhantomData<u8>}, len: 5}}

// gdb-command:print plain_str
// gdbr-check:$2 = "Hello"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,65 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
let mut _0: &[u8];
scope 1 (inlined <Vec<u8> as Deref>::deref) {
debug self => _1;
let mut _4: *const u8;
let mut _5: usize;
let mut _6: *const u8;
let mut _7: usize;
scope 2 (inlined Vec::<u8>::as_ptr) {
debug self => _1;
let mut _2: &alloc::raw_vec::RawVec<u8>;
let mut _5: *mut u8;
scope 3 (inlined alloc::raw_vec::RawVec::<u8>::ptr) {
debug self => _2;
let mut _3: std::ptr::NonNull<u8>;
scope 4 (inlined Unique::<u8>::as_ptr) {
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _3;
debug ((self: Unique<u8>).1: std::marker::PhantomData<u8>) => const PhantomData::<u8>;
scope 5 (inlined NonNull::<u8>::as_ptr) {
debug self => _3;
}
let mut _3: &alloc::raw_vec::RawVecInner;
scope 4 (inlined alloc::raw_vec::RawVecInner::ptr::<u8>) {
debug self => _3;
let mut _4: std::ptr::Unique<u8>;
}
}
}
scope 6 (inlined std::slice::from_raw_parts::<'_, u8>) {
debug data => _4;
debug len => _5;
let _6: *const [u8];
scope 7 (inlined core::ub_checks::check_language_ub) {
scope 8 (inlined core::ub_checks::check_language_ub::runtime) {
scope 5 (inlined std::slice::from_raw_parts::<'_, u8>) {
debug data => _6;
debug len => _7;
let _8: *const [u8];
scope 6 (inlined core::ub_checks::check_language_ub) {
scope 7 (inlined core::ub_checks::check_language_ub::runtime) {
}
}
scope 9 (inlined std::mem::size_of::<u8>) {
scope 8 (inlined std::mem::size_of::<u8>) {
}
scope 10 (inlined align_of::<u8>) {
scope 9 (inlined align_of::<u8>) {
}
scope 11 (inlined slice_from_raw_parts::<u8>) {
debug data => _4;
debug len => _5;
scope 12 (inlined std::ptr::from_raw_parts::<[u8], u8>) {
debug data_pointer => _4;
debug metadata => _5;
scope 10 (inlined slice_from_raw_parts::<u8>) {
debug data => _6;
debug len => _7;
scope 11 (inlined std::ptr::from_raw_parts::<[u8], u8>) {
debug data_pointer => _6;
debug metadata => _7;
}
}
}
}

bb0: {
StorageLive(_4);
StorageLive(_5);
StorageLive(_6);
StorageLive(_2);
_2 = &((*_1).0: alloc::raw_vec::RawVec<u8>);
StorageLive(_3);
_3 = ((((*_1).0: alloc::raw_vec::RawVec<u8>).0: std::ptr::Unique<u8>).0: std::ptr::NonNull<u8>);
_4 = (_3.0: *const u8);
_3 = &(((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner);
StorageLive(_4);
_4 = ((((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner).0: std::ptr::Unique<u8>);
_5 = move _4 as *mut u8 (Transmute);
StorageDead(_4);
StorageDead(_3);
_6 = _5 as *const u8 (PtrToPtr);
StorageDead(_2);
StorageLive(_5);
_5 = ((*_1).1: usize);
_6 = *const [u8] from (_4, _5);
StorageLive(_7);
_7 = ((*_1).1: usize);
_8 = *const [u8] from (_6, _7);
StorageDead(_7);
StorageDead(_6);
StorageDead(_5);
StorageDead(_4);
_0 = &(*_6);
_0 = &(*_8);
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,65 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
let mut _0: &[u8];
scope 1 (inlined <Vec<u8> as Deref>::deref) {
debug self => _1;
let mut _4: *const u8;
let mut _5: usize;
let mut _6: *const u8;
let mut _7: usize;
scope 2 (inlined Vec::<u8>::as_ptr) {
debug self => _1;
let mut _2: &alloc::raw_vec::RawVec<u8>;
let mut _5: *mut u8;
scope 3 (inlined alloc::raw_vec::RawVec::<u8>::ptr) {
debug self => _2;
let mut _3: std::ptr::NonNull<u8>;
scope 4 (inlined Unique::<u8>::as_ptr) {
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _3;
debug ((self: Unique<u8>).1: std::marker::PhantomData<u8>) => const PhantomData::<u8>;
scope 5 (inlined NonNull::<u8>::as_ptr) {
debug self => _3;
}
let mut _3: &alloc::raw_vec::RawVecInner;
scope 4 (inlined alloc::raw_vec::RawVecInner::ptr::<u8>) {
debug self => _3;
let mut _4: std::ptr::Unique<u8>;
}
}
}
scope 6 (inlined std::slice::from_raw_parts::<'_, u8>) {
debug data => _4;
debug len => _5;
let _6: *const [u8];
scope 7 (inlined core::ub_checks::check_language_ub) {
scope 8 (inlined core::ub_checks::check_language_ub::runtime) {
scope 5 (inlined std::slice::from_raw_parts::<'_, u8>) {
debug data => _6;
debug len => _7;
let _8: *const [u8];
scope 6 (inlined core::ub_checks::check_language_ub) {
scope 7 (inlined core::ub_checks::check_language_ub::runtime) {
}
}
scope 9 (inlined std::mem::size_of::<u8>) {
scope 8 (inlined std::mem::size_of::<u8>) {
}
scope 10 (inlined align_of::<u8>) {
scope 9 (inlined align_of::<u8>) {
}
scope 11 (inlined slice_from_raw_parts::<u8>) {
debug data => _4;
debug len => _5;
scope 12 (inlined std::ptr::from_raw_parts::<[u8], u8>) {
debug data_pointer => _4;
debug metadata => _5;
scope 10 (inlined slice_from_raw_parts::<u8>) {
debug data => _6;
debug len => _7;
scope 11 (inlined std::ptr::from_raw_parts::<[u8], u8>) {
debug data_pointer => _6;
debug metadata => _7;
}
}
}
}

bb0: {
StorageLive(_4);
StorageLive(_5);
StorageLive(_6);
StorageLive(_2);
_2 = &((*_1).0: alloc::raw_vec::RawVec<u8>);
StorageLive(_3);
_3 = ((((*_1).0: alloc::raw_vec::RawVec<u8>).0: std::ptr::Unique<u8>).0: std::ptr::NonNull<u8>);
_4 = (_3.0: *const u8);
_3 = &(((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner);
StorageLive(_4);
_4 = ((((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner).0: std::ptr::Unique<u8>);
_5 = move _4 as *mut u8 (Transmute);
StorageDead(_4);
StorageDead(_3);
_6 = _5 as *const u8 (PtrToPtr);
StorageDead(_2);
StorageLive(_5);
_5 = ((*_1).1: usize);
_6 = *const [u8] from (_4, _5);
StorageLive(_7);
_7 = ((*_1).1: usize);
_8 = *const [u8] from (_6, _7);
StorageDead(_7);
StorageDead(_6);
StorageDead(_5);
StorageDead(_4);
_0 = &(*_6);
_0 = &(*_8);
return;
}
}

0 comments on commit 011f462

Please sign in to comment.