pub struct DecayMovingAverage { /* private fields */ }
Expand description
A moving average that decays over time so that the average value skews towards the newer values over time.
The decay factor is a value between 0 and 1 that determines how much of the previous value is kept when updating the average.
A decay factor of 0 means that the average is completely replaced with the new value every time, and a decay factor of 1 means that the average never changes (keeps the old value).
§Choosing Decay Factors for Different Behaviors
Lower decay factor (closer to 0.0):
- Adapts quickly to new values, making the average more responsive to recent changes
- Outliers are “forgotten” faster, providing better tolerance to temporary spikes or anomalies
- Useful for tracking recent trends where you want to react quickly to changes
- Example:
0.1
- heavily weights recent values, good for responsive latency tracking
Higher decay factor (closer to 1.0):
- Changes slowly and retains more historical information
- Outliers have a longer-lasting impact on the average, making them more visible
- Provides more stable tracking that’s less sensitive to temporary fluctuations
- Example:
0.9
- heavily weights historical values, good for stable baseline tracking
When using this to track moving average of latency, it is important that there should be a cap on the maximum value that can be stored.
Implementations§
Source§impl DecayMovingAverage
impl DecayMovingAverage
Sourcepub fn new(init_value: f64, decay_factor: f64) -> Self
pub fn new(init_value: f64, decay_factor: f64) -> Self
Create a new DecayMovingAverage with an initial value and decay factor.
§Arguments
init_value
- The initial value for the moving averagedecay_factor
- A value between 0.0 and 1.0 that controls how much historical data is retained. Lower values (e.g., 0.1) make the average more responsive to recent values and better at forgetting outliers. Higher values (e.g., 0.9) make the average more stable but outliers will have a longer-lasting impact.
Sourcepub fn update_moving_average(&mut self, value: f64)
pub fn update_moving_average(&mut self, value: f64)
Update the moving average with a new value.
The new value is weighted by (1 - decay_factor), and the previous value is weighted by decay_factor, so that the average value skews towards the newer values over time.
Sourcepub fn override_moving_average(&mut self, value: f64)
pub fn override_moving_average(&mut self, value: f64)
Override the moving average with a new value, bypassing the decay calculation.
Unlike update_moving_average()
, this method immediately sets the average to the new value
rather than blending it with the previous value using the decay factor.
This is particularly useful for implementing patterns like “decay moving max”:
- Track the maximum value seen recently, but let it decay over time if no new maxima occur
- When a new maximum is encountered, immediately jump to that value using
override_moving_average()
- For regular updates below the maximum, use
update_moving_average()
to let the value decay naturally
§Example: Decay Moving Max
fn update_moving_max(new_value: f64) {
use mysten_common::decay_moving_average::DecayMovingAverage;
let mut decay_max = DecayMovingAverage::new(0.0, 0.9);
// New maximum encountered - immediately jump to it
if new_value > decay_max.get() {
decay_max.override_moving_average(new_value);
} else {
// Let the maximum decay naturally toward the current value
decay_max.update_moving_average(new_value);
}
}
Trait Implementations§
Source§impl Clone for DecayMovingAverage
impl Clone for DecayMovingAverage
Source§fn clone(&self) -> DecayMovingAverage
fn clone(&self) -> DecayMovingAverage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for DecayMovingAverage
impl RefUnwindSafe for DecayMovingAverage
impl Send for DecayMovingAverage
impl Sync for DecayMovingAverage
impl Unpin for DecayMovingAverage
impl UnwindSafe for DecayMovingAverage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.