Skip to content

Commit 75d937c

Browse files
committed
Auto merge of #54668 - RalfJung:use-maybe-uninit, r=SimonSapin
Use MaybeUninit in libcore All code by @japaric. This re-submits the second half of #53508 (the first half is at #54667). This is likely the one containing the perf regression.
2 parents 6bfb46e + 59786b0 commit 75d937c

File tree

5 files changed

+53
-52
lines changed

5 files changed

+53
-52
lines changed

src/libcore/fmt/float.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use fmt::{Formatter, Result, LowerExp, UpperExp, Display, Debug};
12-
use mem;
12+
use mem::MaybeUninit;
1313
use num::flt2dec;
1414

1515
// Don't inline this so callers don't use the stack space this function
@@ -20,11 +20,11 @@ fn float_to_decimal_common_exact<T>(fmt: &mut Formatter, num: &T,
2020
where T: flt2dec::DecodableFloat
2121
{
2222
unsafe {
23-
let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64
24-
let mut parts: [flt2dec::Part; 4] = mem::uninitialized();
23+
let mut buf = MaybeUninit::<[u8; 1024]>::uninitialized(); // enough for f32 and f64
24+
let mut parts = MaybeUninit::<[flt2dec::Part; 4]>::uninitialized();
2525
let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
2626
*num, sign, precision,
27-
false, &mut buf, &mut parts);
27+
false, buf.get_mut(), parts.get_mut());
2828
fmt.pad_formatted_parts(&formatted)
2929
}
3030
}
@@ -38,10 +38,11 @@ fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter, num: &T,
3838
{
3939
unsafe {
4040
// enough for f32 and f64
41-
let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
42-
let mut parts: [flt2dec::Part; 4] = mem::uninitialized();
41+
let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninitialized();
42+
let mut parts = MaybeUninit::<[flt2dec::Part; 4]>::uninitialized();
4343
let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest, *num,
44-
sign, precision, false, &mut buf, &mut parts);
44+
sign, precision, false, buf.get_mut(),
45+
parts.get_mut());
4546
fmt.pad_formatted_parts(&formatted)
4647
}
4748
}
@@ -75,11 +76,11 @@ fn float_to_exponential_common_exact<T>(fmt: &mut Formatter, num: &T,
7576
where T: flt2dec::DecodableFloat
7677
{
7778
unsafe {
78-
let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64
79-
let mut parts: [flt2dec::Part; 6] = mem::uninitialized();
79+
let mut buf = MaybeUninit::<[u8; 1024]>::uninitialized(); // enough for f32 and f64
80+
let mut parts = MaybeUninit::<[flt2dec::Part; 6]>::uninitialized();
8081
let formatted = flt2dec::to_exact_exp_str(flt2dec::strategy::grisu::format_exact,
8182
*num, sign, precision,
82-
upper, &mut buf, &mut parts);
83+
upper, buf.get_mut(), parts.get_mut());
8384
fmt.pad_formatted_parts(&formatted)
8485
}
8586
}
@@ -94,11 +95,11 @@ fn float_to_exponential_common_shortest<T>(fmt: &mut Formatter,
9495
{
9596
unsafe {
9697
// enough for f32 and f64
97-
let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
98-
let mut parts: [flt2dec::Part; 6] = mem::uninitialized();
98+
let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninitialized();
99+
let mut parts = MaybeUninit::<[flt2dec::Part; 6]>::uninitialized();
99100
let formatted = flt2dec::to_shortest_exp_str(flt2dec::strategy::grisu::format_shortest,
100101
*num, sign, (0, 0), upper,
101-
&mut buf, &mut parts);
102+
buf.get_mut(), parts.get_mut());
102103
fmt.pad_formatted_parts(&formatted)
103104
}
104105
}

