ondas 2.1.0

Read-only, format-independent waveform analysis library
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
use std::{cmp::Reverse, collections::BinaryHeap, ops::ControlFlow};

#[cfg(test)]
use std::sync::atomic::Ordering;

use super::{
    body,
    types::{Storage, Types},
    wire::Wire,
};
use crate::{Result, Time};

// A backend budget, not a platform or resident-set limit. The representation is
// bounded by the validated source: frames, one frame-number stream per group and
// two fields per (group, frame) pair. An input that does not fit keeps the
// sequential walker.
const INDEX_BYTES: usize = 512 * 1024 * 1024;
// Consecutive active ranks covered by one group. A group records validated
// source positions only, never values or query results.
pub(super) const RANKS_PER_GROUP: usize = 64;
// Minimum growth steps keep tiny budgets usable; growth itself is geometric.
const FRAME_CHUNK: usize = 64;
const STREAM_START: usize = 16;
// A group that already has a frame recorded in the current frame.
const NO_GROUP: usize = usize::MAX;
// Retained per-group arrays: two byte streams of `Vec<u8>` elements plus one
// `u64` and three `u32` counters. Used for the constructor preflight.
const GROUPS_PER_ELEMENT_BYTES: usize =
    2 * size_of::<Vec<u8>>() + size_of::<u64>() + 3 * size_of::<u32>();
// A group without a recorded pair yet.
const NO_FRAME: u32 = u32::MAX;

/// One validated source list.
#[derive(Clone, Copy)]
struct Frame {
    time: Time,
    /// True for cycle lists, where every record carries a rank delta.
    cyclic: bool,
}

/// Source positions of validated payloads, shared by whole recipes.
///
/// A frame is one validated source list; it keeps the tick and the list kind.
/// Rank groups of `RANKS_PER_GROUP` consecutive active ranks keep, for every
/// frame where the group reports records, the frame number and the position of
/// its first record: the payload offset and the rank inside the group. Streams
/// are kept per group and stay in ascending frame order, so a query walks them
/// forward, seeks straight to the selected runs and decodes them again with the
/// checked scalar decoder. A representation that does not fit is dropped and
/// the sequential walker answers instead.
pub(super) struct Index {
    frames: Vec<Frame>,
    /// Frame numbers per group: absolute first, then `(delta, repeat)` runs.
    ids: Vec<Vec<u8>>,
    /// `(offset delta, rank in group)` per (group, frame) pair.
    offs: Vec<Vec<u8>>,
    limit: usize,
    allocated: usize,
    // Build state, released by `finish` so it is not retained.
    open: bool,
    current: u32,
    last_group: usize,
    last_frame: Vec<u32>,
    last_offset: Vec<u64>,
    pending_delta: Vec<u32>,
    pending_run: Vec<u32>,
    // Query work counters, kept separate from the retained size.
    // Query diagnostics stay inside the immutable representation so a completed index can be
    // shared between logical opens without a lock; only tests write them. They are shared by
    // every logical open, and a reset at the start of an indexed read does not make them
    // per-reader while several readers run, so diagnostics must use a private cache or a
    // call-local measurement rather than treating these as per-reader state.
    #[cfg(test)]
    frames_scanned: std::sync::atomic::AtomicUsize,
    #[cfg(test)]
    payloads_decoded: std::sync::atomic::AtomicUsize,
    #[cfg(test)]
    payloads_skipped: std::sync::atomic::AtomicUsize,
    #[cfg(test)]
    groups_selected: std::sync::atomic::AtomicUsize,
}

/// Query work of the last indexed read.
#[cfg(test)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Work {
    pub(super) frames_scanned: usize,
    pub(super) payloads_decoded: usize,
    pub(super) payloads_skipped: usize,
    pub(super) groups_selected: usize,
}

impl Index {
    pub(super) fn new(active: usize) -> Option<Self> {
        Self::with_limit(active, INDEX_BYTES)
    }

