Skip to content

Commit 9177334

Browse files
committed
Auto merge of #54668 - RalfJung:use-maybe-uninit, r=<try>
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 71d3a71 + 1fff3e3 commit 9177334

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
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/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)