asap_sketchlib 0.2.1

A high-performance sketching library for approximate stream processing
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
//! OctoSketch multi-threaded sketch framework.
//!
//! Implements the parent-child delta-promotion architecture from OctoSketch (NSDI 2024).
//! Worker threads maintain lightweight child sketches with small counters and emit
//! compact delta entries via an MPSC channel when counters overflow a promotion
//! threshold. An aggregator thread applies deltas to a full-precision parent sketch.
//!
//! Reference:
//! - <https://www.usenix.org/conference/nsdi24/presentation/zhang-yinda>

#[cfg(feature = "octo-runtime")]
use std::marker::PhantomData;
#[cfg(feature = "octo-runtime")]
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
#[cfg(feature = "octo-runtime")]
use std::sync::{Arc, RwLock, Weak};
#[cfg(feature = "octo-runtime")]
use std::thread;

#[cfg(feature = "octo-runtime")]
use crossbeam_channel::{Receiver, Sender, TryRecvError, bounded};

use crate::{
    Classic, CmDelta, Count, CountDelta, CountMin, DataInput, HllDelta, HyperLogLog, RegularPath,
    Vector2D,
};

#[cfg(feature = "octo-runtime")]
/// Legacy queue capacity default retained for config compatibility.
const DEFAULT_QUEUE_CAPACITY: usize = 65536;

// ---------------------------------------------------------------------------
// Traits
// ---------------------------------------------------------------------------

/// Worker-side trait: processes inputs and emits deltas.
pub trait OctoWorker: Send {
    /// Delta type emitted by the worker.
    type Delta: Copy + Send + 'static;

    /// Process one input and emit zero or more deltas.
    fn process<F>(&mut self, input: &DataInput, emit: &mut F)
    where
        F: FnMut(Self::Delta);
}

/// Aggregator-side trait: absorbs deltas into a full-precision sketch.
pub trait OctoAggregator: Send {
    /// Delta type consumed by the aggregator.
    type Delta: Copy + Send + 'static;

    /// Apply a single delta to the parent sketch.
    fn apply(&mut self, delta: Self::Delta);
}

// ---------------------------------------------------------------------------
// Configuration & Runtime (requires "octo-runtime" feature)
// ---------------------------------------------------------------------------

#[cfg(feature = "octo-runtime")]
/// Configuration for `run_octo`.
pub struct OctoConfig {
    /// Number of worker threads (default: 4).
    pub num_workers: usize,
    /// Pin worker threads to cores (default: true).
    /// Worker i is pinned to core i, aggregator to core num_workers.
    /// Silently skipped if pinning fails.
    pub pin_cores: bool,
    /// Queue capacity for bounded worker-input and worker-delta channels (default: 65536).
    pub queue_capacity: usize,
}

#[cfg(feature = "octo-runtime")]
impl Default for OctoConfig {
    fn default() -> Self {
        Self {
            num_workers: 4,
            pin_cores: true,
            queue_capacity: DEFAULT_QUEUE_CAPACITY,
        }
    }
}

#[cfg(feature = "octo-runtime")]
/// Result of an `run_octo` execution.
pub struct OctoResult<P> {
    /// Final parent sketch after all deltas are applied.
    pub parent: P,
}

