refactor: extract sampling primitives into shared module#418
Merged
Conversation
Move SplitMix64 and draw_exponential from task_dumped.rs into a new pub(crate) sampling module. Rename draw_exponential_ns → draw_exponential since the math is unit-agnostic (the caller decides whether 'mean' is nanoseconds, bytes, or anything else). This prepares for the memory profiler (commit 2+), which will reuse the same geometric/Poisson sampling primitive to sample on bytes allocated rather than nanoseconds idle. Pure refactor — no behavior change. Existing task-dump tests pass unmodified (only the function name changed).
Fluzko
reviewed
May 18, 2026
Comment on lines
+40
to
+90
| mod tests { | ||
| use super::SplitMix64; | ||
|
|
||
| #[test] | ||
| fn splitmix_deterministic_with_fixed_seed() { | ||
| let mut rng = SplitMix64::new(42); | ||
| let a = rng.next_u64(); | ||
| let b = rng.next_u64(); | ||
|
|
||
| let mut rng2 = SplitMix64::new(42); | ||
| assert_eq!(a, rng2.next_u64()); | ||
| assert_eq!(b, rng2.next_u64()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn draw_exponential_returns_at_least_1() { | ||
| let mut rng = SplitMix64::new(0); | ||
| for _ in 0..1000 { | ||
| assert!(rng.draw_exponential(1) >= 1); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn draw_exponential_mean_approximates_target() { | ||
| let mut rng = SplitMix64::new(123); | ||
| let mean: u64 = 1024; | ||
| let n = 100_000; | ||
| let sum: f64 = (0..n).map(|_| rng.draw_exponential(mean) as f64).sum(); | ||
| let observed_mean = sum / n as f64; | ||
| // Within ±5% of the configured mean. | ||
| assert!( | ||
| (observed_mean - mean as f64).abs() < mean as f64 * 0.05, | ||
| "observed mean {observed_mean} too far from expected {mean}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn draw_exponential_handles_large_mean() { | ||
| let mut rng = SplitMix64::new(999); | ||
| let mean: u64 = 1_000_000_000; | ||
| let mut saw_large = false; | ||
| for _ in 0..1000 { | ||
| let v = rng.draw_exponential(mean); | ||
| assert!(v >= 1); | ||
| if v > 1_000_000 { | ||
| saw_large = true; | ||
| } | ||
| } | ||
| assert!(saw_large, "expected some values much larger than 1"); | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
These are duplicated, they still live at task_dumped.rs. They're not exactly the same, but pretty much they are, probably worth consolidating them in one place
Fluzko
reviewed
May 18, 2026
| /// | ||
| /// The unit is whatever the caller treats `mean` as (nanoseconds, | ||
| /// bytes, etc.). | ||
| pub(crate) fn draw_exponential(&mut self, mean: u64) -> i64 { |
Contributor
There was a problem hiding this comment.
Suggested change
| pub(crate) fn draw_exponential(&mut self, mean: u64) -> i64 { | |
| pub(crate) fn draw_exponential(&mut self, mean: u64) -> u64 { |
this is guaranteed to return >1 so I would change it to a narrower and more correct type
Fluzko
approved these changes
May 18, 2026
…ted tests - Change draw_exponential return type from i64 to u64 since the value is guaranteed >= 1 - Remove duplicated sampling tests from task_dumped.rs (already covered by sampling.rs with stricter tolerances)
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Move SplitMix64 and draw_exponential from task_dumped.rs into a new pub(crate) sampling module. Rename draw_exponential_ns → draw_exponential since the math is unit-agnostic (the caller decides whether 'mean' is nanoseconds, bytes, or anything else).
This prepares for the memory profiler (commit 2+), which will reuse the same geometric/Poisson sampling primitive to sample on bytes allocated rather than nanoseconds idle.
Pure refactor — no behavior change. Existing task-dump tests pass unmodified (only the function name changed).