about summary refs log tree commit diff
path: root/src/libcore/fmt
diff options
context:
space:
mode:
authorNathan Froyd <froydnj@gmail.com>2017-04-14 16:25:49 -0400
committerNathan Froyd <froydnj@gmail.com>2017-04-28 16:08:37 -0400
commitb2c3102e625f94a084e357ceb976fc98b42b79bf (patch)
treeebbb7c9d7ee0d7a837b3ddb18705ba0b8d2a2afe /src/libcore/fmt
parent2499d819d41b87d9b562377479e4a81f74bd49f1 (diff)
downloadrust-b2c3102e625f94a084e357ceb976fc98b42b79bf.tar.gz
rust-b2c3102e625f94a084e357ceb976fc98b42b79bf.zip
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.
Diffstat (limited to 'src/libcore/fmt')
-rw-r--r--src/libcore/fmt/float.rs56
1 files changed, 34 insertions, 22 deletions
diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs
index 4252807fb9f..87def375b20 100644
--- a/src/libcore/fmt/float.rs
+++ b/src/libcore/fmt/float.rs
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 use fmt::{Formatter, Result, LowerExp, UpperExp, Display, Debug};
+use mem;
 use num::flt2dec;
 
 // 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,
                                     sign: flt2dec::Sign, precision: usize) -> Result
     where T: flt2dec::DecodableFloat
 {
-    let mut buf = [0; 1024]; // enough for f32 and f64
-    let mut parts = [flt2dec::Part::Zero(0); 5];
-    let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
-                                                *num, sign, precision,
-                                                false, &mut buf, &mut parts);
-    fmt.pad_formatted_parts(&formatted)
+    unsafe {
+        let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64
+        let mut parts: [flt2dec::Part; 5] = mem::uninitialized();
+        let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
+                                                    *num, sign, precision,
+                                                    false, &mut buf, &mut parts);
+        fmt.pad_formatted_parts(&formatted)
+    }
 }
 
 // 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,
                                        num: &T, sign: flt2dec::Sign) -> Result
     where T: flt2dec::DecodableFloat
 {
-    let mut buf = [0; flt2dec::MAX_SIG_DIGITS]; // enough for f32 and f64
-    let mut parts = [flt2dec::Part::Zero(0); 5];
-    let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest,
-                                             *num, sign, 0, false, &mut buf, &mut parts);
-    fmt.pad_formatted_parts(&formatted)
+    unsafe {
+        // enough for f32 and f64
+        let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
+        let mut parts: [flt2dec::Part; 5] = mem::uninitialized();
+        let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest,
+                                                 *num, sign, 0, false, &mut buf, &mut parts);
+        fmt.pad_formatted_parts(&formatted)
+    }
 }
 
 // Common code of floating point Debug and Display.
@@ -67,12 +73,14 @@ fn float_to_exponential_common_exact<T>(fmt: &mut Formatter, num: &T,
                                         upper: bool) -> Result
     where T: flt2dec::DecodableFloat
 {
-    let mut buf = [0; 1024]; // enough for f32 and f64
-    let mut parts = [flt2dec::Part::Zero(0); 7];
-    let formatted = flt2dec::to_exact_exp_str(flt2dec::strategy::grisu::format_exact,
-                                              *num, sign, precision,
-                                              upper, &mut buf, &mut parts);
-    fmt.pad_formatted_parts(&formatted)
+    unsafe {
+        let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64
+        let mut parts: [flt2dec::Part; 7] = mem::uninitialized();
+        let formatted = flt2dec::to_exact_exp_str(flt2dec::strategy::grisu::format_exact,
+                                                  *num, sign, precision,
+                                                  upper, &mut buf, &mut parts);
+        fmt.pad_formatted_parts(&formatted)
+    }
 }
 
 // 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,
                                            upper: bool) -> Result
     where T: flt2dec::DecodableFloat
 {
-    let mut buf = [0; flt2dec::MAX_SIG_DIGITS]; // enough for f32 and f64
-    let mut parts = [flt2dec::Part::Zero(0); 7];
-    let formatted = flt2dec::to_shortest_exp_str(flt2dec::strategy::grisu::format_shortest, *num,
-                                                 sign, (0, 0), upper, &mut buf, &mut parts);
-    fmt.pad_formatted_parts(&formatted)
+    unsafe {
+        // enough for f32 and f64
+        let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
+        let mut parts: [flt2dec::Part; 7] = mem::uninitialized();
+        let formatted = flt2dec::to_shortest_exp_str(flt2dec::strategy::grisu::format_shortest,
+                                                     *num, sign, (0, 0), upper,
+                                                     &mut buf, &mut parts);
+        fmt.pad_formatted_parts(&formatted)
+    }
 }
 
 // Common code of floating point LowerExp and UpperExp.