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;
frontalone 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
frontwhile 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>
impl<T> TimeQueue<T>
Sourcepub fn next_time(&self) -> Option<NanoTime>
pub fn next_time(&self) -> Option<NanoTime>
Time of the next item, or None if the queue is empty.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether nothing is scheduled — checked every cycle, so it must not
touch the map beyond is_empty.
Sourcepub fn pop_if_pending(&mut self, current_time: NanoTime) -> Option<T>
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.