Skip to main content

TimeQueue

Struct TimeQueue 

Source
pub struct TimeQueue<T> { /* private fields */ }
Expand description

A time-ordered queue of Ts, earliest first.

§Deduplication is intentional — do not remove it

Pushing a (value, time) pair that is already queued is a no-op: the queue holds at most one entry per distinct (value, time). This is a deliberate feature, relied on by the graph scheduler and feedback machinery — e.g. a node scheduled for the same instant twice, or the same feedback value emitted twice in one cycle, must collapse to a single event rather than fire twice. If you are tempted to “fix” duplicate suppression, it is working as designed; see also CLAUDE.md.

Distinct values at the same time are all kept and pop in FIFO (insertion) order.

§Why PartialEq, not Hash + Eq

Dedup only requires comparing entries for equality, so the bound is T: PartialEq rather than Hash + Eq. That matters because f64 (and other float payloads) implement PartialEq but neither Hash nor Eq, so delay/feedback/CallBackStream work on float streams. (A consequence of float semantics: NaN != NaN, so two NaN entries at the same time are not deduplicated — which is the correct behaviour for NaN.)

§Why the entries are bucketed by time

Dedup only ever compares against entries sharing the same time, so the queue keys entries by time rather than holding one flat heap: a push scans its own instant’s bucket (almost always empty or a single entry) instead of every pending item.

That distinction is on the hot path, not a micro-optimisation. This queue backs the graph scheduler, where every scheduling node holds a pending entry at all times — a ticker re-arms itself the moment it fires — so the pending set is the size of the graph’s timer population, not “a handful”. A flat scan made one Ctx::schedule cost O(pending): measured at 22 ns with one entry queued and 686 ns with 1024, i.e. every timer in the graph taxing every other timer’s tick. delay pays the same shape, its pending set being every value still in flight.

The earliest instant is then held out of the map, in front, and refilled lazily — pop leaves front empty rather than promoting the map’s next instant, and a later push earlier than that instant claims the empty front directly. A BTreeMap insert and remove costs ~30 ns together, and allocates and frees a root node every time the map empties and refills, so the two shapes that matter must not touch it at all:

  • one timer — the map stays empty for the whole run; front alone absorbs every fire-and-re-arm;
  • one fast timer among slow ones — the fast timer’s next fire is earlier than everything in the map, so it lands in the vacated front while the slow instants sit undisturbed. Eager promotion made this the worst case instead: the map thrashed between one entry and none on every cycle.

None of this is observable: earliest time first, FIFO within a time (insertion order), and (value, time) dedup — all exactly as before.

Implementations§

Source§

impl<T> TimeQueue<T>

Source

pub fn new() -> Self

An empty queue.

Source

pub fn next_time(&self) -> Option<NanoTime>

Time of the next item, or None if the queue is empty.

Source

pub fn is_empty(&self) -> bool

Whether nothing is scheduled — checked every cycle, so it must not touch the map beyond is_empty.

Source

pub fn pop(&mut self) -> Option<T>

Pop the earliest item, or None if the queue is empty.

Source

pub fn pop_if_pending(&mut self, current_time: NanoTime) -> Option<T>

Pop the earliest item iff its time is <= current_time. Designed for the while let Some(v) = q.pop_if_pending(now) { ... } idiom that drains all callbacks due at or before the current engine tick.

The common call is the one that finds nothing due — the drain loop’s last iteration, every cycle — so the not-due answer must not touch the map beyond reading its minimum key.

Source

pub fn clear(&mut self)

Drop every pending entry, at every instant. Used when a run ends or a scheduling node is torn down, so a stale schedule cannot resurrect it.

Source§

impl<T: PartialEq> TimeQueue<T>

Source

pub fn push(&mut self, value: T, time: NanoTime)

Push value at time. A (value, time) pair already present in the queue is suppressed (see the type-level docs — dedup is intentional).

Trait Implementations§

Source§

impl<T: Debug> Debug for TimeQueue<T>

Source§

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

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

impl<T> Default for TimeQueue<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for TimeQueue<T>
where Option<(NanoTime, Bucket<T>)>: Freeze, BTreeMap<NanoTime, Bucket<T>>: Freeze,

§

impl<T> RefUnwindSafe for TimeQueue<T>
where Option<(NanoTime, Bucket<T>)>: RefUnwindSafe, BTreeMap<NanoTime, Bucket<T>>: RefUnwindSafe,

§

impl<T> Send for TimeQueue<T>
where Option<(NanoTime, Bucket<T>)>: Send, BTreeMap<NanoTime, Bucket<T>>: Send,

§

impl<T> Sync for TimeQueue<T>
where Option<(NanoTime, Bucket<T>)>: Sync, BTreeMap<NanoTime, Bucket<T>>: Sync,

§

impl<T> Unpin for TimeQueue<T>
where Option<(NanoTime, Bucket<T>)>: Unpin, BTreeMap<NanoTime, Bucket<T>>: Unpin,

§

impl<T> UnsafeUnpin for TimeQueue<T>
where Option<(NanoTime, Bucket<T>)>: UnsafeUnpin, BTreeMap<NanoTime, Bucket<T>>: UnsafeUnpin,

§

impl<T> UnwindSafe for TimeQueue<T>
where Option<(NanoTime, Bucket<T>)>: UnwindSafe, BTreeMap<NanoTime, Bucket<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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.