src/libcore/mem.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ impl<T> ManuallyDrop<T> {
950950
/// ManuallyDrop::new(Box::new(()));
951951
/// ```
952952
#[stable(feature = "manually_drop", since = "1.20.0")]
953-
#[inline]
953+
#[inline(always)]
954954
pub const fn new(value: T) -> ManuallyDrop<T> {
955955
ManuallyDrop { value }
956956
}
@@ -967,7 +967,7 @@ impl<T> ManuallyDrop<T> {
967967
/// let _: Box<()> = ManuallyDrop::into_inner(x); // This drops the `Box`.
968968
/// ```
969969
#[stable(feature = "manually_drop", since = "1.20.0")]
970-
#[inline]
970+
#[inline(always)]
971971
pub const fn into_inner(slot: ManuallyDrop<T>) -> T {
972972
slot.value
973973
}
@@ -1015,15 +1015,15 @@ impl<T: ?Sized> ManuallyDrop<T> {
10151015
#[stable(feature = "manually_drop", since = "1.20.0")]
10161016
impl<T: ?Sized> Deref for ManuallyDrop<T> {
10171017
type Target = T;
1018-
#[inline]
1018+
#[inline(always)]
10191019
fn deref(&self) -> &T {
10201020
&self.value
10211021
}
10221022
}
10231023

10241024
#[stable(feature = "manually_drop", since = "1.20.0")]
10251025
impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
1026-
#[inline]
1026+
#[inline(always)]
10271027
fn deref_mut(&mut self) -> &mut T {
10281028
&mut self.value
10291029
}
@@ -1044,6 +1044,7 @@ impl<T> MaybeUninit<T> {
10441044
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
10451045
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
10461046
#[unstable(feature = "maybe_uninit", issue = "53491")]
1047+
#[inline(always)]
10471048
pub const fn new(val: T) -> MaybeUninit<T> {
10481049
MaybeUninit { value: ManuallyDrop::new(val) }
10491050
}
@@ -1053,6 +1054,7 @@ impl<T> MaybeUninit<T> {
10531054
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
10541055
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
10551056
#[unstable(feature = "maybe_uninit", issue = "53491")]
1057+
#[inline(always)]
10561058
pub const fn uninitialized() -> MaybeUninit<T> {
10571059
MaybeUninit { uninit: () }
10581060
}
@@ -1066,6 +1068,7 @@ impl<T> MaybeUninit<T> {
10661068
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
10671069
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
10681070
#[unstable(feature = "maybe_uninit", issue = "53491")]
1071+
#[inline]
10691072
pub fn zeroed() -> MaybeUninit<T> {
10701073
let mut u = MaybeUninit::<T>::uninitialized();
10711074
unsafe {
@@ -1076,6 +1079,7 @@ impl<T> MaybeUninit<T> {
10761079

10771080
/// Set the value of the `MaybeUninit`. This overwrites any previous value without dropping it.
10781081
#[unstable(feature = "maybe_uninit", issue = "53491")]
1082+
#[inline(always)]
10791083
pub fn set(&mut self, val: T) {
10801084
unsafe {
10811085
self.value = ManuallyDrop::new(val);
@@ -1091,6 +1095,7 @@ impl<T> MaybeUninit<T> {
10911095
/// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
10921096
/// state, otherwise this will immediately cause undefined behavior.
10931097
#[unstable(feature = "maybe_uninit", issue = "53491")]
1098+
#[inline(always)]
10941099
pub unsafe fn into_inner(self) -> T {
10951100
ManuallyDrop::into_inner(self.value)
10961101
}
@@ -1102,6 +1107,7 @@ impl<T> MaybeUninit<T> {
11021107
/// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
11031108
/// state, otherwise this will immediately cause undefined behavior.
11041109
#[unstable(feature = "maybe_uninit", issue = "53491")]
1110+
#[inline(always)]
11051111
pub unsafe fn get_ref(&self) -> &T {
11061112
&*self.value
11071113
}
@@ -1113,20 +1119,23 @@ impl<T> MaybeUninit<T> {
11131119
/// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
11141120
/// state, otherwise this will immediately cause undefined behavior.
11151121
#[unstable(feature = "maybe_uninit", issue = "53491")]
1122+
#[inline(always)]
11161123
pub unsafe fn get_mut(&mut self) -> &mut T {
11171124
&mut *self.value
11181125
}
11191126

11201127
/// Get a pointer to the contained value. Reading from this pointer will be undefined
11211128
/// behavior unless the `MaybeUninit` is initialized.
11221129
#[unstable(feature = "maybe_uninit", issue = "53491")]
1130+
#[inline(always)]
11231131
pub fn as_ptr(&self) -> *const T {
11241132
unsafe { &*self.value as *const T }
11251133
}
11261134

11271135
/// Get a mutable pointer to the contained value. Reading from this pointer will be undefined
11281136
/// behavior unless the `MaybeUninit` is initialized.
11291137
#[unstable(feature = "maybe_uninit", issue = "53491")]
1138+
#[inline(always)]
11301139
pub fn as_mut_ptr(&mut self) -> *mut T {
11311140
unsafe { &mut *self.value as *mut T }
11321141
}

src/libcore/ptr.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ use ops::{CoerceUnsized, DispatchFromDyn};
7979
use fmt;
8080
use hash;
8181
use marker::{PhantomData, Unsize};
82-
use mem;
82+
use mem::{self, MaybeUninit};
8383
use nonzero::NonZero;
8484

8585
use cmp::Ordering::{self, Less, Equal, Greater};
@@ -295,17 +295,14 @@ pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
295295
#[inline]
296296
#[stable(feature = "rust1", since = "1.0.0")]
297297
pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
298-
// Give ourselves some scratch space to work with
299-
let mut tmp: T = mem::uninitialized();
298+
// Give ourselves some scratch space to work with.
299+
// We do not have to worry about drops: `MaybeUninit` does nothing when dropped.
300+
let mut tmp = MaybeUninit::<T>::uninitialized();
300301

301302
// Perform the swap
302-
copy_nonoverlapping(x, &mut tmp, 1);
303+
copy_nonoverlapping(x, tmp.as_mut_ptr(), 1);
303304
copy(y, x, 1); // `x` and `y` may overlap
304-
copy_nonoverlapping(&tmp, y, 1);
305-
306-
// y and t now point to the same thing, but we need to completely forget `tmp`
307-
// because it's no longer relevant.
308-
mem::forget(tmp);
305+
copy_nonoverlapping(tmp.get_ref(), y, 1);
309306
}
310307

311308
/// Swaps `count * size_of::<T>()` bytes between the two regions of memory
@@ -392,8 +389,8 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
392389
while i + block_size <= len {
393390
// Create some uninitialized memory as scratch space
394391
// Declaring `t` here avoids aligning the stack when this loop is unused
395-
let mut t: Block = mem::uninitialized();
396-
let t = &mut t as *mut _ as *mut u8;
392+
let mut t = mem::MaybeUninit::<Block>::uninitialized();
393+
let t = t.as_mut_ptr() as *mut u8;
397394
let x = x.add(i);
398395
let y = y.add(i);
399396

@@ -407,10 +404,10 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
407404

408405
if i < len {
409406
// Swap any remaining bytes
410-
let mut t: UnalignedBlock = mem::uninitialized();
407+
let mut t = mem::MaybeUninit::<UnalignedBlock>::uninitialized();
411408
let rem = len - i;
412409

413-
let t = &mut t as *mut _ as *mut u8;
410+
let t = t.as_mut_ptr() as *mut u8;
414411
let x = x.add(i);
415412
let y = y.add(i);
416413

@@ -575,9 +572,9 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
575572
#[inline]
576573
#[stable(feature = "rust1", since = "1.0.0")]
577574
pub unsafe fn read<T>(src: *const T) -> T {
578-
let mut tmp: T = mem::uninitialized();
579-
copy_nonoverlapping(src, &mut tmp, 1);
580-
tmp
575+
let mut tmp = MaybeUninit::<T>::uninitialized();
576+
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
577+
tmp.into_inner()
581578
}
582579

583580
/// Reads the value from `src` without moving it. This leaves the
@@ -642,11 +639,11 @@ pub unsafe fn read<T>(src: *const T) -> T {
642639
#[inline]
643640
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
644641
pub unsafe fn read_unaligned<T>(src: *const T) -> T {
645-
let mut tmp: T = mem::uninitialized();
642+
let mut tmp = MaybeUninit::<T>::uninitialized();
646643
copy_nonoverlapping(src as *const u8,
647-
&mut tmp as *mut T as *mut u8,
644+
tmp.as_mut_ptr() as *mut u8,
648645
mem::size_of::<T>());
649-
tmp
646+
tmp.into_inner()
650647
}
651648

652649
/// Overwrites a memory location with the given value without reading or

src/libcore/slice/rotate.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use cmp;
12-
use mem;
12+
use mem::{self, MaybeUninit};
1313
use ptr;
1414

1515
/// Rotation is much faster if it has access to a little bit of memory. This
@@ -26,12 +26,6 @@ union RawArray<T> {
2626
}
2727

2828
impl<T> RawArray<T> {
29-
fn new() -> Self {
30-
unsafe { mem::uninitialized() }
31-
}
32-
fn ptr(&self) -> *mut T {
33-
unsafe { &self.typed as *const T as *mut T }
34-
}
3529
fn cap() -> usize {
3630
if mem::size_of::<T>() == 0 {
3731
usize::max_value()
@@ -88,8 +82,8 @@ pub unsafe fn ptr_rotate<T>(mut left: usize, mid: *mut T, mut right: usize) {
8882
}
8983
}
9084

91-
let rawarray = RawArray::new();
92-
let buf = rawarray.ptr();
85+
let mut rawarray = MaybeUninit::<RawArray<T>>::uninitialized();
86+
let buf = &mut (*rawarray.as_mut_ptr()).typed as *mut [T; 2] as *mut T;
9387

9488
let dim = mid.sub(left).add(right);
9589
if left <= right {

src/libcore/slice/sort.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! stable sorting implementation.
1818
1919
use cmp;
20-
use mem;
20+
use mem::{self, MaybeUninit};
2121
use ptr;
2222

2323
/// When dropped, copies from `src` into `dest`.
@@ -226,14 +226,14 @@ fn partition_in_blocks<T, F>(v: &mut [T], pivot: &T, is_less: &mut F) -> usize
226226
let mut block_l = BLOCK;
227227
let mut start_l = ptr::null_mut();
228228
let mut end_l = ptr::null_mut();
229-
let mut offsets_l: [u8; BLOCK] = unsafe { mem::uninitialized() };
229+
let mut offsets_l = MaybeUninit::<[u8; BLOCK]>::uninitialized();
230230

231231
// The current block on the right side (from `r.sub(block_r)` to `r`).
232232
let mut r = unsafe { l.add(v.len()) };
233233
let mut block_r = BLOCK;
234234
let mut start_r = ptr::null_mut();
235235
let mut end_r = ptr::null_mut();
236-
let mut offsets_r: [u8; BLOCK] = unsafe { mem::uninitialized() };
236+
let mut offsets_r = MaybeUninit::<[u8; BLOCK]>::uninitialized();
237237

238238
// FIXME: When we get VLAs, try creating one array of length `min(v.len(), 2 * BLOCK)` rather
239239
// than two fixed-size arrays of length `BLOCK`. VLAs might be more cache-efficient.
@@ -272,8 +272,8 @@ fn partition_in_blocks<T, F>(v: &mut [T], pivot: &T, is_less: &mut F) -> usize
272272

273273
if start_l == end_l {
274274
// Trace `block_l` elements from the left side.
275-
start_l = offsets_l.as_mut_ptr();
276-
end_l = offsets_l.as_mut_ptr();
275+
start_l = offsets_l.as_mut_ptr() as *mut u8;
276+
end_l = offsets_l.as_mut_ptr() as *mut u8;
277277
let mut elem = l;
278278

279279
for i in 0..block_l {
@@ -288,8 +288,8 @@ fn partition_in_blocks<T, F>(v: &mut [T], pivot: &T, is_less: &mut F) -> usize
288288

289289
if start_r == end_r {
290290
// Trace `block_r` elements from the right side.
291-
start_r = offsets_r.as_mut_ptr();
292-
end_r = offsets_r.as_mut_ptr();
291+
start_r = offsets_r.as_mut_ptr() as *mut u8;
292+
end_r = offsets_r.as_mut_ptr() as *mut u8;
293293
let mut elem = r;
294294

295295
for i in 0..block_r {

0 commit comments

Comments
 (0)