Skip to content

Commit 8f98d9a

Browse files
committed
feat(payload): adopt Probability for ValueConf::float_probability
Change `ValueConf::float_probability` from `f32` to the `Probability` alias of `BoundedProbability<{ f32::to_bits(0.0) }>`. The `try_from` impl enforces the finite + `[0.0, 1.0]` invariant at deserialize time; `ValueConf::new` now takes the validated type directly. (`ValueConf::valid` already had no finite/range check on this field, so nothing is removed there.) The new type threads through `NumValueGenerator`'s `Constant` and `Uniform` variants. At the two comparison sites in `<NumValueGenerator as Generator>:: generate`, `.get()` extracts the inner `f32` so RNG sequences and bit-exact output are preserved. Verified: `cargo test -p lading-payload` (250 pass). `cargo bench -p lading-payload --bench dogstatsd --baseline pr1` shows ±2-6% swings across all throughput sizes; the unrelated `cache_*` benches in `--bench block` show similar magnitude run-to-run variance, so the delta is noise rather than a real effect.
1 parent 2905d45 commit 8f98d9a

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

lading_payload/src/dogstatsd.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ impl Default for MetricWeights {
113113
#[serde(deny_unknown_fields)]
114114
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
115115
pub struct ValueConf {
116-
/// Odds out of 256 that the value will be a float and not an integer.
117-
float_probability: f32,
116+
/// Probability that the value will be a float and not an integer.
117+
float_probability: Probability,
118118
range: ConfRange<i64>,
119119
}
120120

121121
impl ValueConf {
122122
/// Create a new instance of `ValueConf` according to the args
123123
#[must_use]
124-
pub fn new(float_probability: f32, range: ConfRange<i64>) -> Self {
124+
pub fn new(float_probability: Probability, range: ConfRange<i64>) -> Self {
125125
Self {
126126
float_probability,
127127
range,
@@ -136,7 +136,7 @@ impl ValueConf {
136136
impl Default for ValueConf {
137137
fn default() -> Self {
138138
Self {
139-
float_probability: 0.5, // 50%
139+
float_probability: Probability::try_new(0.5).expect("0.5 is in [0.0, 1.0]"),
140140
range: ConfRange::Inclusive {
141141
min: i64::MIN,
142142
max: i64::MAX,

lading_payload/src/dogstatsd/common.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rand::{
66
prelude::Distribution,
77
};
88

9-
use crate::{Error, Generator};
9+
use crate::{Error, Generator, common::config::Probability};
1010

1111
use super::{ConfRange, ValueConf};
1212

@@ -21,12 +21,12 @@ pub enum NumValue {
2121
#[derive(Clone, Debug)]
2222
pub(crate) enum NumValueGenerator {
2323
Constant {
24-
float_probability: f32,
24+
float_probability: Probability,
2525
int: i64,
2626
float: f64,
2727
},
2828
Uniform {
29-
float_probability: f32,
29+
float_probability: Probability,
3030
int_distr: Uniform<i64>,
3131
float_distr: Uniform<f64>,
3232
},
@@ -67,7 +67,7 @@ impl<'a> Generator<'a> for NumValueGenerator {
6767
int,
6868
float,
6969
} => {
70-
if prob < *float_probability {
70+
if prob < float_probability.get() {
7171
Ok(NumValue::Float(*float))
7272
} else {
7373
Ok(NumValue::Int(*int))
@@ -78,7 +78,7 @@ impl<'a> Generator<'a> for NumValueGenerator {
7878
int_distr,
7979
float_distr,
8080
} => {
81-
if prob < *float_probability {
81+
if prob < float_probability.get() {
8282
Ok(NumValue::Float(float_distr.sample(rng)))
8383
} else {
8484
Ok(NumValue::Int(int_distr.sample(rng)))

0 commit comments

Comments
 (0)