Skip to content

Commit b2c3102

Browse files
committed
fmt: use mem::uninitialized for float formatting buffers
Spending time to initialize these is just wasted work, as we'll overwrite them soon anyway. Fixes #41259.
1 parent 2499d81 commit b2c3102

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

src/libcore/fmt/float.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
use fmt::{Formatter, Result, LowerExp, UpperExp, Display, Debug};
12+
use mem;
1213
use num::flt2dec;
1314

1415
// Don't inline this so callers don't use the stack space this function
@@ -18,12 +19,14 @@ fn float_to_decimal_common_exact<T>(fmt: &mut Formatter, num: &T,
1819
sign: flt2dec::Sign, precision: usize) -> Result
1920
where T: flt2dec::DecodableFloat
2021
{
21-
let mut buf = [0; 1024]; // enough for f32 and f64
22-
let mut parts = [flt2dec::Part::Zero(0); 5];
23-
let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
24-
*num, sign, precision,
25-
false, &mut buf, &mut parts);
26-
fmt.pad_formatted_parts(&formatted)
22+
unsafe {
23+
let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64
24+
let mut parts: [flt2dec::Part; 5] = mem::uninitialized();
25+
let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
26+
*num, sign, precision,
27+
false, &mut buf, &mut parts);
28+
fmt.pad_formatted_parts(&formatted)
29+
}
2730
}
2831

2932
// Don't inline this so callers that call both this and the above won't wind
@@ -33,11 +36,14 @@ fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter,
3336
num: &T, sign: flt2dec::Sign) -> Result
3437
where T: flt2dec::DecodableFloat
3538
{
36-
let mut buf = [0; flt2dec::MAX_SIG_DIGITS]; // enough for f32 and f64
37-
let mut parts = [flt2dec::Part::Zero(0); 5];
38-
let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest,
39-
*num, sign, 0, false, &mut buf, &mut parts);
40-
fmt.pad_formatted_parts(&formatted)
39+
unsafe {
40+
// enough for f32 and f64
41+
let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
42+
let mut parts: [flt2dec::Part; 5] = mem::uninitialized();
43+
let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest,
44+
*num, sign, 0, false, &mut buf, &mut parts);
45+
fmt.pad_formatted_parts(&formatted)
46+
}
4147
}
4248

4349
// Common code of floating point Debug and Display.
@@ -67,12 +73,14 @@ fn float_to_exponential_common_exact<T>(fmt: &mut Formatter, num: &T,
6773
upper: bool) -> Result
6874
where T: flt2dec::DecodableFloat
6975
{
70-
let mut buf = [0; 1024]; // enough for f32 and f64
71-
let mut parts = [flt2dec::Part::Zero(0); 7];
72-
let formatted = flt2dec::to_exact_exp_str(flt2dec::strategy::grisu::format_exact,
73-
*num, sign, precision,
74-
upper, &mut buf, &mut parts);
75-
fmt.pad_formatted_parts(&formatted)
76+
unsafe {
77+
let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64
78+
let mut parts: [flt2dec::Part; 7] = mem::uninitialized();
79+
let formatted = flt2dec::to_exact_exp_str(flt2dec::strategy::grisu::format_exact,
80+
*num, sign, precision,
81+
upper, &mut buf, &mut parts);
82+
fmt.pad_formatted_parts(&formatted)
83+
}
7684
}
7785

7886
// Don't inline this so callers that call both this and the above won't wind
@@ -83,11 +91,15 @@ fn float_to_exponential_common_shortest<T>(fmt: &mut Formatter,
8391
upper: bool) -> Result
8492
where T: flt2dec::DecodableFloat
8593
{
86-
let mut buf = [0; flt2dec::MAX_SIG_DIGITS]; // enough for f32 and f64
87-
let mut parts = [flt2dec::Part::Zero(0); 7];
88-
let formatted = flt2dec::to_shortest_exp_str(flt2dec::strategy::grisu::format_shortest, *num,
89-
sign, (0, 0), upper, &mut buf, &mut parts);
90-
fmt.pad_formatted_parts(&formatted)
94+
unsafe {
95+
// enough for f32 and f64
96+
let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
97+
let mut parts: [flt2dec::Part; 7] = mem::uninitialized();
98+
let formatted = flt2dec::to_shortest_exp_str(flt2dec::strategy::grisu::format_shortest,
99+
*num, sign, (0, 0), upper,
100+
&mut buf, &mut parts);
101+
fmt.pad_formatted_parts(&formatted)
102+
}
91103
}
92104

93105
// Common code of floating point LowerExp and UpperExp.

0 commit comments

Comments
 (0)