-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #76241 - RalfJung:flt2dec, r=Mark-Simulacrum
flt2dec: properly handle uninitialized memory The float-to-str code currently uses uninitialized memory incorrectly (see #76092). This PR fixes that. Specifically, that code used `&mut [T]` as "out references", but it would be incorrect for the caller to actually pass uninitialized memory. So the PR changes this to `&mut [MaybeUninit<T>]`, and then functions return a `&[T]` to the part of the buffer that they initialized (some functions already did that, indirectly via `&Formatted`, others were adjusted to return that buffer instead of just the initialized length). What I particularly like about this is that it moves `unsafe` to the right place: previously, the outermost caller had to use `unsafe` to assert that things are initialized; now it is the functions that do the actual initializing which have the corresponding `unsafe` block when they call `MaybeUninit::slice_get_ref` (renamed in #76217 to `slice_assume_init_ref`). Reviewers please be aware that I have no idea how any of this code actually works. My changes were purely mechanical and type-driven. The test suite passes so I guess I didn't screw up badly... Cc @sfackler this is somewhat related to your RFC, and possibly some of this code could benefit from (a generalized version of) the API you describe there. But for now I think what I did is "good enough". Fixes #76092.
- Loading branch information
Showing
8 changed files
with
387 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,76 @@ | ||
use super::super::*; | ||
use core::num::flt2dec::strategy::dragon::*; | ||
use std::mem::MaybeUninit; | ||
use test::Bencher; | ||
|
||
#[bench] | ||
fn bench_small_shortest(b: &mut Bencher) { | ||
let decoded = decode_finite(3.141592f64); | ||
let mut buf = [0; MAX_SIG_DIGITS]; | ||
b.iter(|| format_shortest(&decoded, &mut buf)); | ||
let mut buf = [MaybeUninit::new(0); MAX_SIG_DIGITS]; | ||
b.iter(|| { | ||
format_shortest(&decoded, &mut buf); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn bench_big_shortest(b: &mut Bencher) { | ||
let decoded = decode_finite(f64::MAX); | ||
let mut buf = [0; MAX_SIG_DIGITS]; | ||
b.iter(|| format_shortest(&decoded, &mut buf)); | ||
let mut buf = [MaybeUninit::new(0); MAX_SIG_DIGITS]; | ||
b.iter(|| { | ||
format_shortest(&decoded, &mut buf); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn bench_small_exact_3(b: &mut Bencher) { | ||
let decoded = decode_finite(3.141592f64); | ||
let mut buf = [0; 3]; | ||
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN)); | ||
let mut buf = [MaybeUninit::new(0); 3]; | ||
b.iter(|| { | ||
format_exact(&decoded, &mut buf, i16::MIN); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn bench_big_exact_3(b: &mut Bencher) { | ||
let decoded = decode_finite(f64::MAX); | ||
let mut buf = [0; 3]; | ||
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN)); | ||
let mut buf = [MaybeUninit::new(0); 3]; | ||
b.iter(|| { | ||
format_exact(&decoded, &mut buf, i16::MIN); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn bench_small_exact_12(b: &mut Bencher) { | ||
let decoded = decode_finite(3.141592f64); | ||
let mut buf = [0; 12]; | ||
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN)); | ||
let mut buf = [MaybeUninit::new(0); 12]; | ||
b.iter(|| { | ||
format_exact(&decoded, &mut buf, i16::MIN); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn bench_big_exact_12(b: &mut Bencher) { | ||
let decoded = decode_finite(f64::MAX); | ||
let mut buf = [0; 12]; | ||
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN)); | ||
let mut buf = [MaybeUninit::new(0); 12]; | ||
b.iter(|| { | ||
format_exact(&decoded, &mut buf, i16::MIN); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn bench_small_exact_inf(b: &mut Bencher) { | ||
let decoded = decode_finite(3.141592f64); | ||
let mut buf = [0; 1024]; | ||
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN)); | ||
let mut buf = [MaybeUninit::new(0); 1024]; | ||
b.iter(|| { | ||
format_exact(&decoded, &mut buf, i16::MIN); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn bench_big_exact_inf(b: &mut Bencher) { | ||
let decoded = decode_finite(f64::MAX); | ||
let mut buf = [0; 1024]; | ||
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN)); | ||
let mut buf = [MaybeUninit::new(0); 1024]; | ||
b.iter(|| { | ||
format_exact(&decoded, &mut buf, i16::MIN); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.