    fn with_limit(active: usize, limit: usize) -> Option<Self> {
        let count = active.div_ceil(RANKS_PER_GROUP);
        // Aggregate preflight before any reserve: an oversized active table is
        // refused without requesting optional capacity. The post-reserve check
        // below keeps the accounting exact if the allocator rounds up.
        if count.checked_mul(GROUPS_PER_ELEMENT_BYTES)? > limit {
            return None;
        }
        let mut ids = Vec::new();
        ids.try_reserve_exact(count).ok()?;
        let mut offs = Vec::new();
        offs.try_reserve_exact(count).ok()?;
        let mut last_frame = Vec::new();
        last_frame.try_reserve_exact(count).ok()?;
        let mut last_offset = Vec::new();
        last_offset.try_reserve_exact(count).ok()?;
        let mut pending_delta = Vec::new();
        pending_delta.try_reserve_exact(count).ok()?;
        let mut pending_run = Vec::new();
        pending_run.try_reserve_exact(count).ok()?;
        let allocated = (ids.capacity() + offs.capacity()) * size_of::<Vec<u8>>()
            + (last_frame.capacity() + pending_delta.capacity() + pending_run.capacity())
                * size_of::<u32>()
            + last_offset.capacity() * size_of::<u64>();
        if allocated > limit {
            return None;
        }
        ids.resize_with(count, Vec::new);
        offs.resize_with(count, Vec::new);
        last_frame.resize(count, NO_FRAME);
        last_offset.resize(count, 0);
        pending_delta.resize(count, 0);
        pending_run.resize(count, 0);
        Some(Self {
            frames: Vec::new(),
            ids,
            offs,
            limit,
            allocated,
            open: false,
            current: 0,
            last_group: NO_GROUP,
            last_frame,
            last_offset,
            pending_delta,
            pending_run,
            #[cfg(test)]
            frames_scanned: std::sync::atomic::AtomicUsize::new(0),
            #[cfg(test)]
            payloads_decoded: std::sync::atomic::AtomicUsize::new(0),
            #[cfg(test)]
            payloads_skipped: std::sync::atomic::AtomicUsize::new(0),
            #[cfg(test)]
            groups_selected: std::sync::atomic::AtomicUsize::new(0),
        })
    }

    /// False discards the optional index; body validation must still finish.
    pub(super) fn push(&mut self, rank: usize, time: Time, offset: u64, cyclic: bool) -> bool {
        if self.last_frame.is_empty() {
            return false;
        }
        if !self.open {
            if !self.grow_frames() {
                return false;
            }
            let Ok(current) = u32::try_from(self.frames.len()) else {
                return false;
            };
            self.frames.push(Frame { time, cyclic });
            self.current = current;
            self.open = true;
            self.last_group = NO_GROUP;
        }
        let group = rank / RANKS_PER_GROUP;
        if group == self.last_group {
            return true;
        }
        self.last_group = group;
        self.record(group, rank, offset)
    }

    /// Close the current frame so the next record starts a new one.
    pub(super) fn list_end(&mut self) {
        self.open = false;
        self.last_group = NO_GROUP;
    }

    /// Flush pending runs and release build-only arrays. False means the
    /// representation could not be completed and the whole index must be
    /// discarded: an incomplete index is never kept.
    pub(super) fn finish(&mut self) -> bool {
        if self.last_frame.is_empty() {
            return true;
        }
        for group in 0..self.ids.len() {
            if !self.flush(group) {
                return false;
            }
        }
        self.last_frame = Vec::new();
        self.last_offset = Vec::new();
        self.pending_delta = Vec::new();
        self.pending_run = Vec::new();
        self.allocated = self.retained();
        true
    }

    /// Bytes of the retained representation.
    fn retained(&self) -> usize {
        self.frames.capacity() * size_of::<Frame>()
            + (self.ids.capacity() + self.offs.capacity()) * size_of::<Vec<u8>>()
            + self.ids.iter().map(Vec::capacity).sum::<usize>()
            + self.offs.iter().map(Vec::capacity).sum::<usize>()
    }

