Skip to content

Commit

Permalink
fmt: use mem::uninitialized for float formatting buffers
Browse files Browse the repository at this point in the history
Spending time to initialize these is just wasted work, as we'll
overwrite them soon anyway.

Fixes #41259.
  • Loading branch information
froydnj committed Apr 28, 2017
1 parent 2499d81 commit b2c3102
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions src/libcore/fmt/float.rs
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit b2c3102

Please sign in to comment.