uiharu-core 0.1.0

The runtime of the uiharu streaming protocol decode engine
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
// SPDX-License-Identifier: Apache-2.0

//! Event streams: the envelope every event carries, the control items that travel with events,
//! and the bounded broadcast queue that hands one producer's items to every reader.
//!
//! A queue holds each item once. Readers keep their own position and the queue drops an item
//! once every reader has passed it, so a slow reader holds the queue, and through it the
//! producer, back: that is the backpressure decoders feel in [`Outbox::emit`].
//!
//! [`Outbox::emit`]: crate::node::Outbox::emit

use std::any::Any;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;

use serde::{Deserialize, Serialize};

use crate::time::{Tick, TickRange};

/// An event with its envelope: where on the signal it comes from, when it became certain and
/// its position in the stream.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Event<T> {
    /// The event's number in its stream, from 0, counting events only.
    pub seq: u64,
    /// The ticks the event covers on the signal; empty for a result decided at one instant.
    pub span: TickRange,
    /// The tick at which the result became certain. No later event of the stream is settled
    /// earlier.
    pub settled: Tick,
    /// The event itself.
    pub payload: T,
}

/// What travels along a stream: events, and the control items that frame them.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Item<T> {
    /// An event.
    Event(Event<T>),
    /// Samples, and whatever happened in them, are missing for these ticks.
    Gap(TickRange),
    /// Nothing more will be reported before this tick; time has moved on without events.
    Progress(Tick),
    /// The stream has ended at this tick; nothing follows.
    End(Tick),
}

impl<T> Item<T> {
    /// The event, if this is one.
    pub fn event(&self) -> Option<&Event<T>> {
        match self {
            Item::Event(event) => Some(event),
            _ => None,
        }
    }

    /// The event, if this is one, giving the item up.
    pub fn into_event(self) -> Option<Event<T>> {
        match self {
            Item::Event(event) => Some(event),
            _ => None,
        }
    }

    /// Whether this is the end of the stream.
    pub fn is_end(&self) -> bool {
        matches!(self, Item::End(_))
    }

    /// The tick at which the item takes effect: where an event's span starts, where a gap
    /// begins, the tick progress or the end announces. A node reading several inputs takes
    /// the item whose time is earliest, so that what it does depends on the items alone.
    pub fn time(&self) -> Tick {
        match self {
            Item::Event(event) => event.span.start,
            Item::Gap(range) => range.start,
            Item::Progress(tick) | Item::End(tick) => *tick,
        }
    }
}

/// A reader's identity on a [`Broadcast`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ReaderId(usize);

/// A bounded queue from one producer to any number of readers.
#[derive(Debug)]
pub struct Broadcast<T> {
    items: VecDeque<Item<T>>,
    /// The absolute index of `items[0]`.
    head: u64,
    /// The next absolute index each reader will read; `None` for a detached reader.
    readers: Vec<Option<u64>>,
    capacity: usize,
    next_seq: u64,
    high_water: usize,
    ended: bool,
}

impl<T> Broadcast<T> {
    /// A queue holding at most `capacity` items; at least one.
    pub fn new(capacity: usize) -> Self {
        Self {
            items: VecDeque::new(),
            head: 0,
            readers: Vec::new(),
            capacity: capacity.max(1),
            next_seq: 0,
            high_water: 0,
            ended: false,
        }
    }

    /// Adds a reader that starts at the oldest item still held.
    pub fn add_reader(&mut self) -> ReaderId {
        self.readers.push(Some(self.head));
        ReaderId(self.readers.len() - 1)
    }

    /// Removes a reader; the items it alone held back are dropped.
    pub fn remove_reader(&mut self, reader: ReaderId) {
        if let Some(slot) = self.readers.get_mut(reader.0) {
            *slot = None;
        }
        self.trim();
    }

    /// Whether another item would not fit.
    pub fn is_full(&self) -> bool {
        self.items.len() >= self.capacity
    }

    /// Whether the end of the stream has been pushed.
    pub fn is_ended(&self) -> bool {
        self.ended
    }