    /// Retained bytes of the completed representation.
    pub(super) fn retained_bytes(&self) -> usize {
        self.retained()
    }

    #[cfg(test)]
    pub(super) fn work(&self) -> Work {
        Work {
            frames_scanned: self.frames_scanned.load(Ordering::Relaxed),
            payloads_decoded: self.payloads_decoded.load(Ordering::Relaxed),
            payloads_skipped: self.payloads_skipped.load(Ordering::Relaxed),
            groups_selected: self.groups_selected.load(Ordering::Relaxed),
        }
    }

    #[cfg(test)]
    fn set_limit(&mut self, limit: usize) {
        self.limit = limit;
    }

    fn grow_frames(&mut self) -> bool {
        if self.frames.len() < self.frames.capacity() {
            return true;
        }
        let remaining = self.limit - self.allocated;
        // Geometric growth keeps the build amortized linear; the reserve is
        // capped by the remaining budget, so slack stays bounded.
        let chunk = if self.frames.is_empty() {
            1
        } else {
            self.frames.capacity().div_ceil(2).max(FRAME_CHUNK)
        };
        let chunk = chunk.min(remaining / size_of::<Frame>());
        if chunk == 0 {
            return false;
        }
        let before = self.frames.capacity();
        if self.frames.try_reserve_exact(chunk).is_err() {
            return false;
        }
        self.allocated += (self.frames.capacity() - before) * size_of::<Frame>();
        self.allocated <= self.limit
    }

    fn grow_stream(
        stream: &mut Vec<u8>,
        needed: usize,
        limit: usize,
        allocated: &mut usize,
    ) -> bool {
        if stream.len() + needed <= stream.capacity() {
            return true;
        }
        let remaining = limit - *allocated;
        if needed > remaining {
            return false;
        }
        // Small streams start small: one chunk per group would cost more than
        // the positions it stores.
        let chunk = needed
            .max(stream.capacity().div_ceil(2))
            .max(STREAM_START)
            .min(remaining);
        let before = stream.capacity();
        if stream.try_reserve_exact(chunk).is_err() {
            return false;
        }
        *allocated += stream.capacity() - before;
        *allocated <= limit
    }

    /// Record the first pair of one group in the current frame.
    fn record(&mut self, group: usize, rank: usize, offset: u64) -> bool {
        let frame = self.current;
        let within = (rank % RANKS_PER_GROUP) as u64;
        let mut bytes = [0u8; 20];
        let mut used = 0;
        if self.last_frame[group] == NO_FRAME {
            // The first frame number and offset of a group are absolute.
            used += encode(u64::from(frame), &mut bytes[used..]);
        } else {
            let delta = frame - self.last_frame[group];
            if delta == self.pending_delta[group] {
                self.pending_run[group] += 1;
            } else {
                if !self.flush(group) {
                    return false;
                }
                self.pending_delta[group] = delta;
                self.pending_run[group] = 1;
            }
        }
        let mut position = [0u8; 20];
        let offset_used = encode(offset - self.last_offset[group], &mut position);
        let position_used = offset_used + encode(within, &mut position[offset_used..]);
        if !Self::grow_stream(
            &mut self.offs[group],
            position_used,
            self.limit,
            &mut self.allocated,
        ) || !Self::grow_stream(&mut self.ids[group], used, self.limit, &mut self.allocated)
        {
            return false;
        }
        self.offs[group].extend_from_slice(&position[..position_used]);
        self.ids[group].extend_from_slice(&bytes[..used]);
        self.last_frame[group] = frame;
        self.last_offset[group] = offset;
        true
    }

    /// Write the pending run of one group as a delta and repeat count.
    fn flush(&mut self, group: usize) -> bool {
        let run = self.pending_run[group];
        if run == 0 {
            return true;
        }
        let mut bytes = [0u8; 10];
        let used = encode(u64::from(self.pending_delta[group]), &mut bytes);
        let used = used + encode(u64::from(run), &mut bytes[used..]);
        if !Self::grow_stream(&mut self.ids[group], used, self.limit, &mut self.allocated) {
            return false;
        }
        self.ids[group].extend_from_slice(&bytes[..used]);
        self.pending_run[group] = 0;
        true
    }

