Skip to main content

RollingMax

Struct RollingMax 

Source
pub struct RollingMax<T, const WINDOW: usize> { /* private fields */ }
Expand description

A rolling accumulator that tracks the largest value in a fixed size window.

  • Push is O(1)
  • Get max is O(1)
  • There are no heap allocations.

Like [std::collections::BinaryHeap], RollingMax exposes a “maximum only” API. The minimum can be found by using core::cmp::Reverse.

use core::cmp::Reverse;
use high_roller::rolling_max::RollingMax;

type RollingMin<T, const WINDOW: usize> = RollingMax<Reverse<T>, WINDOW>;

The example below shows how this might be used to publish telemetry for the highest latency event among the most recent 100 samples.

use high_roller::rolling_max::RollingMax;
use rand::Rng;

// Assume this is unbounded in reality.
let events = (0..1000).map(|_| network_latency_us());

let mut window: RollingMax<u32, 100> = RollingMax::new();
for latency in events {
    window.push(latency);
    window.max().copied().map(emit_network_telemetry);
}

fn network_latency_us() -> u32 {
    rand::rng().next_u32()
}

fn emit_network_telemetry(max_latency_us: u32) {
    core::hint::black_box(max_latency_us);
}

§Design

The algorithm for this is well-known but not formalized anywhere I found easily accessible. The constraint of accumulating values internally is also a slight divergence from how this problem is typically presented. While RollingMax was motivated by a genuine need in production code, I also verified it against LeetCode 239, which exercises the same use case.

Implementations§

Source§

impl<T, const W: usize> RollingMax<T, W>
where T: PartialOrd,

Source

pub const fn new() -> Self

Constructs a new empty RollingMax.

This type is stored entirely on the stack, so be aware of window size. Boxing might be a good idea. Doing so yourself enables cache-friendlier patterns than if each RollingMax were unconditionally allocated on the heap.

use core::cmp::Reverse;
use high_roller::rolling_max::RollingMax;

const WINDOW: usize = 6000;

#[derive(Default)]
struct MyTelemetry {
    max_latency: RollingMax<u32, WINDOW>,
    min_latency: RollingMax<Reverse<u32>, WINDOW>,
    largest_batch: RollingMax<usize, WINDOW>
}

// `MyTelemetry` is too big to live on the stack. But keeping
// everything in one allocation may yield friendlier cache
// access patterns.
let _telemetry = Box::new(MyTelemetry::default());
Source

pub fn push(&mut self, entry: T)

Source

pub fn max(&self) -> Option<&T>

Trait Implementations§

Source§

impl<T: Debug, const WINDOW: usize> Debug for RollingMax<T, WINDOW>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default, const WINDOW: usize> Default for RollingMax<T, WINDOW>

Source§

fn default() -> RollingMax<T, WINDOW>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T, const WINDOW: usize> Freeze for RollingMax<T, WINDOW>
where T: Freeze,

§

impl<T, const WINDOW: usize> RefUnwindSafe for RollingMax<T, WINDOW>
where T: RefUnwindSafe,

§

impl<T, const WINDOW: usize> Send for RollingMax<T, WINDOW>
where T: Send,

§

impl<T, const WINDOW: usize> Sync for RollingMax<T, WINDOW>
where T: Sync,

§

impl<T, const WINDOW: usize> Unpin for RollingMax<T, WINDOW>
where T: Unpin,

§

impl<T, const WINDOW: usize> UnsafeUnpin for RollingMax<T, WINDOW>
where T: UnsafeUnpin,

§

impl<T, const WINDOW: usize> UnwindSafe for RollingMax<T, WINDOW>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.