    /// The items held right now.
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Whether no item is held.
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// The most items held at any time so far.
    pub fn high_water(&self) -> usize {
        self.high_water
    }

    /// The most items the queue holds.
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// The next event's sequence number.
    pub fn next_seq(&self) -> u64 {
        self.next_seq
    }

    /// Appends `item`, numbering an event, or hands it back when the queue is full. Two
    /// progress items in a row fold into one, so progress never fills a queue. Nothing can
    /// follow the end of the stream.
    pub fn try_push(&mut self, item: Item<T>) -> Result<(), Item<T>> {
        debug_assert!(
            !self.ended,
            "an item was pushed after the end of the stream"
        );
        if let Item::Progress(tick) = item
            && let Some(Item::Progress(last)) = self.items.back_mut()
        {
            *last = (*last).max(tick);
            return Ok(());
        }
        if self.is_full() {
            return Err(item);
        }
        let item = match item {
            Item::Event(mut event) => {
                event.seq = self.next_seq;
                self.next_seq += 1;
                Item::Event(event)
            }
            Item::End(tick) => {
                self.ended = true;
                Item::End(tick)
            }
            other => other,
        };
        self.items.push_back(item);
        self.high_water = self.high_water.max(self.items.len());
        Ok(())
    }

    /// The items waiting for `reader`.
    pub fn pending(&self, reader: ReaderId) -> usize {
        let Some(Some(next)) = self.readers.get(reader.0).copied() else {
            return 0;
        };
        usize::try_from(self.head + self.items.len() as u64 - next).unwrap_or(0)
    }

    /// The next item for `reader`, if one is waiting.
    pub fn peek(&self, reader: ReaderId) -> Option<&Item<T>> {
        let next = self.readers.get(reader.0).copied().flatten()?;
        self.items.get(usize::try_from(next - self.head).ok()?)
    }

    /// Moves `reader` past its next item. The end of the stream stays in place: a reader never
    /// moves past it.
    pub fn advance(&mut self, reader: ReaderId) {
        let Some(Some(next)) = self.readers.get(reader.0).copied() else {
            return;
        };
        let Some(item) = self
            .items
            .get(usize::try_from(next - self.head).unwrap_or(usize::MAX))
        else {
            return;
        };
        if item.is_end() {
            return;
        }
        if let Some(slot) = self.readers.get_mut(reader.0) {
            *slot = Some(next + 1);
        }
        self.trim();
    }

    /// Drops the items every reader has passed.
    fn trim(&mut self) {
        let slowest = self.readers.iter().flatten().copied().min();
        let keep_from = match slowest {
            Some(index) => index,
            // Without readers nothing holds the items back.
            None => self.head + self.items.len() as u64,
        };
        while self.head < keep_from {
            if self.items.front().is_some_and(Item::is_end) {
                break;
            }
            self.items.pop_front();
            self.head += 1;
        }
    }
}

/// What the executor does with a queue without knowing its payload type.
pub trait QueueControl {
    /// The items held right now.
    fn len(&self) -> usize;
    /// Whether no item is held.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
    /// The most items the queue holds.
    fn capacity(&self) -> usize;
    /// The most items held at any time so far.
    fn high_water(&self) -> usize;
    /// Whether the end of the stream has been pushed.
    fn is_ended(&self) -> bool;
    /// Adds a reader that starts at the oldest item still held.
    fn add_reader(&self) -> ReaderId;
    /// Pushes `Progress(tick)` without knowing the event type: `false` when the queue is full
    /// and the item could not fold into a progress item already at the back.
    fn try_progress(&self, tick: Tick) -> bool;
    /// The items `reader` has not passed yet.
    fn pending(&self, reader: ReaderId) -> usize;
}

impl<T: 'static> QueueControl for RefCell<Broadcast<T>> {
    fn len(&self) -> usize {
        self.borrow().len()
    }

    fn add_reader(&self) -> ReaderId {
        self.borrow_mut().add_reader()
    }

    fn capacity(&self) -> usize {
        self.borrow().capacity()
    }

    fn high_water(&self) -> usize {
        self.borrow().high_water()
    }

    fn is_ended(&self) -> bool {
        self.borrow().is_ended()
    }

    fn try_progress(&self, tick: Tick) -> bool {
        self.borrow_mut().try_push(Item::Progress(tick)).is_ok()
    }

    fn pending(&self, reader: ReaderId) -> usize {
        self.borrow().pending(reader)
    }
}