    /// Read the selected recipes from validated source positions.
    ///
    /// `None` means the query scratch could not be reserved and the caller
    /// must use the sequential walker instead.
    pub(super) fn read<B>(
        &self,
        wire: &mut Wire,
        types: &Types,
        active: &[(u32, usize)],
        ids: &[u32],
        end: Time,
        mut visitor: impl FnMut(body::Event) -> ControlFlow<B>,
    ) -> Result<Option<ControlFlow<B>>> {
        #[cfg(test)]
        {
            self.frames_scanned.store(0, Ordering::Relaxed);
            self.payloads_decoded.store(0, Ordering::Relaxed);
            self.payloads_skipped.store(0, Ordering::Relaxed);
            self.groups_selected.store(0, Ordering::Relaxed);
        }
        let mut ranks = Vec::new();
        if ranks.try_reserve_exact(ids.len()).is_err() {
            return Ok(None);
        }
        for &id in ids {
            let rank = active
                .binary_search_by_key(&id, |&(id, _)| id)
                .map_err(|_| wire.malformed("inactive private source ID"))?;
            ranks.push(rank);
        }
        ranks.sort_unstable();
        ranks.dedup();
        if ranks.len() > u32::MAX as usize {
            return Ok(None);
        }
        let mut cursors = Vec::new();
        if cursors.try_reserve_exact(ranks.len()).is_err() {
            return Ok(None);
        }
        // One cursor per group, carrying the group's own last selected rank so
        // a scan never decodes trailing payloads after it.
        let mut position = 0;
        while position < ranks.len() {
            let group = ranks[position] / RANKS_PER_GROUP;
            let base = group * RANKS_PER_GROUP;
            let mut last = position;
            let mut mask = 0u64;
            loop {
                mask |= 1u64 << (ranks[last] - base);
                if last + 1 >= ranks.len() || ranks[last + 1] / RANKS_PER_GROUP != group {
                    break;
                }
                last += 1;
            }
            // Route index of this group's first selected rank in the query's
            // ascending selected-rank list; a scan adds the number of selected ranks
            // below the one it reports, so the consumer resolves its range without a
            // search.
            let Ok(first_route) = u32::try_from(position) else {
                return Ok(None);
            };
            position = last + 1;
            if self.ids[group].is_empty() {
                continue;
            }
            cursors.push(Cursor {
                group,
                mask,
                first_route,
                last: ranks[last],
                ids: 0,
                offs: 0,
                started: false,
                frame: 0,
                delta: 0,
                remaining: 0,
                offset: 0,
                first: 0,
            });
        }
        #[cfg(test)]
        {
            self.groups_selected.store(cursors.len(), Ordering::Relaxed);
        }
        let mut heap = BinaryHeap::new();
        if heap.try_reserve(cursors.len()).is_err() {
            return Ok(None);
        }
        for (slot, cursor) in cursors.iter_mut().enumerate() {
            if self.advance(cursor) {
                heap.push(Reverse((cursor.frame, slot)));
            }
        }
        let mut pending = Vec::new();
        if pending.try_reserve_exact(cursors.len()).is_err() {
            return Ok(None);
        }
        let mut current = None;
        while let Some(Reverse((frame, _))) = heap.peek().copied() {
            let stored = self.frames[frame as usize];
            if stored.time > end {
                break;
            }
            if let Some(previous) = current
                && previous != stored.time
                && let ControlFlow::Break(value) = tick(&mut visitor, previous)
            {
                return Ok(Some(ControlFlow::Break(value)));
            }
            current = Some(stored.time);
            pending.clear();
            while let Some(Reverse((next, slot))) = heap.peek().copied() {
                if next != frame {
                    break;
                }
                heap.pop();
                pending.push(slot);
            }
            for &slot in &pending {
                let cursor = &cursors[slot];
                let sources = Sources { active, types };
                if let ControlFlow::Break(value) =
                    self.scan(wire, sources, stored, cursor, &mut visitor)?
                {
                    return Ok(Some(ControlFlow::Break(value)));
                }
            }
            for &slot in &pending {
                if self.advance(&mut cursors[slot]) {
                    heap.push(Reverse((cursors[slot].frame, slot)));
                }
            }
        }
        if let Some(time) = current
            && let ControlFlow::Break(value) = tick(&mut visitor, time)
        {
            return Ok(Some(ControlFlow::Break(value)));
        }
        Ok(Some(ControlFlow::Continue(())))
    }

