-
Notifications
You must be signed in to change notification settings - Fork 432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Speed up Uniform Duration sampling. #583
Changes from all commits
2e1265f
fbab1a1
84ffc8d
79450d0
9ea8394
92ff078
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -693,18 +693,23 @@ uniform_float_impl! { f64x8, u64x8, f64, u64, 64 - 52 } | |
#[cfg(feature = "std")] | ||
#[derive(Clone, Copy, Debug)] | ||
pub struct UniformDuration { | ||
offset: Duration, | ||
mode: UniformDurationMode, | ||
offset: u32, | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
#[derive(Debug, Copy, Clone)] | ||
enum UniformDurationMode { | ||
Small { | ||
secs: u64, | ||
nanos: Uniform<u32>, | ||
}, | ||
Medium { | ||
nanos: Uniform<u64>, | ||
}, | ||
Large { | ||
size: Duration, | ||
max_secs: u64, | ||
max_nanos: u32, | ||
secs: Uniform<u64>, | ||
} | ||
} | ||
|
@@ -737,52 +742,72 @@ impl UniformSampler for UniformDuration { | |
let low = *low_b.borrow(); | ||
let high = *high_b.borrow(); | ||
assert!(low <= high, "Uniform::new_inclusive called with `low > high`"); | ||
let size = high - low; | ||
let nanos = size | ||
.as_secs() | ||
.checked_mul(1_000_000_000) | ||
.and_then(|n| n.checked_add(size.subsec_nanos() as u64)); | ||
|
||
let mode = match nanos { | ||
Some(nanos) => { | ||
UniformDurationMode::Small { | ||
nanos: Uniform::new_inclusive(0, nanos), | ||
} | ||
|
||
let low_s = low.as_secs(); | ||
let low_n = low.subsec_nanos(); | ||
let mut high_s = high.as_secs(); | ||
let mut high_n = high.subsec_nanos(); | ||
|
||
if high_n < low_n { | ||
high_s = high_s - 1; | ||
high_n = high_n + 1_000_000_000; | ||
} | ||
|
||
let mode = if low_s == high_s { | ||
UniformDurationMode::Small { | ||
secs: low_s, | ||
nanos: Uniform::new_inclusive(low_n, high_n), | ||
} | ||
None => { | ||
} else { | ||
let max = high_s | ||
.checked_mul(1_000_000_000) | ||
.and_then(|n| n.checked_add(high_n as u64)); | ||
|
||
if let Some(higher_bound) = max { | ||
let lower_bound = low_s * 1_000_000_000 + low_n as u64; | ||
UniformDurationMode::Medium { | ||
nanos: Uniform::new_inclusive(lower_bound, higher_bound), | ||
} | ||
} else { | ||
// An offset is applied to simplify generation of nanoseconds | ||
let max_nanos = high_n - low_n; | ||
UniformDurationMode::Large { | ||
size: size, | ||
secs: Uniform::new_inclusive(0, size.as_secs()), | ||
max_secs: high_s, | ||
max_nanos, | ||
secs: Uniform::new_inclusive(low_s, high_s), | ||
} | ||
} | ||
}; | ||
|
||
UniformDuration { | ||
mode, | ||
offset: low, | ||
offset: low_n, | ||
} | ||
} | ||
|
||
#[inline] | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Duration { | ||
let d = match self.mode { | ||
UniformDurationMode::Small { nanos } => { | ||
match self.mode { | ||
UniformDurationMode::Small { secs, nanos } => { | ||
let n = nanos.sample(rng); | ||
Duration::new(secs, n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately this is now incorrect since it's possible that n > 10^9 (in this case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a |
||
} | ||
UniformDurationMode::Medium { nanos } => { | ||
let nanos = nanos.sample(rng); | ||
Duration::new(nanos / 1_000_000_000, (nanos % 1_000_000_000) as u32) | ||
} | ||
UniformDurationMode::Large { size, secs } => { | ||
UniformDurationMode::Large { max_secs, max_nanos, secs } => { | ||
// constant folding means this is at least as fast as `gen_range` | ||
let nano_range = Uniform::new(0, 1_000_000_000); | ||
loop { | ||
let d = Duration::new(secs.sample(rng), nano_range.sample(rng)); | ||
if d <= size { | ||
break d; | ||
let s = secs.sample(rng); | ||
let n = nano_range.sample(rng); | ||
if !(s == max_secs && n > max_nanos) { | ||
let sum = n + self.offset; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After adding the offset back, |
||
break Duration::new(s, sum); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
self.offset + d | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The offset is only used by Large mode so can be moved there.
Edit: sorry, I see your comment about this. Fine. It should make no difference to the size of the struct either way.