/// A queue as the graph holds it: untyped for binding, and for control and statistics.
#[derive(Clone)]
pub(crate) struct QueueHandles {
    pub(crate) any: Rc<dyn Any>,
    pub(crate) control: Rc<dyn QueueControl>,
}

/// Makes the queue of an output port carrying `T`.
pub(crate) fn make_queue<T: 'static>(capacity: usize) -> QueueHandles {
    let queue = Rc::new(RefCell::new(Broadcast::<T>::new(capacity)));
    QueueHandles {
        any: Rc::clone(&queue) as Rc<dyn Any>,
        control: queue,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn event(payload: u32) -> Item<u32> {
        Item::Event(Event {
            seq: 0,
            span: TickRange::at(Tick(0)),
            settled: Tick(0),
            payload,
        })
    }

    #[test]
    fn readers_share_items_and_the_slowest_holds_them() {
        let mut queue = Broadcast::new(2);
        let fast = queue.add_reader();
        let slow = queue.add_reader();
        assert!(queue.try_push(event(1)).is_ok());
        assert!(queue.try_push(event(2)).is_ok());
        assert!(queue.try_push(event(3)).is_err(), "the queue is full");

        assert_eq!(
            queue.peek(fast).and_then(Item::event).map(|e| e.payload),
            Some(1)
        );
        queue.advance(fast);
        queue.advance(fast);
        assert!(queue.peek(fast).is_none());
        assert_eq!(queue.len(), 2, "the slow reader still holds both");
        assert!(queue.is_full());

        queue.advance(slow);
        assert_eq!(queue.len(), 1);
        assert!(queue.try_push(event(3)).is_ok());
        assert_eq!(
            queue
                .peek(fast)
                .and_then(Item::event)
                .map(|e| (e.seq, e.payload)),
            Some((2, 3))
        );
        assert_eq!(queue.high_water(), 2);
    }

    #[test]
    fn events_are_numbered_in_order() {
        let mut queue = Broadcast::new(8);
        let reader = queue.add_reader();
        for payload in 0..3 {
            queue.try_push(event(payload)).expect("fits");
        }
        queue
            .try_push(Item::Gap(TickRange::new(Tick(1), Tick(2))))
            .expect("fits");
        queue.try_push(event(3)).expect("fits");
        let mut seqs = Vec::new();
        while let Some(item) = queue.peek(reader) {
            if let Some(event) = item.event() {
                seqs.push(event.seq);
            }
            queue.advance(reader);
        }
        assert_eq!(seqs, vec![0, 1, 2, 3]);
    }

    #[test]
    fn progress_folds_and_the_end_stays() {
        let mut queue: Broadcast<u32> = Broadcast::new(1);
        let reader = queue.add_reader();
        queue.try_push(Item::Progress(Tick(5))).expect("fits");
        queue.try_push(Item::Progress(Tick(9))).expect("fits");
        queue.try_push(Item::Progress(Tick(7))).expect("fits");
        assert_eq!(queue.len(), 1);
        assert_eq!(queue.peek(reader), Some(&Item::Progress(Tick(9))));
        queue.advance(reader);
        queue.try_push(Item::End(Tick(10))).expect("fits");
        assert!(queue.is_ended());
        queue.advance(reader);
        queue.advance(reader);
        assert_eq!(queue.peek(reader), Some(&Item::End(Tick(10))));
        assert_eq!(queue.len(), 1);
    }

    #[test]
    fn removing_a_reader_releases_its_items() {
        let mut queue = Broadcast::new(4);
        let gone = queue.add_reader();
        let stays = queue.add_reader();
        queue.try_push(event(1)).expect("fits");
        queue.advance(stays);
        assert_eq!(queue.len(), 1);
        queue.remove_reader(gone);
        assert!(queue.is_empty());
        assert!(queue.peek(gone).is_none());
    }
}