    /// Decode the next frame number and position of one group.
    fn advance(&self, cursor: &mut Cursor) -> bool {
        let ids = &self.ids[cursor.group];
        let mut position = cursor.ids as usize;
        if !cursor.started {
            let Some(first) = decode(ids, &mut position) else {
                return false;
            };
            let Ok(first) = u32::try_from(first) else {
                return false;
            };
            cursor.frame = first;
            cursor.started = true;
        } else if cursor.remaining > 0 {
            cursor.remaining -= 1;
            cursor.frame += cursor.delta;
        } else {
            if position >= ids.len() {
                return false;
            }
            let (Some(delta), Some(run)) = (decode(ids, &mut position), decode(ids, &mut position))
            else {
                return false;
            };
            let (Ok(delta), Ok(run)) = (u32::try_from(delta), u32::try_from(run)) else {
                return false;
            };
            if run == 0 {
                return false;
            }
            cursor.delta = delta;
            cursor.remaining = run - 1;
            cursor.frame += delta;
        }
        cursor.ids = position as u32;
        let offs = &self.offs[cursor.group];
        let mut position = cursor.offs as usize;
        if position >= offs.len() {
            return false;
        }
        let (Some(delta), Some(first)) = (decode(offs, &mut position), decode(offs, &mut position))
        else {
            return false;
        };
        cursor.offset += delta;
        cursor.first = first as usize;
        cursor.offs = position as u32;
        true
    }

    /// Decode the records of one group in one frame, reporting selected ranks.
    fn scan<B>(
        &self,
        wire: &mut Wire,
        sources: Sources<'_>,
        frame: Frame,
        cursor: &Cursor,
        visitor: &mut impl FnMut(body::Event) -> ControlFlow<B>,
    ) -> Result<ControlFlow<B>> {
        let last = cursor.last;
        let base = cursor.group * RANKS_PER_GROUP;
        let group_end = (base + RANKS_PER_GROUP).min(sources.active.len());
        let mut rank = base + cursor.first;
        if rank >= group_end || rank > last {
            return Ok(ControlFlow::Continue(()));
        }
        #[cfg(test)]
        {
            self.frames_scanned.fetch_add(1, Ordering::Relaxed);
        }
        wire.seek_value(cursor.offset)?;
        loop {
            let (id, ty) = sources.active[rank];
            let local = rank - base;
            let bit = 1u64 << local;
            if cursor.mask & bit != 0 {
                let offset = wire.offset;
                let value = body::scalar(wire, sources.types, ty)?;
                #[cfg(test)]
                {
                    self.payloads_decoded.fetch_add(1, Ordering::Relaxed);
                }
                // Selected ranks are numbered in the query's ascending list, so the
                // count of selected ranks below this one is its offset in that list.
                let route = cursor.first_route + (cursor.mask & (bit - 1)).count_ones();
                if let ControlFlow::Break(value) = visitor(body::Event::Scalar {
                    id,
                    rank,
                    time: frame.time,
                    offset,
                    value,
                    cyclic: frame.cyclic,
                    route,
                }) {
                    return Ok(ControlFlow::Break(value));
                }
            } else {
                skip(wire, sources.types, ty)?;
                #[cfg(test)]
                {
                    self.payloads_skipped.fetch_add(1, Ordering::Relaxed);
                }
            }
            if !frame.cyclic {
                rank += 1;
            } else {
                let delta = wire.uleb()? as usize;
                if delta == 0 {
                    break;
                }
                rank = rank
                    .checked_add(delta)
                    .ok_or_else(|| wire.malformed("cycle scalar rank overflow"))?;
            }
            if rank >= group_end || rank >= sources.active.len() || rank > last {
                break;
            }
        }
        Ok(ControlFlow::Continue(()))
    }
}

