nativeview-engine 0.1.0-rc.1

Framework-agnostic scene engine traits, hot-reload cdylib loader, and Frame-0 snapshot codecs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//! Stable-slot, priority-ordered, deadline-aware [`FrameTickRegistry`].

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;

use nativeview_store::{ChangeSet, NodeStore, Transaction};

use super::{FrameTickHook, FrameWaker, SlotWaker, TickDemand, TickHookId, TickPriority};

struct HookSlot {
    id: TickHookId,
    priority: TickPriority,
    hook: Box<dyn FrameTickHook>,
}

#[repr(transparent)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct SlotBitMask(u64);

impl SlotBitMask {
    const EMPTY: Self = Self(0);

    #[inline]
    const fn raw(self) -> u64 {
        self.0
    }

    #[inline]
    const fn is_empty(self) -> bool {
        self.0 == 0
    }

    #[inline]
    const fn contains(self, slot: usize) -> bool {
        (self.0 & (1u64 << slot)) != 0
    }

    #[inline]
    fn set(&mut self, slot: usize) {
        self.0 |= 1u64 << slot;
    }

    #[inline]
    fn clear(&mut self, slot: usize) {
        self.0 &= !(1u64 << slot);
    }

    #[inline]
    fn union_raw(&mut self, bits: u64) {
        self.0 |= bits;
    }

    #[inline]
    fn clear_raw(&mut self, bits: u64) {
        self.0 &= !bits;
    }
}

#[repr(transparent)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
struct MonotonicMs(u64);

impl MonotonicMs {
    const NEVER: Self = Self(u64::MAX);

    #[inline]
    const fn raw(self) -> u64 {
        self.0
    }
}

#[derive(Debug, Clone, Copy, Default)]
struct OverflowState {
    active: bool,
    timed: bool,
    deadline_ms: MonotonicMs,
}

/// Stable-slot, priority-ordered registry of [`FrameTickHook`] participants with O(1) idle and
/// future-deadline checks and zero steady-state allocations.
///
/// Slot indices are monotonically assigned and never shifted on priority insert or removal so
/// external [`SlotWaker`] handles always address their exact slot (wakes for removed slots are
/// masked out via `live_mask`).
pub struct FrameTickRegistry {
    slots: Vec<Option<HookSlot>>,
    /// Live slot indices sorted by `(priority ASC, id ASC)`.
    order: Vec<usize>,
    next_id: TickHookId,
    /// Bit `i` (`i < 64`) is set iff `slots[i]` holds a live hook.
    live_mask: SlotBitMask,
    /// Bit `i` (`i < 64`) is set while slot `i` demands [`TickDemand::NextFrame`].
    active_mask: SlotBitMask,
    /// Bit `i` (`i < 64`) is set while slot `i` is waiting on `deadlines_ms[i]`.
    timed_mask: SlotBitMask,
    /// Monotonic deadlines (`now_ms.saturating_add(delay_ms)`) for timed slots `< 64`.
    deadlines_ms: [MonotonicMs; 64],
    /// Minimum deadline across all timed slots (`< 64` and overflow); `None` when no timed slot is armed.
    earliest_deadline_ms: Option<MonotonicMs>,
    /// Per-slot scheduling state for slots `>= 64` (`slots[64 + i]`); empty unless `slots.len() > 64`.
    overflow: Vec<OverflowState>,
    /// Number of live overflow slots (`>= 64`) currently demanding `NextFrame`.
    overflow_active_count: usize,
    /// Number of live overflow slots (`>= 64`).
    overflow_live_count: usize,
    /// Last monotonic timestamp observed by `refresh_active`, `has_due`, `next_demand`, or `tick_active`.
    last_now_ms: Option<MonotonicMs>,
    /// External wakeups for slots `0..64` from [`FrameWaker`].
    woken_mask: Arc<AtomicU64>,
    /// External wakeups for overflow slots `>= 64` from [`FrameWaker`].
    woken_overflow: Arc<AtomicBool>,
    /// Optional host wake callback attached to every [`FrameWaker`] / [`SlotWaker`] handed out.
    on_wake: Option<Arc<dyn Fn() + Send + Sync>>,
}

impl Default for FrameTickRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl FrameTickRegistry {
    /// Creates an empty registry.
    #[inline]
    pub fn new() -> Self {
        Self::with_optional_on_wake(None)
    }

