From b2c3102e625f94a084e357ceb976fc98b42b79bf Mon Sep 17 00:00:00 2001 From: Nathan Froyd Date: Fri, 14 Apr 2017 16:25:49 -0400 Subject: 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. --- src/libcore/fmt/float.rs | 56 +++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 22 deletions(-) (limited to 'src') 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(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(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(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(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. -- cgit 1.4.1-3-g733a5