/// Read-only inputs shared by every frame of one query.
#[derive(Clone, Copy)]
struct Sources<'a> {
    active: &'a [(u32, usize)],
    types: &'a Types,
}

/// Decoding state of one group's position streams.
struct Cursor {
    group: usize,
    /// Selected local ranks of this group, one bit per rank.
    mask: u64,
    /// Route index of this group's first selected rank in the query's ascending
    /// selected-rank list.
    first_route: u32,
    /// Largest selected rank of this group.
    last: usize,
    ids: u32,
    offs: u32,
    started: bool,
    frame: u32,
    delta: u32,
    remaining: u32,
    offset: u64,
    first: usize,
}

/// Skip one validated payload without building its value.
fn skip(wire: &mut Wire, types: &Types, ty: usize) -> Result<()> {
    let (storage, _, _) = types
        .scalar(ty)
        .ok_or_else(|| wire.malformed("non-scalar body type"))?;
    match storage {
        Storage::B2 | Storage::E8 => {
            wire.byte()?;
        }
        Storage::F64 => {
            wire.fixed::<8>()?;
        }
        Storage::I32 | Storage::I64 | Storage::P32 | Storage::P64 => loop {
            if wire.byte()? & 0x80 == 0 {
                break;
            }
        },
    }
    Ok(())
}

fn tick<B>(visitor: &mut impl FnMut(body::Event) -> ControlFlow<B>, time: Time) -> ControlFlow<B> {
    if let ControlFlow::Break(value) = visitor(body::Event::ListEnd(time)) {
        return ControlFlow::Break(value);
    }
    visitor(body::Event::Tick(time))
}

fn encode(mut value: u64, out: &mut [u8]) -> usize {
    let mut used = 0;
    while value >= 0x80 {
        out[used] = (value as u8) | 0x80;
        value >>= 7;
        used += 1;
    }
    out[used] = value as u8;
    used + 1
}