    /// Creates an empty registry whose [`Self::waker`] and [`Self::slot_waker`] invoke `on_wake`.
    #[inline]
    pub fn with_on_wake(on_wake: Arc<dyn Fn() + Send + Sync>) -> Self {
        Self::with_optional_on_wake(Some(on_wake))
    }

    #[inline]
    fn with_optional_on_wake(on_wake: Option<Arc<dyn Fn() + Send + Sync>>) -> Self {
        Self {
            slots: Vec::new(),
            order: Vec::new(),
            next_id: TickHookId::new(1),
            live_mask: SlotBitMask::EMPTY,
            active_mask: SlotBitMask::EMPTY,
            timed_mask: SlotBitMask::EMPTY,
            deadlines_ms: [MonotonicMs::NEVER; 64],
            earliest_deadline_ms: None,
            overflow: Vec::new(),
            overflow_active_count: 0,
            overflow_live_count: 0,
            last_now_ms: None,
            woken_mask: Arc::new(AtomicU64::new(0)),
            woken_overflow: Arc::new(AtomicBool::new(false)),
            on_wake,
        }
    }

    /// Creates a registry from `hooks` registered at [`TickPriority::NORMAL`].
    pub fn from_hooks(hooks: Vec<Box<dyn FrameTickHook>>) -> Self {
        let mut reg = Self::new();
        reg.slots.reserve(hooks.len());
        reg.order.reserve(hooks.len());
        for hook in hooks {
            reg.push(hook);
        }
        reg
    }

    /// Creates a registry from `(priority, hook)` pairs with a bound `on_wake` callback.
    pub fn from_entries_with_on_wake(
        entries: Vec<(TickPriority, Box<dyn FrameTickHook>)>,
        on_wake: Arc<dyn Fn() + Send + Sync>,
    ) -> Self {
        let mut reg = Self::with_on_wake(on_wake);
        reg.slots.reserve(entries.len());
        reg.order.reserve(entries.len());
        for (priority, hook) in entries {
            reg.push_with_priority(priority, hook);
        }
        reg
    }

    /// Returns a thread-safe [`FrameWaker`] for waking slots from outside `tick()`.
    #[inline]
    pub fn waker(&self) -> FrameWaker {
        FrameWaker {
            woken_mask: Arc::clone(&self.woken_mask),
            woken_overflow: Arc::clone(&self.woken_overflow),
            on_wake: self.on_wake.as_ref().map(Arc::clone),
        }
    }

    /// Returns a thread-safe, zero-alloc [`SlotWaker`] bound to `slot`.
    #[inline]
    pub fn slot_waker(&self, slot: usize) -> SlotWaker {
        SlotWaker::new(self.waker(), slot)
    }

    /// Number of live registered hooks.
    #[inline]
    pub fn len(&self) -> usize {
        self.order.len()
    }

