pub struct Window<const N: usize> { /* private fields */ }Expand description
A rolling window of the newest N samples, and the percentiles read off it.
The samples are nanoseconds by convention rather than by type: every caller
so far is timing something, and a Duration here would push the conversion
into the percentile arithmetic instead of leaving it at the edges where the
unit is known.
N is a const parameter and not a field so that
percentiles can sort into a stack array. This runs
once a frame while a debug panel is up, and a fresh Vec per call would be
an allocation on that path for a buffer whose size the window already bounds.
Implementations§
Source§impl<const N: usize> Window<N>
impl<const N: usize> Window<N>
Sourcepub fn record(&mut self, sample: u64)
pub fn record(&mut self, sample: u64)
Records one sample, evicting the oldest when the window is full.
Sourcepub fn percentiles(&self) -> Option<(u64, u64)>
pub fn percentiles(&self) -> Option<(u64, u64)>
The window’s p50 and p95, or None below MIN_PERCENTILE_SAMPLES.
The floor is the whole reason this is one type rather than a sort at
each call site: a p95 over fewer samples is the maximum wearing a
percentile’s name, and a caller that forgets the check reports it anyway.