Skip to content

Commit d4c1752

Browse files
committed
Auto merge of #54668 - RalfJung:use-maybe-uninit, r=<try>
Use MaybeUninit 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 96734ae + 81fabcb commit d4c1752

File tree

5 files changed

+40
-47
lines changed

5 files changed

+40
-47
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/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ macro_rules! test_v512 { ($item:item) => {}; }
248248
#[allow(unused_macros)]
249249
macro_rules! vector_impl { ($([$f:ident, $($args:tt)*]),*) => { $($f!($($args)*);)* } }
250250
#[path = "../stdsimd/coresimd/mod.rs"]
251+
// replacing uses of mem::{uninitialized,zeroed} with MaybeUninit needs to be in the stdsimd repo
252+
#[allow(deprecated)]
251253
#[allow(missing_docs, missing_debug_implementations, dead_code, unused_imports)]
252254
#[unstable(feature = "stdsimd", issue = "48556")]
253255
#[cfg(not(stage0))] // allow changes to how stdsimd works in stage0

src/libcore/ptr.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ use ops::CoerceUnsized;
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};
@@ -296,16 +296,12 @@ pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
296296
#[stable(feature = "rust1", since = "1.0.0")]
297297
pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
298298
// Give ourselves some scratch space to work with
299-
let mut tmp: T = mem::uninitialized();
299+
let mut tmp = MaybeUninit::<T>::uninitialized();
300300

301301
// Perform the swap
302-
copy_nonoverlapping(x, &mut tmp, 1);
302+
copy_nonoverlapping(x, tmp.as_mut_ptr(), 1);
303303
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);
304+
copy_nonoverlapping(tmp.get_ref(), y, 1);
309305
}
310306

311307
/// Swaps `count * size_of::<T>()` bytes between the two regions of memory
@@ -392,8 +388,8 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
392388
while i + block_size <= len {
393389
// Create some uninitialized memory as scratch space
394390
// 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;
391+
let mut t = mem::MaybeUninit::<Block>::uninitialized();
392+
let t = t.as_mut_ptr() as *mut u8;
397393
let x = x.add(i);
398394
let y = y.add(i);
399395

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

408404
if i < len {
409405
// Swap any remaining bytes
410-
let mut t: UnalignedBlock = mem::uninitialized();
406+
let mut t = mem::MaybeUninit::<UnalignedBlock>::uninitialized();
411407
let rem = len - i;
412408

413-
let t = &mut t as *mut _ as *mut u8;
409+
let t = t.as_mut_ptr() as *mut u8;
414410
let x = x.add(i);
415411
let y = y.add(i);
416412

@@ -575,9 +571,9 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
575571
#[inline]
576572
#[stable(feature = "rust1", since = "1.0.0")]
577573
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
574+
let mut tmp = MaybeUninit::<T>::uninitialized();
575+
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
576+
tmp.into_inner()
581577
}
582578

583579
/// Reads the value from `src` without moving it. This leaves the
@@ -642,11 +638,11 @@ pub unsafe fn read<T>(src: *const T) -> T {
642638
#[inline]
643639
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
644640
pub unsafe fn read_unaligned<T>(src: *const T) -> T {
645-
let mut tmp: T = mem::uninitialized();
641+
let mut tmp = MaybeUninit::<T>::uninitialized();
646642
copy_nonoverlapping(src as *const u8,
647-
&mut tmp as *mut T as *mut u8,
643+
tmp.as_mut_ptr() as *mut u8,
648644
mem::size_of::<T>());
649-
tmp
645+
tmp.into_inner()
650646
}
651647

652648
/// 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)