#[cfg(feature = "octo-runtime")]
enum WorkerMsg {
    Data(DataInput<'static>),
    End,
}

#[cfg(feature = "octo-runtime")]
/// Extends a `DataInput` lifetime to `'static` for cross-thread transport in
/// streaming mode. Caller must ensure all borrowed data outlives worker processing.
#[inline(always)]
unsafe fn assume_input_static(input: DataInput<'_>) -> DataInput<'static> {
    // SAFETY: enforced by caller contract described above.
    unsafe { std::mem::transmute::<DataInput<'_>, DataInput<'static>>(input) }
}

#[cfg(feature = "octo-runtime")]
/// Streaming Octo runtime that accepts incremental inserts and finalizes into a parent sketch.
pub struct OctoRuntime<W, P>
where
    W: OctoWorker + 'static,
    P: OctoAggregator<Delta = W::Delta> + Send + Sync + 'static,
{
    core: Option<OctoCore<P>>,
    _worker_marker: PhantomData<W>,
}

#[cfg(feature = "octo-runtime")]
/// Read-only handle for querying the live aggregator state while runtime is active.
pub struct OctoReadHandle<P> {
    parent: Weak<RwLock<P>>,
}

#[cfg(feature = "octo-runtime")]
impl<P> Clone for OctoReadHandle<P> {
    fn clone(&self) -> Self {
        Self {
            parent: Weak::clone(&self.parent),
        }
    }
}

#[cfg(feature = "octo-runtime")]
impl<P> OctoReadHandle<P> {
    /// Executes a read-only closure over the live parent state.
    pub fn with_parent<R>(&self, f: impl FnOnce(&P) -> R) -> R {
        let parent = self
            .parent
            .upgrade()
            .expect("Octo runtime has been finished and parent state was dropped");
        let guard = parent.read().expect("parent lock poisoned");
        f(&guard)
    }
}

#[cfg(feature = "octo-runtime")]
struct OctoCore<P> {
    worker_input_txs: Vec<Sender<WorkerMsg>>,
    next_worker: AtomicUsize,
    worker_handles: Vec<thread::JoinHandle<()>>,
    aggregator_handle: Option<thread::JoinHandle<()>>,
    parent: Arc<RwLock<P>>,
    closed: AtomicBool,
}

#[cfg(feature = "octo-runtime")]
impl<P> OctoCore<P> {
    fn read_handle(&self) -> OctoReadHandle<P> {
        OctoReadHandle {
            parent: Arc::downgrade(&self.parent),
        }
    }

    fn close(&self) {
        if self.closed.swap(true, Ordering::AcqRel) {
            return;
        }
        for tx in &self.worker_input_txs {
            let _ = tx.send(WorkerMsg::End);
        }
    }
}

#[cfg(feature = "octo-runtime")]
impl<P> OctoCore<P> {
    fn into_parent(mut self) -> P {
        self.close();

        for handle in self.worker_handles.drain(..) {
            handle.join().expect("worker thread panicked during finish");
        }

        if let Some(aggregator) = self.aggregator_handle.take() {
            aggregator
                .join()
                .expect("aggregator thread panicked during finish");
        }

        let parent_lock = match Arc::try_unwrap(self.parent) {
            Ok(lock) => lock,
            Err(_) => panic!("Octo parent still has external strong references at finish"),
        };
        parent_lock.into_inner().expect("parent lock poisoned")
    }
}

#[cfg(feature = "octo-runtime")]
impl<P> OctoCore<P>
where
    P: Send + Sync + 'static,
{
    fn start<W>(
        workers: Vec<W>,
        parent: P,
        num_workers: usize,
        pin_cores: bool,
        queue_capacity: usize,
    ) -> Self
    where
        W: OctoWorker + 'static,
        P: OctoAggregator<Delta = W::Delta>,
    {
        assert_eq!(workers.len(), num_workers);
        let queue_capacity = queue_capacity.max(1);

        let parent = Arc::new(RwLock::new(parent));
        let parent_for_aggregator = Arc::clone(&parent);
        let mut delta_txs: Vec<Sender<W::Delta>> = Vec::with_capacity(num_workers);
        let mut delta_rxs: Vec<Option<Receiver<W::Delta>>> = Vec::with_capacity(num_workers);
        for _ in 0..num_workers {
            let (tx, rx) = bounded::<W::Delta>(queue_capacity);
            delta_txs.push(tx);
            delta_rxs.push(Some(rx));
        }

        let aggregator_handle = thread::spawn(move || {
            if pin_cores {
                let _ = core_affinity::set_for_current(core_affinity::CoreId { id: num_workers });
            }

            let mut disconnected_workers = 0usize;
            while disconnected_workers < num_workers {
                let mut made_progress = false;
                for rx_slot in &mut delta_rxs {
                    let Some(rx) = rx_slot else {
                        continue;
                    };
                    match rx.try_recv() {
                        Ok(delta) => {
                            let mut guard = parent_for_aggregator
                                .write()
                                .expect("parent lock poisoned in aggregator");
                            guard.apply(delta);
                            made_progress = true;
                        }
                        Err(TryRecvError::Empty) => {}
                        Err(TryRecvError::Disconnected) => {
                            *rx_slot = None;
                            disconnected_workers += 1;
                        }
                    }
                }
                if !made_progress {
                    std::hint::spin_loop();
                }
            }
        });

        let mut worker_input_txs = Vec::with_capacity(num_workers);
        let mut worker_handles = Vec::with_capacity(num_workers);
        for (worker_id, (mut worker, delta_tx_worker)) in
            workers.into_iter().zip(delta_txs).enumerate()
        {
            let (worker_tx, worker_rx) = bounded::<WorkerMsg>(queue_capacity);
            worker_input_txs.push(worker_tx);
            worker_handles.push(thread::spawn(move || {
                if pin_cores {
                    let _ = core_affinity::set_for_current(core_affinity::CoreId { id: worker_id });
                }
                while let Ok(msg) = worker_rx.recv() {
                    match msg {
                        WorkerMsg::Data(input) => worker.process(&input, &mut |delta| {
                            delta_tx_worker
                                .send(delta)
                                .expect("aggregator receiver dropped while workers still running");
                        }),
                        WorkerMsg::End => break,
                    }
                }
            }));
        }

        Self {
            worker_input_txs,
            next_worker: AtomicUsize::new(0),
            worker_handles,
            aggregator_handle: Some(aggregator_handle),
            parent,
            closed: AtomicBool::new(false),
        }
    }
}

#[cfg(feature = "octo-runtime")]
impl<W, P> OctoRuntime<W, P>
where
    W: OctoWorker + 'static,
    P: OctoAggregator<Delta = W::Delta> + Send + Sync + 'static,
{
    pub fn new<F, PF>(config: &OctoConfig, worker_factory: F, parent_factory: PF) -> Self
    where
        F: Fn(usize) -> W,
        PF: FnOnce() -> P,
    {
        let num_workers = config.num_workers.max(1);
        let workers: Vec<W> = (0..num_workers).map(worker_factory).collect();
        let parent = parent_factory();
        let core = OctoCore::start(
            workers,
            parent,
            num_workers,
            config.pin_cores,
            config.queue_capacity,
        );

        Self {
            core: Some(core),
            _worker_marker: PhantomData,
        }
    }

    pub fn read_handle(&self) -> OctoReadHandle<P> {
        self.core
            .as_ref()
            .expect("runtime core missing")
            .read_handle()
    }

    pub fn close(&self) {
        self.core.as_ref().expect("runtime core missing").close();
    }

    pub fn insert(&mut self, input: DataInput<'_>) {
        let core = self.core.as_ref().expect("runtime core missing");
        if core.closed.load(Ordering::Acquire) {
            panic!("cannot insert after runtime has been closed");
        }

        let worker_id =
            core.next_worker.fetch_add(1, Ordering::AcqRel) % core.worker_input_txs.len();
        // SAFETY: caller explicitly guarantees borrowed data lives long enough.
        let static_input = unsafe { assume_input_static(input) };
        core.worker_input_txs[worker_id]
            .send(WorkerMsg::Data(static_input))
            .expect("worker receiver dropped while runtime is active");
    }

    pub fn insert_batch(&mut self, inputs: &[DataInput<'_>]) {
        for input in inputs {
            self.insert(input.clone());
        }
    }

    pub fn finish(mut self) -> OctoResult<P> {
        let parent = self
            .core
            .take()
            .expect("runtime core missing")
            .into_parent();

        OctoResult { parent }
    }
}

// ---------------------------------------------------------------------------
// Concrete worker/parent implementations
// ---------------------------------------------------------------------------

// -- CountMin --

/// OctoSketch worker backed by `CountMin`.
pub struct CmOctoWorker {
    sketch: CountMin<Vector2D<i32>, RegularPath>,
}

impl CmOctoWorker {
    /// Creates a Count-Min-backed Octo worker.
    pub fn new(rows: usize, cols: usize) -> Self {
        Self {
            sketch: CountMin::with_dimensions(rows, cols),
        }
    }
}

impl OctoWorker for CmOctoWorker {
    type Delta = CmDelta;

    #[inline(always)]
    fn process<F>(&mut self, input: &DataInput, emit: &mut F)
    where
        F: FnMut(Self::Delta),
    {
        self.sketch.insert_emit_delta(input, emit);
    }
}

/// OctoSketch parent wrapping a full-precision `CountMin`.
pub struct CmOctoAggregator {
    /// Parent Count-Min sketch updated by worker deltas.
    pub sketch: CountMin<Vector2D<i32>, RegularPath>,
}

impl OctoAggregator for CmOctoAggregator {
    type Delta = CmDelta;

    #[inline(always)]
    fn apply(&mut self, delta: CmDelta) {
        self.sketch.apply_delta(delta);
    }
}

// -- Count Sketch --

/// OctoSketch worker backed by `Count`.
pub struct CountOctoWorker {
    child: Count<Vector2D<i32>, RegularPath>,
}

impl CountOctoWorker {
    /// Creates a Count-Sketch-backed Octo worker.
    pub fn new(rows: usize, cols: usize) -> Self {
        Self {
            child: Count::with_dimensions(rows, cols),
        }
    }
}

impl OctoWorker for CountOctoWorker {
    type Delta = CountDelta;

    #[inline(always)]
    fn process<F>(&mut self, input: &DataInput, emit: &mut F)
    where
        F: FnMut(Self::Delta),
    {
        self.child.insert_emit_delta(input, emit);
    }
}

/// OctoSketch parent wrapping a full-precision `Count`.
pub struct CountOctoAggregator {
    /// Parent Count Sketch updated by worker deltas.
    pub sketch: Count<Vector2D<i32>, RegularPath>,
}

impl OctoAggregator for CountOctoAggregator {
    type Delta = CountDelta;

    #[inline(always)]
    fn apply(&mut self, delta: CountDelta) {
        self.sketch.apply_delta(delta);
    }
}

// -- HyperLogLog --

/// OctoSketch worker backed by `HyperLogLog`.
pub struct HllOctoWorker {
    child: HyperLogLog<Classic>,
}

impl HllOctoWorker {
    /// Creates a HyperLogLog-backed Octo worker.
    pub fn new() -> Self {
        Self {
            child: HyperLogLog::default(),
        }
    }
}

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

impl OctoWorker for HllOctoWorker {
    type Delta = HllDelta;

    #[inline(always)]
    fn process<F>(&mut self, input: &DataInput, emit: &mut F)
    where
        F: FnMut(Self::Delta),
    {
        self.child.insert_emit_delta(input, emit);
    }
}

/// OctoSketch parent wrapping a full-precision `HyperLogLog<Classic>`.
pub struct HllOctoAggregator {
    /// Parent HyperLogLog sketch updated by worker deltas.
    pub sketch: HyperLogLog<Classic>,
}

impl OctoAggregator for HllOctoAggregator {
    type Delta = HllDelta;

    #[inline(always)]
    fn apply(&mut self, delta: HllDelta) {
        self.sketch.apply_delta(delta);
    }
}

// ---------------------------------------------------------------------------
// Core execution engine
// ---------------------------------------------------------------------------

#[cfg(feature = "octo-runtime")]
/// Runs the OctoSketch multi-threaded insert protocol.
///
/// 1. Dispatches `inputs` across workers round-robin through per-worker channels.
/// 2. Each worker maintains a child sketch, emitting deltas via an MPSC channel.
/// 3. The aggregator blocks on the channel and applies deltas to the parent.
/// 4. Returns the fully-merged parent sketch.
pub fn run_octo<W, P>(
    inputs: &[DataInput<'_>],
    config: &OctoConfig,
    worker_factory: impl Fn(usize) -> W,
    parent_factory: impl FnOnce() -> P,
) -> OctoResult<P>
where
    W: OctoWorker + 'static,
    P: OctoAggregator<Delta = W::Delta> + Send + Sync + 'static,
{
    let mut runtime = OctoRuntime::new(config, worker_factory, parent_factory);
    for input in inputs {
        runtime.insert(input.clone());
    }
    runtime.finish()
}

#[cfg(all(test, feature = "octo-runtime"))]
mod tests {
    use super::*;
    use crate::DataInput;

    // -----------------------------------------------------------------------
    // Layer 2 unit tests: child sketches + apply_delta
    // -----------------------------------------------------------------------

    #[test]
    fn cm_insert_emit_delta_emits_at_threshold() {
        let mut worker_sketch = CountMin::<Vector2D<i32>, RegularPath>::with_dimensions(3, 64);
        let key = DataInput::U64(42);
        let mut deltas: Vec<CmDelta> = Vec::new();

        // Insert CM_PROMASK-1 times: no delta yet.
        for _ in 0..(crate::CM_PROMASK - 1) {
            worker_sketch.insert_emit_delta(&key, &mut |d| deltas.push(d));
        }
        assert!(deltas.is_empty(), "should not emit before threshold");

        // One more insert triggers the delta.
        worker_sketch.insert_emit_delta(&key, &mut |d| deltas.push(d));
        assert_eq!(deltas.len(), 3, "should emit one delta per row (3 rows)");
        for d in &deltas {
            assert_eq!(d.value, crate::CM_PROMASK);
        }
    }

    #[test]
    fn cm_apply_delta_increments_parent() {
        let mut parent = CountMin::<Vector2D<i32>, RegularPath>::with_dimensions(3, 64);
        let delta = CmDelta {
            row: 1,
            col: 5,
            value: 100,
        };
        parent.apply_delta(delta);
        assert_eq!(parent.as_storage().query_one_counter(1, 5), 100);

        parent.apply_delta(delta);
        assert_eq!(parent.as_storage().query_one_counter(1, 5), 200);
    }

    #[test]
    fn count_child_emits_delta_at_threshold() {
        let mut child = Count::<Vector2D<i32>, RegularPath>::with_dimensions(3, 64);
        let key = DataInput::U64(99);
        let mut deltas: Vec<CountDelta> = Vec::new();

        // Insert enough times to trigger at least one delta.
        for _ in 0..200 {
            child.insert_emit_delta(&key, &mut |d| deltas.push(d));
        }
        // With COUNT_PROMASK = 63 and 3 rows, after 200 inserts we
        // should have emitted at least 3 deltas (one per row per threshold).
        assert!(
            deltas.len() >= 3,
            "expected at least 3 deltas, got {}",
            deltas.len()
        );
    }

    #[test]
    fn count_apply_delta_increments_parent() {
        let mut parent = Count::<Vector2D<i32>, RegularPath>::with_dimensions(3, 64);
        let delta = CountDelta {
            row: 0,
            col: 10,
            value: -50,
        };
        parent.apply_delta(delta);
        assert_eq!(parent.as_storage().query_one_counter(0, 10), -50);
    }

    #[test]
    fn hll_child_emits_delta_on_improvement() {
        let mut child = HyperLogLog::<Classic>::default();
        let mut deltas: Vec<HllDelta> = Vec::new();

        // First insert should always emit (register goes from 0 to something > 0).
        child.insert_emit_delta(&DataInput::U64(1), &mut |d| deltas.push(d));
        assert_eq!(deltas.len(), 1, "first insert should emit a delta");

        // Inserting the same value again should NOT emit (register unchanged).
        let len_before = deltas.len();
        child.insert_emit_delta(&DataInput::U64(1), &mut |d| deltas.push(d));
        assert_eq!(deltas.len(), len_before, "duplicate should not emit");
    }

    #[test]
    fn hll_apply_delta_updates_register() {
        let mut parent = HyperLogLog::<Classic>::default();
        let delta = HllDelta {
            pos: 100,
            value: 10,
        };
        parent.apply_delta(delta);

        // Apply a smaller value — should not change.
        let smaller = HllDelta { pos: 100, value: 5 };
        parent.apply_delta(smaller);

        // Apply a larger value — should update.
        let larger = HllDelta {
            pos: 100,
            value: 15,
        };
        parent.apply_delta(larger);
    }

    // -----------------------------------------------------------------------
    // Layer 3 integration tests: run_octo
    // -----------------------------------------------------------------------

    #[test]
    fn run_octo_cm_matches_single_thread() {
        let rows = 3;
        let cols = 4096;
        let n = 100_000u64;

        let inputs: Vec<DataInput<'_>> = (0..n).map(|i| DataInput::U64(i % 1024)).collect();

        // Single-threaded reference.
        let mut reference = CountMin::<Vector2D<i32>, RegularPath>::with_dimensions(rows, cols);
        for input in &inputs {
            reference.insert(input);
        }

        // Multi-threaded via OctoSketch.
        let config = OctoConfig {
            num_workers: 4,
            pin_cores: false, // don't pin in tests (CI may have few cores)
            queue_capacity: 8192,
        };
        let result = run_octo(
            &inputs,
            &config,
            |_| CmOctoWorker::new(rows, cols),
            || CmOctoAggregator {
                sketch: CountMin::with_dimensions(rows, cols),
            },
        );

        // Verify estimates are close.
        // OctoSketch child uses u8 counters, so there may be slight differences
        // due to the promotion threshold batching, but the total counts should match.
        for key_val in 0u64..1024 {
            let key = DataInput::U64(key_val);
            let ref_est = reference.estimate(&key);
            let octo_est = result.parent.sketch.estimate(&key);
            // The octo estimate should be <= reference (some counts may be
            // lost in the u8 remainder that didn't reach PROMASK).
            assert!(
                (ref_est - octo_est).abs() < 200,
                "key {key_val}: ref={ref_est}, octo={octo_est}, diff={}",
                (ref_est - octo_est).abs()
            );
        }
    }

    #[test]
    fn run_octo_hll_cardinality() {
        let n = 50_000u64;
        let inputs: Vec<DataInput<'_>> = (0..n).map(DataInput::U64).collect();

        let config = OctoConfig {
            num_workers: 4,
            pin_cores: false,
            queue_capacity: 16384,
        };

        let result = run_octo(
            &inputs,
            &config,
            |_| HllOctoWorker::new(),
            || HllOctoAggregator {
                sketch: HyperLogLog::<Classic>::default(),
            },
        );

        let estimate = result.parent.sketch.estimate();
        let truth = n as f64;
        let error = (estimate as f64 - truth).abs() / truth;
        assert!(
            error < 0.05,
            "HLL cardinality error {error:.4} exceeded 5% (truth {truth}, estimate {estimate})"
        );
    }

    #[test]
    fn run_octo_count_sketch_basic() {
        let rows = 3;
        let cols = 4096;
        let n = 50_000u64;

        let inputs: Vec<DataInput<'_>> = (0..n).map(|i| DataInput::U64(i % 512)).collect();

        let config = OctoConfig {
            num_workers: 2,
            pin_cores: false,
            queue_capacity: 8192,
        };

        let result = run_octo(
            &inputs,
            &config,
            |_| CountOctoWorker::new(rows, cols),
            || CountOctoAggregator {
                sketch: Count::with_dimensions(rows, cols),
            },
        );

        // Each key appears ~97 times. Check a sample of keys.
        let expected_per_key = (n / 512) as f64;
        for key_val in [0u64, 100, 255, 511] {
            let key = DataInput::U64(key_val);
            let est = result.parent.sketch.estimate(&key);
            assert!(
                (est - expected_per_key).abs() < expected_per_key * 0.5,
                "key {key_val}: estimate={est}, expected~{expected_per_key}"
            );
        }
    }

    #[test]
    fn run_octo_single_worker() {
        // Edge case: single worker should still work.
        let inputs: Vec<DataInput<'_>> = (0..1000u64).map(DataInput::U64).collect();

        let config = OctoConfig {
            num_workers: 1,
            pin_cores: false,
            queue_capacity: 4096,
        };

        let result = run_octo(
            &inputs,
            &config,
            |_| HllOctoWorker::new(),
            || HllOctoAggregator {
                sketch: HyperLogLog::<Classic>::default(),
            },
        );

        let estimate = result.parent.sketch.estimate();
        assert!(
            estimate > 900 && estimate < 1100,
            "single-worker HLL estimate {estimate} out of range for 1000 distinct keys"
        );
    }

    #[test]
    fn octo_runtime_streaming_cm_matches_batch_api() {
        let rows = 3;
        let cols = 4096;
        let n = 30_000u64;
        let inputs: Vec<DataInput<'_>> = (0..n).map(|i| DataInput::U64(i % 1024)).collect();
        let config = OctoConfig {
            num_workers: 4,
            pin_cores: false,
            queue_capacity: 8192,
        };

        let batch_result = run_octo(
            &inputs,
            &config,
            |_| CmOctoWorker::new(rows, cols),
            || CmOctoAggregator {
                sketch: CountMin::with_dimensions(rows, cols),
            },
        );

        let mut runtime = OctoRuntime::new(
            &config,
            move |_| CmOctoWorker::new(rows, cols),
            move || CmOctoAggregator {
                sketch: CountMin::with_dimensions(rows, cols),
            },
        );
        for input in &inputs {
            runtime.insert(input.clone());
        }
        let streaming_result = runtime.finish();

        for key_val in 0u64..128 {
            let key = DataInput::U64(key_val);
            let batch_est = batch_result.parent.sketch.estimate(&key);
            let stream_est = streaming_result.parent.sketch.estimate(&key);
            assert_eq!(batch_est, stream_est, "key {key_val} mismatch");
        }
    }

    #[test]
    fn octo_runtime_mixed_insert_and_batch_hll() {
        let config = OctoConfig {
            num_workers: 2,
            pin_cores: false,
            queue_capacity: 4096,
        };
        let mut runtime = OctoRuntime::new(
            &config,
            |_| HllOctoWorker::new(),
            || HllOctoAggregator {
                sketch: HyperLogLog::<Classic>::default(),
            },
        );

        runtime.insert(DataInput::U64(1));
        runtime.insert(DataInput::U64(2));
        let batch: Vec<DataInput<'_>> = (3..2000).map(DataInput::U64).collect();
        runtime.insert_batch(&batch);
        let result = runtime.finish();
        let estimate = result.parent.sketch.estimate();
        assert!(
            estimate > 1700 && estimate < 2300,
            "runtime mixed insert+batch estimate {estimate} is out of expected range"
        );
    }

    #[test]
    fn octo_runtime_live_read_handle_observes_progress() {
        let config = OctoConfig {
            num_workers: 2,
            pin_cores: false,
            queue_capacity: 4096,
        };
        let mut runtime = OctoRuntime::new(
            &config,
            |_| CountingWorker,
            || CountingAggregator { total: 0 },
        );
        let reader = runtime.read_handle();

        for i in 0..64u64 {
            runtime.insert(DataInput::U64(i));
        }
        std::thread::sleep(std::time::Duration::from_millis(5));
        let observed = reader.with_parent(|p| p.total);
        assert!(
            observed <= 64,
            "live reader should observe a partial or complete total"
        );

        let result = runtime.finish();
        assert_eq!(
            result.parent.total, 64,
            "all inserted items should be accounted for"
        );
        assert!(
            result.parent.total >= observed,
            "final total should not be less than live snapshot"
        );
    }

    #[test]
    fn octo_runtime_close_is_idempotent() {
        let config = OctoConfig {
            num_workers: 2,
            pin_cores: false,
            queue_capacity: 4096,
        };
        let runtime = OctoRuntime::new(
            &config,
            |_| HllOctoWorker::new(),
            || HllOctoAggregator {
                sketch: HyperLogLog::<Classic>::default(),
            },
        );
        runtime.close();
        runtime.close();
        let result = runtime.finish();
        assert_eq!(result.parent.sketch.estimate(), 0);
    }

    #[test]
    #[should_panic(expected = "cannot insert after runtime has been closed")]
    fn octo_runtime_insert_after_close_panics() {
        let config = OctoConfig {
            num_workers: 2,
            pin_cores: false,
            queue_capacity: 4096,
        };
        let mut runtime = OctoRuntime::new(
            &config,
            |_| HllOctoWorker::new(),
            || HllOctoAggregator {
                sketch: HyperLogLog::<Classic>::default(),
            },
        );
        runtime.close();
        runtime.insert(DataInput::U64(1));
    }

    #[test]
    fn octo_runtime_empty_stream_finishes() {
        let config = OctoConfig {
            num_workers: 4,
            pin_cores: false,
            queue_capacity: 4096,
        };
        let runtime = OctoRuntime::new(
            &config,
            |_| HllOctoWorker::new(),
            || HllOctoAggregator {
                sketch: HyperLogLog::<Classic>::default(),
            },
        );
        let result = runtime.finish();
        let estimate = result.parent.sketch.estimate();
        assert!(
            estimate == 0,
            "empty runtime should estimate 0 cardinality, got {estimate}"
        );
    }

    struct CountingWorker;

    impl OctoWorker for CountingWorker {
        type Delta = u64;

        fn process<F>(&mut self, _input: &DataInput, emit: &mut F)
        where
            F: FnMut(Self::Delta),
        {
            emit(1);
        }
    }

    struct CountingAggregator {
        total: u64,
    }

    impl OctoAggregator for CountingAggregator {
        type Delta = u64;

        fn apply(&mut self, delta: Self::Delta) {
            self.total += delta;
        }
    }

    #[test]
    fn octo_runtime_close_preserves_queued_items_without_dispatcher() {
        let config = OctoConfig {
            num_workers: 4,
            pin_cores: false,
            queue_capacity: 4096,
        };
        let n = 257usize;
        let mut runtime = OctoRuntime::new(
            &config,
            |_| CountingWorker,
            || CountingAggregator { total: 0 },
        );

        for i in 0..n as u64 {
            runtime.insert(DataInput::U64(i + 42));
        }
        runtime.close();
        let result = runtime.finish();
        assert_eq!(
            result.parent.total, n as u64,
            "runtime close should preserve already queued items"
        );
    }

    struct WorkerIdEmitter {
        worker_id: usize,
    }

    impl OctoWorker for WorkerIdEmitter {
        type Delta = usize;

        fn process<F>(&mut self, _input: &DataInput, emit: &mut F)
        where
            F: FnMut(Self::Delta),
        {
            emit(self.worker_id);
        }
    }

    struct WorkerLoadAggregator {
        loads: Vec<u64>,
    }

    impl OctoAggregator for WorkerLoadAggregator {
        type Delta = usize;

        fn apply(&mut self, delta: Self::Delta) {
            self.loads[delta] += 1;
        }
    }

    #[test]
    fn octo_runtime_round_robin_selector_distributes_deterministically() {
        let num_workers = 3;
        let inserts = 10u64;
        let config = OctoConfig {
            num_workers,
            pin_cores: false,
            queue_capacity: 4096,
        };
        let mut runtime = OctoRuntime::new(
            &config,
            |worker_id| WorkerIdEmitter { worker_id },
            || WorkerLoadAggregator {
                loads: vec![0; num_workers],
            },
        );

        for i in 0..inserts {
            runtime.insert(DataInput::U64(i));
        }
        let result = runtime.finish();

        assert_eq!(result.parent.loads, vec![4, 3, 3]);
    }
}