    /// Returns `true` when no live hooks are registered.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.order.is_empty()
    }

    /// Returns the last monotonic timestamp observed by this registry, if any.
    #[inline]
    pub fn last_now_ms(&self) -> Option<u64> {
        self.last_now_ms.map(MonotonicMs::raw)
    }

    /// Registers a hook at [`TickPriority::NORMAL`], returning `(id, stable_slot, initial_demand)`.
    pub fn push(&mut self, hook: Box<dyn FrameTickHook>) -> (TickHookId, usize, TickDemand) {
        self.push_with_priority(TickPriority::NORMAL, hook)
    }

    /// Registers a hook at `priority` without shifting any existing slot index, returning
    /// `(id, stable_slot, initial_demand)`.
    pub fn push_with_priority(
        &mut self,
        priority: TickPriority,
        hook: Box<dyn FrameTickHook>,
    ) -> (TickHookId, usize, TickDemand) {
        let base_ms = self.last_now_ms.map_or(0, MonotonicMs::raw);
        self.push_with_priority_impl(priority, hook, base_ms)
    }

    /// Registers a hook at `priority` anchoring any initial [`TickDemand::AfterMs`] deadline to
    /// monotonic `now_ms` (and advancing `last_now_ms` monotonically).
    pub fn push_with_priority_at(&mut self, priority: TickPriority, hook: Box<dyn FrameTickHook>, now_ms: u64) -> (TickHookId, usize, TickDemand) {
        let base_ms = self.last_now_ms.map_or(now_ms, |prev| prev.raw().max(now_ms));
        self.last_now_ms = Some(MonotonicMs(base_ms));
        self.push_with_priority_impl(priority, hook, base_ms)
    }

    fn push_with_priority_impl(
        &mut self,
        priority: TickPriority,
        hook: Box<dyn FrameTickHook>,
        base_ms: u64,
    ) -> (TickHookId, usize, TickDemand) {
        let slot = self.slots.len();
        let id = self.next_id;
        self.next_id = TickHookId::new(self.next_id.raw().wrapping_add(1).max(1));
        let demand = hook.demand().normalize();
        self.slots.push(Some(HookSlot { id, priority, hook }));

        let pos = self
            .order
            .partition_point(|&existing_slot| {
                let Some(entry) = self.slots[existing_slot].as_ref() else {
                    return false;
                };
                (entry.priority, entry.id) < (priority, id)
            });
        self.order.insert(pos, slot);

        if slot < 64 {
            self.live_mask.set(slot);
            self.deadlines_ms[slot] = MonotonicMs::NEVER;
        } else {
            self.overflow.push(OverflowState {
                active: false,
                timed: false,
                deadline_ms: MonotonicMs::NEVER,
            });
            self.overflow_live_count += 1;
        }

        self.set_slot_demand(slot, demand, base_ms);
        if let TickDemand::AfterMs(_) = demand {
            self.recompute_earliest_deadline();
        }
        (id, slot, demand)
    }

    /// Removes the hook identified by `id` without shifting any other slot's index.
    pub fn remove(&mut self, id: TickHookId) -> bool {
        let Some(order_pos) = self.order.iter().position(|&slot| {
            self.slots[slot]
                .as_ref()
                .is_some_and(|entry| entry.id == id)
        }) else {
            return false;
        };
        let slot = self.order.remove(order_pos);
        self.slots[slot] = None;
        if slot < 64 {
            let bit = 1u64 << slot;
            self.live_mask.clear(slot);
            self.active_mask.clear(slot);
            self.timed_mask.clear(slot);
            self.deadlines_ms[slot] = MonotonicMs::NEVER;
            self.woken_mask.fetch_and(!bit, Ordering::AcqRel);
        } else {
            let ov = &mut self.overflow[slot - 64];
            if ov.active {
                self.overflow_active_count -= 1;
            }
            *ov = OverflowState {
                active: false,
                timed: false,
                deadline_ms: MonotonicMs::NEVER,
            };
            self.overflow_live_count -= 1;
            if self.overflow_live_count == 0 {
                self.woken_overflow.store(false, Ordering::Release);
            }
        }
        self.recompute_earliest_deadline();
        true
    }

    #[inline]
    fn set_slot_demand(&mut self, slot: usize, demand: TickDemand, now_ms: u64) {
        if slot < 64 {
            match demand {
                TickDemand::Idle => {
                    self.active_mask.clear(slot);
                    self.timed_mask.clear(slot);
                    self.deadlines_ms[slot] = MonotonicMs::NEVER;
                }
                TickDemand::NextFrame => {
                    self.active_mask.set(slot);
                    self.timed_mask.clear(slot);
                    self.deadlines_ms[slot] = MonotonicMs::NEVER;
                }
                TickDemand::AfterMs(ms) => {
                    let deadline = MonotonicMs(now_ms.saturating_add(u64::from(ms)));
                    self.active_mask.clear(slot);
                    self.timed_mask.set(slot);
                    self.deadlines_ms[slot] = deadline;
                }
            }
        } else {
            let ov = &mut self.overflow[slot - 64];
            let was_active = ov.active;
            match demand {
                TickDemand::Idle => {
                    ov.active = false;
                    ov.timed = false;
                    ov.deadline_ms = MonotonicMs::NEVER;
                }
                TickDemand::NextFrame => {
                    ov.active = true;
                    ov.timed = false;
                    ov.deadline_ms = MonotonicMs::NEVER;
                }
                TickDemand::AfterMs(ms) => {
                    ov.active = false;
                    ov.timed = true;
                    ov.deadline_ms = MonotonicMs(now_ms.saturating_add(u64::from(ms)));
                }
            }
            match (was_active, ov.active) {
                (false, true) => self.overflow_active_count += 1,
                (true, false) => self.overflow_active_count -= 1,
                _ => {}
            }
        }
    }

    /// Sticky merge used by `refresh_active`: promotes `Idle` -> `AfterMs`/`NextFrame` and
    /// tightens an existing deadline, never dropping previously woken/active slots or pushing
    /// existing deadlines forward.
    #[inline]
    fn merge_slot_demand_sticky(&mut self, slot: usize, demand: TickDemand, now_ms: u64) -> bool {
        match demand {
            TickDemand::Idle => false,
            TickDemand::NextFrame => {
                if slot < 64 {
                    let was_timed = self.timed_mask.contains(slot);
                    self.active_mask.set(slot);
                    self.timed_mask.clear(slot);
                    self.deadlines_ms[slot] = MonotonicMs::NEVER;
                    was_timed
                } else {
                    let ov = &mut self.overflow[slot - 64];
                    let was_timed = ov.timed;
                    if !ov.active {
                        ov.active = true;
                        self.overflow_active_count += 1;
                    }
                    ov.timed = false;
                    ov.deadline_ms = MonotonicMs::NEVER;
                    was_timed
                }
            }
            TickDemand::AfterMs(ms) => {
                let cand = MonotonicMs(now_ms.saturating_add(u64::from(ms)));
                if slot < 64 {
                    if self.active_mask.contains(slot) {
                        return false;
                    }
                    if self.timed_mask.contains(slot) {
                        if cand < self.deadlines_ms[slot] {
                            self.deadlines_ms[slot] = cand;
                            return true;
                        }
                        return false;
                    }
                    self.timed_mask.set(slot);
                    self.deadlines_ms[slot] = cand;
                    true
                } else {
                    let ov = &mut self.overflow[slot - 64];
                    if ov.active {
                        return false;
                    }
                    if ov.timed {
                        if cand < ov.deadline_ms {
                            ov.deadline_ms = cand;
                            return true;
                        }
                        return false;
                    }
                    ov.timed = true;
                    ov.deadline_ms = cand;
                    true
                }
            }
        }
    }

    #[inline]
    fn recompute_earliest_deadline(&mut self) {
        let mut earliest: Option<MonotonicMs> = None;
        let mut tm = self.timed_mask.raw();
        while tm != 0 {
            let slot = tm.trailing_zeros() as usize;
            tm &= tm - 1;
            let d = self.deadlines_ms[slot];
            earliest = Some(earliest.map_or(d, |cur| cur.min(d)));
        }
        if self.overflow_live_count > 0 {
            for ov in &self.overflow {
                if ov.timed {
                    earliest = Some(earliest.map_or(ov.deadline_ms, |cur| cur.min(ov.deadline_ms)));
                }
            }
        }
        self.earliest_deadline_ms = earliest;
    }

    #[inline]
    fn drain_woken(&mut self) {
        let mut timed_changed = false;
        let woken = self.woken_mask.swap(0, Ordering::AcqRel);
        let live_woken = woken & self.live_mask.raw();
        if live_woken != 0 {
            self.active_mask.union_raw(live_woken);
            let cleared_timed = self.timed_mask.raw() & live_woken;
            if cleared_timed != 0 {
                self.timed_mask.clear_raw(live_woken);
                let mut m = cleared_timed;
                while m != 0 {
                    let slot = m.trailing_zeros() as usize;
                    m &= m - 1;
                    self.deadlines_ms[slot] = MonotonicMs::NEVER;
                }
                timed_changed = true;
            }
        }
        if self.woken_overflow.swap(false, Ordering::AcqRel) && self.overflow_live_count > 0 {
            for (off, ov) in self.overflow.iter_mut().enumerate() {
                if self.slots[64 + off].is_some() {
                    if !ov.active {
                        ov.active = true;
                        self.overflow_active_count += 1;
                    }
                    if ov.timed {
                        ov.timed = false;
                        ov.deadline_ms = MonotonicMs::NEVER;
                        timed_changed = true;
                    }
                }
            }
        }
        if timed_changed {
            self.recompute_earliest_deadline();
        }
    }

    #[inline]
    fn drain_woken_if_pending(&mut self) {
        if self.woken_mask.load(Ordering::Acquire) != 0
            || self.woken_overflow.load(Ordering::Acquire)
        {
            self.drain_woken();
        }
    }

    /// Returns `true` if a [`FrameWaker`] woke a live slot since the last drain.
    #[inline]
    pub fn has_pending_wakes(&self) -> bool {
        (self.woken_mask.load(Ordering::Acquire) & self.live_mask.raw()) != 0
            || (self.overflow_live_count > 0 && self.woken_overflow.load(Ordering::Acquire))
    }

    /// Queries every live hook's [`FrameTickHook::demand`] after a store commit and merges any
    /// newly active/timed demands into the schedule (sticky: never clears woken slots or pushes
    /// existing deadlines forward).
    #[inline]
    pub fn refresh_active(&mut self, now_ms: u64) {
        self.last_now_ms = Some(MonotonicMs(now_ms));
        if self.order.is_empty() {
            return;
        }
        let mut timed_changed = false;
        for i in 0..self.order.len() {
            let slot = self.order[i];
            let Some(entry) = self.slots[slot].as_ref() else {
                continue;
            };
            let demand = entry.hook.demand().normalize();
            timed_changed |= self.merge_slot_demand_sticky(slot, demand, now_ms);
        }
        if timed_changed {
            self.recompute_earliest_deadline();
        }
    }

    /// Store-aware variant of [`Self::refresh_active`] calling [`FrameTickHook::demand_on_commit`]
    /// so store-scanning hooks skip unrelated commits with zero allocations.
    #[inline]
    pub fn refresh_active_with_store(
        &mut self,
        store: &NodeStore,
        changes: &ChangeSet,
        now_ms: u64,
    ) {
        self.last_now_ms = Some(MonotonicMs(now_ms));
        if self.order.is_empty() {
            return;
        }
        let mut timed_changed = false;
        for i in 0..self.order.len() {
            let slot = self.order[i];
            let Some(entry) = self.slots[slot].as_ref() else {
                continue;
            };
            let demand = entry.hook.demand_on_commit(store, changes).normalize();
            timed_changed |= self.merge_slot_demand_sticky(slot, demand, now_ms);
        }
        if timed_changed {
            self.recompute_earliest_deadline();
        }
    }

    /// Returns `true` if any live hook is due at `now_ms` (O(1) fast path when idle or waiting
    /// on a future deadline).
    #[inline]
    pub fn has_due(&mut self, now_ms: u64) -> bool {
        let now = MonotonicMs(now_ms);
        self.last_now_ms = Some(now);
        if self.order.is_empty() {
            return false;
        }
        self.drain_woken_if_pending();
        !self.active_mask.is_empty()
            || self.overflow_active_count > 0
            || self.earliest_deadline_ms.is_some_and(|d| d <= now)
    }

    /// Returns the aggregate [`TickDemand`] across all live hooks at `now_ms` in O(1) time with
    /// zero allocations.
    #[inline]
    pub fn next_demand(&mut self, now_ms: u64) -> TickDemand {
        let now = MonotonicMs(now_ms);
        self.last_now_ms = Some(now);
        if self.order.is_empty() {
            return TickDemand::Idle;
        }
        self.drain_woken_if_pending();
        if !self.active_mask.is_empty() || self.overflow_active_count > 0 {
            return TickDemand::NextFrame;
        }
        match self.earliest_deadline_ms {
            None => TickDemand::Idle,
            Some(d) if d <= now => TickDemand::NextFrame,
            Some(d) => TickDemand::AfterMs((d.raw() - now_ms).min(u64::from(u32::MAX)) as u32),
        }
    }

    /// Ticks all due hooks in strict `(priority ASC, id ASC)` order and returns the merged
    /// [`TickDemand`] across ALL live hooks (including non-due timed hooks).
    pub fn tick_active(
        &mut self,
        tx: &mut Transaction<'_>,
        dt_s: f32,
        now_ms: u64,
    ) -> TickDemand {
        let now = MonotonicMs(now_ms);
        self.last_now_ms = Some(now);
        if self.order.is_empty() {
            return TickDemand::Idle;
        }
        self.drain_woken_if_pending();

        let mut due_low = self.active_mask.raw();
        if !self.timed_mask.is_empty() && self.earliest_deadline_ms.is_some_and(|d| d <= now) {
            let mut tm = self.timed_mask.raw();
            while tm != 0 {
                let slot = tm.trailing_zeros() as usize;
                tm &= tm - 1;
                if self.deadlines_ms[slot] <= now {
                    due_low |= 1u64 << slot;
                }
            }
        }

        if due_low == 0
            && self.overflow_active_count == 0
            && self.earliest_deadline_ms.is_none_or(|d| d > now)
        {
            return self.next_demand(now_ms);
        }

        for i in 0..self.order.len() {
            let slot = self.order[i];
            let is_due = if slot < 64 {
                (due_low & (1u64 << slot)) != 0
            } else {
                let ov = &self.overflow[slot - 64];
                ov.active || (ov.timed && ov.deadline_ms <= now)
            };
            if !is_due {
                continue;
            }
            let demand = self.slots[slot]
                .as_mut()
                .expect("order only indexes live slots")
                .hook
                .tick(tx, dt_s, now_ms)
                .normalize();
            self.set_slot_demand(slot, demand, now_ms);
        }

        self.recompute_earliest_deadline();
        self.next_demand(now_ms)
    }
}