fn decode(bytes: &[u8], position: &mut usize) -> Option<u64> {
    let mut value = 0u64;
    for group in 0..10 {
        let byte = *bytes.get(*position)?;
        *position += 1;
        value |= u64::from(byte & 0x7f) << (7 * group);
        if byte & 0x80 == 0 {
            return Some(value);
        }
    }
    None
}

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

    fn push_frame(index: &mut Index, time: u64, ranks: &[usize]) {
        for &rank in ranks {
            assert!(index.push(
                rank,
                Time::from_ticks(time),
                100 * time + rank as u64,
                false
            ));
        }
        index.list_end();
    }

    /// Collect one group's `(frame, offset, first rank)` triples in source order.
    fn pairs(index: &Index, group: usize) -> Vec<(u32, u64, usize)> {
        let mut cursor = Cursor {
            group,
            mask: u64::MAX,
            first_route: 0,
            last: usize::MAX,
            ids: 0,
            offs: 0,
            started: false,
            frame: 0,
            delta: 0,
            remaining: 0,
            offset: 0,
            first: 0,
        };
        let mut pairs = Vec::new();
        while index.advance(&mut cursor) {
            pairs.push((cursor.frame, cursor.offset, cursor.first));
        }
        pairs
    }

    #[test]
    fn one_pair_per_group_and_frame_carries_its_own_position() {
        let mut index = Index::with_limit(2 * RANKS_PER_GROUP, 1 << 24).unwrap();
        push_frame(&mut index, 0, &[3, 9, 70]);
        push_frame(&mut index, 5, &[3, 70, 71]);
        push_frame(&mut index, 9, &[70]);
        assert!(index.finish());
        // Ranks 3 and 9 share group 0 and frame 0: only the run start is kept.
        assert_eq!(pairs(&index, 0), vec![(0, 3, 3), (1, 503, 3)]);
        assert_eq!(pairs(&index, 1), vec![(0, 70, 6), (1, 570, 6), (2, 970, 6)]);
        assert!(index.retained_bytes() <= index.limit);
    }

    #[test]
    fn repeated_stride_stays_lossless_and_compact() {
        let mut index = Index::with_limit(RANKS_PER_GROUP, 1 << 24).unwrap();
        for frame in 0..100u64 {
            push_frame(&mut index, frame, &[0, 1]);
        }
        assert!(index.finish());
        let expected = (0..100)
            .map(|frame| (frame as u32, 0, 0))
            .collect::<Vec<_>>();
        let actual = pairs(&index, 0)
            .into_iter()
            .map(|(frame, _, first)| (frame, 0, first))
            .collect::<Vec<_>>();
        assert_eq!(actual, expected);
        // Frame numbers use runs; positions stay one pair each.
        assert!(index.ids[0].len() < 8, "{}", index.ids[0].len());
        assert!(index.offs[0].len() >= 100, "{}", index.offs[0].len());
        assert!(index.offs[0].len() <= 200, "{}", index.offs[0].len());
    }

    #[test]
    fn oversized_active_table_is_refused_before_any_reserve() {
        // 600 M ranks is 9 375 000 groups and 637.5 MB of per-group arrays,
        // above the 512 MiB budget, so construction must refuse quickly.
        assert!(Index::new(600_000_000).is_none());
        assert!(Index::with_limit(600_000_000, 1024).is_none());
        // The preflight is exact: one byte below the aggregate is refused too.
        let groups = 600_000_000usize.div_ceil(RANKS_PER_GROUP);
        let aggregate = groups.checked_mul(GROUPS_PER_ELEMENT_BYTES).unwrap();
        assert!(Index::with_limit(600_000_000, aggregate - 1).is_none());
    }

    #[test]
    fn failed_finish_never_yields_an_incomplete_index() {
        let mut index = Index::with_limit(RANKS_PER_GROUP, 1 << 24).unwrap();
        // Alternating strides flush runs during the build; the last run stays
        // pending until `finish`.
        for frame in 0..16u64 {
            push_frame(&mut index, frame * 2 + frame % 2, &[0]);
        }
        // The pending run is only written by `finish`: fill the stream capacity
        // exactly and refuse any growth, so completion cannot succeed.
        let group = 0;
        index.ids[group].shrink_to_fit();
        index.set_limit(index.allocated);
        assert!(!index.finish());
        // The caller must discard the index; nothing is silently completed.
        assert!(!index.ids[group].is_empty());
    }

    #[test]
    fn budget_counts_every_retained_capacity_and_rejects_growth() {
        let limit = 1 << 20;
        let mut index = Index::with_limit(RANKS_PER_GROUP, limit).unwrap();
        for frame in 0..64u64 {
            push_frame(&mut index, frame, &[0, 1]);
        }
        assert!(index.finish());
        let independent = index.frames.capacity() * size_of::<Frame>()
            + (index.ids.capacity() + index.offs.capacity()) * size_of::<Vec<u8>>()
            + index.ids.iter().map(Vec::capacity).sum::<usize>()
            + index.offs.iter().map(Vec::capacity).sum::<usize>();
        assert_eq!(independent, index.retained());
        assert!(index.retained() <= limit);

        // A representation that cannot fit is rejected without overshoot.
        let mut index = Index::with_limit(RANKS_PER_GROUP, 64 * 1024).unwrap();
        let mut frame = 0u64;
        while index.push(0, Time::from_ticks(frame), frame, false) {
            index.list_end();
            frame += 1;
            assert!(frame < 100_000);
        }
        assert!(frame > 0);
        // A refused growth is bounded by the last reserve step, not unbounded.
        assert!(index.retained() <= 64 * 1024 + 2 * index.frames.capacity() * size_of::<Frame>());
    }
}