bitwheel 0.6.0

High-performance fixed capacity timer wheel
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
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
use std::time::{Duration, Instant};

use crate::timer::{
    Timer, TimerHandle,
    gear::{Gear, InsertError, NUM_SLOTS, SLOT_MASK},
};

pub const DEFAULT_GEARS: usize = 5;
pub const DEFAULT_RESOLUTION_MS: u64 = 4;
pub const DEFAULT_SLOT_CAP: usize = 32;
pub const DEFAULT_MAX_PROBES: usize = 3;

pub struct BitWheel<
    T,
    const NUM_GEARS: usize = DEFAULT_GEARS,
    const RESOLUTION_MS: u64 = DEFAULT_RESOLUTION_MS,
    const SLOT_CAP: usize = DEFAULT_SLOT_CAP,
    const MAX_PROBES: usize = DEFAULT_MAX_PROBES,
> {
    gears: [Gear<T, SLOT_CAP>; NUM_GEARS],
    epoch: Instant,
    current_tick: u64,
    next_fire_tick: Option<u64>,

    // Cached min fire tick per gear + dirty tracking
    gear_next_fire: [Option<u64>; NUM_GEARS],
    gear_dirty: u64,

    len: usize,
}

impl<
    T,
    const NUM_GEARS: usize,
    const RESOLUTION_MS: u64,
    const SLOT_CAP: usize,
    const MAX_PROBES: usize,
> Default for BitWheel<T, NUM_GEARS, RESOLUTION_MS, SLOT_CAP, MAX_PROBES>
{
    fn default() -> Self {
        Self::new()
    }
}

impl<
    T,
    const NUM_GEARS: usize,
    const RESOLUTION_MS: u64,
    const SLOT_CAP: usize,
    const MAX_PROBES: usize,
> BitWheel<T, NUM_GEARS, RESOLUTION_MS, SLOT_CAP, MAX_PROBES>
{
    const RESOLUTION_SHIFT: u32 = RESOLUTION_MS.trailing_zeros();

    pub fn with_epoch(epoch: Instant) -> Self {
        const {
            assert!(NUM_GEARS >= 1, "must have at least one gear");
            assert!(NUM_GEARS <= 64, "cannot have more than 64 gears");
            assert!(RESOLUTION_MS >= 1, "resolution must be at least 1ms");
            assert!(
                RESOLUTION_MS.is_power_of_two(),
                "resolution must be a power of 2"
            );
            assert!(
                6 * NUM_GEARS + (64 - RESOLUTION_MS.leading_zeros() as usize) <= 64,
                "configuration would overflow u64 - reduce NUM_GEARS or RESOLUTION_MS"
            );
            assert!(MAX_PROBES < 64, "max probes must be less than 64");
        }

        Self {
            gears: std::array::from_fn(|_| Gear::new()),
            epoch,
            current_tick: 0,
            next_fire_tick: None,
            gear_next_fire: [None; NUM_GEARS],
            gear_dirty: 0,
            len: 0,
        }
    }

    pub fn new() -> Self {
        Self::with_epoch(Instant::now())
    }

    pub fn boxed() -> Box<Self> {
        Box::new(Self::new())
    }

    pub fn boxed_with_epoch(epoch: Instant) -> Box<Self> {
        Box::new(Self::with_epoch(epoch))
    }

    /// Useful for deciding how to scale the `BitWheel`
    /// for your use case such that it satisfies the
    /// timer constraints.
    pub const fn gear_granularities() -> [u64; NUM_GEARS] {
        let mut granularities = [0; NUM_GEARS];
        granularities[0] = RESOLUTION_MS;

        let mut idx = 1;
        while idx < NUM_GEARS {
            granularities[idx] = granularities[idx - 1] * (NUM_SLOTS as u64);
            idx += 1;
        }

        granularities
    }

    /// Useful for deciding how to scale the `BitWheel`
    /// for your use case such that it does not take
    /// up a tremendous amount of memory.
    pub const fn memory_footprint() -> usize {
        // BitWheel struct (includes Gear array with Slot structs inline)
        let struct_size = std::mem::size_of::<Self>();

        // Heap allocation: each Slot has Box<[Entry<T>; SLOT_CAP]>
        // Entry<T> has ~24 bytes overhead (next_occupied, prev_occupied, discriminant)
        // as a conservative estimate.
        const ENTRY_OVERHEAD: usize = 24;
        let entry_size = std::mem::size_of::<T>() + ENTRY_OVERHEAD;
        let heap_per_slot = SLOT_CAP * entry_size;
        let total_heap = NUM_GEARS * NUM_SLOTS * heap_per_slot;

        struct_size + total_heap
    }

    #[inline(always)]
    pub const fn len(&self) -> usize {
        self.len
    }

    /// Peek at the next fire time.
    #[inline(always)]
    pub fn peek_next_fire(&self) -> Option<Instant> {
        self.next_fire_tick.map(|tick| self.tick_to_instant(tick))
    }

    #[inline(always)]
    pub fn duration_until_next(&self) -> Option<Duration> {
        self.next_fire_tick.map(|next| {
            let ticks_remaining = next.saturating_sub(self.current_tick);
            Duration::from_millis(ticks_remaining * RESOLUTION_MS)
        })
    }

    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.next_fire_tick.is_none()
    }

    pub fn insert(&mut self, when: Instant, timer: T) -> Result<TimerHandle, InsertError<T>> {
        let when_tick = self.instant_to_tick(when);
        let delay = when_tick.saturating_sub(self.current_tick).max(1);

        let gear_idx = self.gear_for_delay(delay);
        let target_slot = self.slot_for_tick(gear_idx, when_tick);
        let Ok(guard) = self.gears[gear_idx].acquire_next_available(target_slot, MAX_PROBES) else {
            return Err(InsertError(timer));
        };

        let actual_slot = guard.slot();
        let key = guard.insert(timer);

        // Compute actual fire tick and update caches
        let fire_tick = self.compute_fire_tick(gear_idx, actual_slot);
        self.gear_next_fire[gear_idx] =
            Some(self.gear_next_fire[gear_idx].map_or(fire_tick, |t| t.min(fire_tick)));
        self.next_fire_tick = Some(self.next_fire_tick.map_or(fire_tick, |t| t.min(fire_tick)));

        self.len += 1;
        Ok(TimerHandle {
            when_offset: when_tick,
            key: key as u32,
            gear: gear_idx as u8,
            slot: actual_slot as u8,
            overflow: false,
        })
    }

    pub fn cancel(&mut self, handle: TimerHandle) -> Option<T> {
        debug_assert!(!handle.overflow, "received unexpected handle for overflow");
        if handle.when_offset <= self.current_tick {
            return None;
        }

        let gear_idx = handle.gear as usize;
        let slot = handle.slot as usize;
        let key = handle.key as usize;

        if gear_idx >= NUM_GEARS {
            return None;
        }

        // SAFETY: This remove is safe due to the following invariants:
        //
        // 1. TimerHandle is only created by insert(), which stores valid
        //    gear/slot/key values at the time of insertion.
        //
        // 2. TimerHandle has no Clone/Copy, so ownership is unique.
        //    Once cancel() takes ownership, no other cancel is possible.
        //
        // 3. The when_offset > current_tick check ensures the timer hasn't
        //    fired yet, so the entry must still exist in the wheel.
        //
        // Together these guarantee the key points to a valid, occupied entry.
        let guard = self.gears[gear_idx].acquire(slot);
        let timer = guard.remove(key);

        // Mark gear dirty - removed timer might have been the min
        self.gear_dirty |= 1 << gear_idx;
        self.len = self.len.saturating_sub(1);
        Some(timer)
    }

    pub fn poll(&mut self, now: Instant, ctx: &mut T::Context) -> usize
    where
        T: Timer,
    {
        let target_tick = self.instant_to_tick(now);
        if target_tick <= self.current_tick {
            return 0;
        }

        // Skip-ahead optimization
        match self.next_fire_tick {
            None => {
                self.current_tick = target_tick;
                return 0;
            }
            Some(nft) if nft > target_tick => {
                self.current_tick = target_tick;
                return 0;
            }
            Some(nft) => {
                if nft > self.current_tick + 1 {
                    self.current_tick = nft - 1;
                }
            }
        }

        let mut fired = 0;
        for tick in (self.current_tick + 1)..=target_tick {
            fired += self.poll_tick(tick, ctx);
        }

        self.current_tick = target_tick;
        self.recompute_next_fire();
        fired
    }

    #[inline(always)]
    fn poll_tick(&mut self, tick: u64, ctx: &mut T::Context) -> usize
    where
        T: Timer,
    {
        let mut fired = 0;
        for gear_idx in 0..NUM_GEARS {
            if gear_idx > 0 {
                let mask = (1u64 << (6 * gear_idx)) - 1;
                if (tick & mask) != 0 {
                    continue;
                }
            }

            let slot = self.slot_for_tick(gear_idx, tick);
            fired += self.drain_and_fire(gear_idx, slot, ctx);
        }

        fired
    }

    #[inline(always)]
    fn drain_and_fire(&mut self, gear_idx: usize, slot: usize, ctx: &mut T::Context) -> usize
    where
        T: Timer,
    {
        let mut fired = 0;
        let guard = self.gears[gear_idx].acquire(slot);
        while let Some(mut timer) = guard.pop() {
            // Mark gear dirty since we removed a timer
            self.gear_dirty |= 1 << gear_idx;

            fired += 1;
            timer.fire(ctx);
        }

        fired
    }

    #[inline(always)]
    fn recompute_next_fire(&mut self) {
        // Recompute only dirty gears
        while self.gear_dirty != 0 {
            let gear_idx = self.gear_dirty.trailing_zeros() as usize;
            self.gear_dirty &= self.gear_dirty - 1;

            self.gear_next_fire[gear_idx] = self.compute_gear_min_fire(gear_idx);
        }

        // Could use sentinel instead
        let mut min_tick = u64::MAX;
        for &cached in &self.gear_next_fire {
            if let Some(tick) = cached {
                min_tick = min_tick.min(tick);
            }
        }
        self.next_fire_tick = if min_tick == u64::MAX {
            None
        } else {
            Some(min_tick)
        };
    }

    #[inline(always)]
    fn compute_gear_min_fire(&self, gear_idx: usize) -> Option<u64> {
        let occupied = self.gears[gear_idx].occupied_bitmap();
        if occupied == 0 {
            return None;
        }

        let current_slot = self.slot_for_tick(gear_idx, self.current_tick);

        // Rotate so slot after current is at bit 0
        let rotation = (current_slot as u32 + 1) & (SLOT_MASK as u32);
        let rotated = occupied.rotate_right(rotation);

        let distance = rotated.trailing_zeros() as usize;
        let next_slot = (current_slot + 1 + distance) & SLOT_MASK;

        Some(self.compute_fire_tick(gear_idx, next_slot))
    }

    #[inline(always)]
    fn compute_fire_tick(&self, gear_idx: usize, slot: usize) -> u64 {
        let shift = gear_idx * 6;
        let gear_period = 1u64 << (shift + 6);
        let slot_fire_offset = (slot as u64) << shift;
        let current_in_period = self.current_tick & (gear_period - 1);
        let base = self.current_tick & !(gear_period - 1);
        let passed = (slot_fire_offset <= current_in_period) as u64;
        base + passed * gear_period + slot_fire_offset
    }

    #[inline(always)]
    pub(crate) fn instant_to_tick(&self, when: Instant) -> u64 {
        when.saturating_duration_since(self.epoch).as_millis() as u64 >> Self::RESOLUTION_SHIFT
    }

    #[inline(always)]
    pub(crate) fn current_tick(&self) -> u64 {
        self.current_tick
    }

    #[inline(always)]
    fn gear_for_delay(&self, delay: u64) -> usize {
        if delay == 0 {
            return 0;
        }

        let gear = (63 - delay.leading_zeros()) as usize / 6;
        gear.min(NUM_GEARS - 1)
    }

    #[inline(always)]
    fn slot_for_tick(&self, gear: usize, tick: u64) -> usize {
        let shift = gear * 6;
        ((tick >> shift) & 63) as usize
    }

    /// Convert tick back to Instant.
    #[inline(always)]
    pub(crate) fn tick_to_instant(&self, tick: u64) -> Instant {
        self.epoch + Duration::from_millis(tick << Self::RESOLUTION_SHIFT)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::cell::Cell;
    use std::rc::Rc;

    // ==================== Test Timer Implementations ====================

    /// Simple one-shot timer that records when it fired.
    struct OneShotTimer {
        id: usize,
        fired: Rc<Cell<bool>>,
    }

    impl OneShotTimer {
        fn new(id: usize) -> (Self, Rc<Cell<bool>>) {
            let fired = Rc::new(Cell::new(false));
            (
                Self {
                    id,
                    fired: Rc::clone(&fired),
                },
                fired,
            )
        }
    }

    impl Timer for OneShotTimer {
        type Context = Vec<usize>;

        fn fire(&mut self, ctx: &mut Self::Context) {
            self.fired.set(true);
            ctx.push(self.id);
        }
    }

    /// Counter timer for simple fire counting.
    struct CounterTimer;

    impl Timer for CounterTimer {
        type Context = usize;

        fn fire(&mut self, ctx: &mut Self::Context) {
            *ctx += 1;
        }
    }

    // ==================== Construction Tests ====================

    #[test]
    fn test_new() {
        let wheel: Box<BitWheel<OneShotTimer>> = BitWheel::boxed();
        assert!(wheel.is_empty());
        assert!(wheel.duration_until_next().is_none());
    }

    #[test]
    fn test_with_epoch() {
        let epoch = Instant::now();
        let wheel: Box<BitWheel<OneShotTimer>> = BitWheel::boxed_with_epoch(epoch);
        assert!(wheel.is_empty());
        assert_eq!(wheel.current_tick, 0);
    }

    #[test]
    fn test_default() {
        let wheel: Box<BitWheel<OneShotTimer>> = BitWheel::boxed();
        assert!(wheel.is_empty());
    }

    #[test]
    fn test_custom_config() {
        // 4 gears, 10ms resolution, 16 slots, 5 max probes
        let wheel: Box<BitWheel<OneShotTimer, 4, 8, 16, 5>> = BitWheel::boxed();
        assert!(wheel.is_empty());
    }

    // ==================== Insert Tests ====================

    #[test]
    fn test_insert_single() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (timer, _fired) = OneShotTimer::new(1);
        let when = epoch + Duration::from_millis(100);

        let handle = wheel.insert(when, timer).unwrap();
        assert!(!wheel.is_empty());
        assert!(wheel.duration_until_next().is_some());
        assert_eq!(handle.gear, 1); // 100 ticks > 63, goes to gear 1
    }

    #[test]
    fn test_insert_updates_next_fire() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        assert!(wheel.duration_until_next().is_none());

        let (timer, _) = OneShotTimer::new(1);
        wheel
            .insert(epoch + Duration::from_millis(50), timer)
            .unwrap();

        let duration = wheel.duration_until_next().unwrap();
        assert!(duration.as_millis() <= 50);
    }

    #[test]
    fn test_insert_multiple_updates_next_fire_to_earliest() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (timer1, _) = OneShotTimer::new(1);
        let (timer2, _) = OneShotTimer::new(2);

        // Insert later timer first
        wheel
            .insert(epoch + Duration::from_millis(100), timer1)
            .unwrap();
        let d1 = wheel.duration_until_next().unwrap();

        // Insert earlier timer
        wheel
            .insert(epoch + Duration::from_millis(30), timer2)
            .unwrap();
        let d2 = wheel.duration_until_next().unwrap();

        assert!(d2 < d1);
        assert!(d2.as_millis() <= 30);
    }

    #[test]
    fn test_insert_gear_selection_gear0() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // Delays 1-63 should go to gear 0
        let (timer, _) = OneShotTimer::new(1);
        let handle = wheel
            .insert(epoch + Duration::from_millis(30), timer)
            .unwrap();
        assert_eq!(handle.gear, 0);
    }

    #[test]
    fn test_insert_gear_selection_gear1() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // Delays 64-4095 should go to gear 1
        let (timer, _) = OneShotTimer::new(1);
        let handle = wheel
            .insert(epoch + Duration::from_millis(100), timer)
            .unwrap();
        assert_eq!(handle.gear, 1);

        let (timer2, _) = OneShotTimer::new(2);
        let handle2 = wheel
            .insert(epoch + Duration::from_millis(4000), timer2)
            .unwrap();
        assert_eq!(handle2.gear, 1);
    }

    #[test]
    fn test_insert_gear_selection_gear2() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // Delays 4096+ should go to gear 2
        let (timer, _) = OneShotTimer::new(1);
        let handle = wheel
            .insert(epoch + Duration::from_millis(5000), timer)
            .unwrap();
        assert_eq!(handle.gear, 2);
    }

    #[test]
    fn test_insert_with_probing() {
        let epoch = Instant::now();
        // Small slot capacity to force probing
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 2, 10>> = BitWheel::boxed_with_epoch(epoch);

        let when = epoch + Duration::from_millis(10);

        // Fill up the target slot
        let (t1, _) = OneShotTimer::new(1);
        let (t2, _) = OneShotTimer::new(2);
        let h1 = wheel.insert(when, t1).unwrap();
        let h2 = wheel.insert(when, t2).unwrap();

        // Same slot
        assert_eq!(h1.slot, h2.slot);

        // Third insert should probe to next slot
        let (t3, _) = OneShotTimer::new(3);
        let h3 = wheel.insert(when, t3).unwrap();
        assert_ne!(h2.slot, h3.slot);
    }

    #[test]
    fn test_insert_slot_full_error() {
        let epoch = Instant::now();
        // Tiny config: 1 slot cap, 1 max probe
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 1, 1>> = BitWheel::boxed_with_epoch(epoch);

        let when = epoch + Duration::from_millis(10);

        let (t1, _) = OneShotTimer::new(1);
        wheel.insert(when, t1).unwrap();

        // Should fail - slot full and only 1 probe allowed
        let (t2, _) = OneShotTimer::new(2);
        let result = wheel.insert(when, t2);
        assert!(result.is_err());
    }

    #[test]
    fn test_insert_past_timer() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // Advance current_tick by polling
        let mut ctx = Vec::new();
        wheel.poll(epoch + Duration::from_millis(100), &mut ctx);

        // Insert timer "in the past" - should get delay of 1 (minimum)
        let (timer, _) = OneShotTimer::new(1);
        let handle = wheel
            .insert(epoch + Duration::from_millis(50), timer)
            .unwrap();

        // Should be in gear 0 with minimum delay
        assert_eq!(handle.gear, 0);
    }

    // ==================== Cancel Tests ====================

    #[test]
    fn test_cancel_returns_timer() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (timer, _) = OneShotTimer::new(42);
        let handle = wheel
            .insert(epoch + Duration::from_millis(100), timer)
            .unwrap();

        let cancelled = wheel.cancel(handle);
        assert!(cancelled.is_some());
        assert_eq!(cancelled.unwrap().id, 42);
    }

    #[test]
    fn test_cancel_after_poll_returns_none() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (timer, _) = OneShotTimer::new(1);
        let handle = wheel
            .insert(epoch + Duration::from_millis(10), timer)
            .unwrap();

        // Poll past the timer's deadline
        let mut ctx = Vec::new();
        wheel.poll(epoch + Duration::from_millis(100), &mut ctx);

        // Timer already fired, cancel should return None
        let cancelled = wheel.cancel(handle);
        assert!(cancelled.is_none());
    }

    // ==================== Poll Tests ====================

    #[test]
    fn test_poll_no_timers() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let mut ctx = Vec::new();
        let fired = wheel.poll(epoch + Duration::from_millis(100), &mut ctx);

        assert_eq!(fired, 0);
        assert!(ctx.is_empty());
    }

    #[test]
    fn test_poll_before_deadline() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (timer, fired) = OneShotTimer::new(1);
        wheel
            .insert(epoch + Duration::from_millis(100), timer)
            .unwrap();

        let mut ctx = Vec::new();
        let count = wheel.poll(epoch + Duration::from_millis(50), &mut ctx);

        assert_eq!(count, 0);
        assert!(!fired.get());
        assert!(ctx.is_empty());
    }

    #[test]
    fn test_poll_at_deadline() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (timer, fired) = OneShotTimer::new(1);
        wheel
            .insert(epoch + Duration::from_millis(10), timer)
            .unwrap();

        let mut ctx = Vec::new();
        let count = wheel.poll(epoch + Duration::from_millis(10), &mut ctx);

        assert_eq!(count, 1);
        assert!(fired.get());
        assert_eq!(ctx, vec![1]);
    }

    #[test]
    fn test_poll_after_deadline() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (timer, fired) = OneShotTimer::new(1);
        wheel
            .insert(epoch + Duration::from_millis(10), timer)
            .unwrap();

        let mut ctx = Vec::new();
        let count = wheel.poll(epoch + Duration::from_millis(100), &mut ctx);

        assert_eq!(count, 1);
        assert!(fired.get());
        assert_eq!(ctx, vec![1]);
    }

    #[test]
    fn test_poll_multiple_timers_same_slot() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let when = epoch + Duration::from_millis(10);
        let (t1, f1) = OneShotTimer::new(1);
        let (t2, f2) = OneShotTimer::new(2);
        let (t3, f3) = OneShotTimer::new(3);

        wheel.insert(when, t1).unwrap();
        wheel.insert(when, t2).unwrap();
        wheel.insert(when, t3).unwrap();

        let mut ctx = Vec::new();
        let count = wheel.poll(epoch + Duration::from_millis(20), &mut ctx);

        assert_eq!(count, 3);
        assert!(f1.get());
        assert!(f2.get());
        assert!(f3.get());
        assert_eq!(ctx.len(), 3);
    }

    #[test]
    fn test_poll_multiple_timers_different_times() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (t1, f1) = OneShotTimer::new(1);
        let (t2, f2) = OneShotTimer::new(2);
        let (t3, f3) = OneShotTimer::new(3);

        wheel.insert(epoch + Duration::from_millis(10), t1).unwrap();
        wheel.insert(epoch + Duration::from_millis(20), t2).unwrap();
        wheel.insert(epoch + Duration::from_millis(30), t3).unwrap();

        // Poll at 15ms - only first should fire
        let mut ctx = Vec::new();
        wheel.poll(epoch + Duration::from_millis(15), &mut ctx);
        assert!(f1.get());
        assert!(!f2.get());
        assert!(!f3.get());
        assert_eq!(ctx, vec![1]);

        // Poll at 25ms - second should fire
        ctx.clear();
        wheel.poll(epoch + Duration::from_millis(25), &mut ctx);
        assert!(f2.get());
        assert!(!f3.get());
        assert_eq!(ctx, vec![2]);

        // Poll at 35ms - third should fire
        ctx.clear();
        wheel.poll(epoch + Duration::from_millis(35), &mut ctx);
        assert!(f3.get());
        assert_eq!(ctx, vec![3]);
    }

    #[test]
    fn test_poll_clears_is_empty() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (timer, _) = OneShotTimer::new(1);
        wheel
            .insert(epoch + Duration::from_millis(10), timer)
            .unwrap();
        assert!(!wheel.is_empty());

        let mut ctx = Vec::new();
        wheel.poll(epoch + Duration::from_millis(100), &mut ctx);
        assert!(wheel.is_empty());
    }

    #[test]
    fn test_poll_updates_duration_until_next() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (t1, _) = OneShotTimer::new(1);
        let (t2, _) = OneShotTimer::new(2);

        wheel.insert(epoch + Duration::from_millis(10), t1).unwrap();
        wheel.insert(epoch + Duration::from_millis(50), t2).unwrap();

        // Before poll, next fire should be ~10ms
        let d1 = wheel.duration_until_next().unwrap();
        assert!(d1.as_millis() <= 10);

        // Poll to fire first timer
        let mut ctx = Vec::new();
        wheel.poll(epoch + Duration::from_millis(20), &mut ctx);

        // After poll, next fire should be ~50ms from epoch, minus current position
        let d2 = wheel.duration_until_next().unwrap();
        assert!(d2.as_millis() <= 30); // 50 - 20 = 30
    }

    // ==================== Gear Rotation Tests ====================

    #[test]
    fn test_gear0_every_tick() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<CounterTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // Insert timers at consecutive ticks
        for i in 1..=5 {
            wheel
                .insert(epoch + Duration::from_millis(i), CounterTimer)
                .unwrap();
        }

        let mut count = 0usize;

        // Poll each tick individually
        for i in 1..=5 {
            wheel.poll(epoch + Duration::from_millis(i), &mut count);
        }

        assert_eq!(count, 5);
    }

    #[test]
    fn test_gear1_rotation() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<CounterTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // Insert timer in gear 1 range (64-4095 ticks)
        wheel
            .insert(epoch + Duration::from_millis(100), CounterTimer)
            .unwrap();

        let mut count = 0usize;

        // Poll at tick 100
        wheel.poll(epoch + Duration::from_millis(100), &mut count);
        assert_eq!(count, 1);
    }

    #[test]
    fn test_higher_gear_precision() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // Insert timer at 5000ms (gear 2 territory)
        let (timer, fired) = OneShotTimer::new(1);
        let handle = wheel
            .insert(epoch + Duration::from_millis(5000), timer)
            .unwrap();
        assert_eq!(handle.gear, 2);

        let mut ctx = Vec::new();

        // Poll before - should not fire
        wheel.poll(epoch + Duration::from_millis(4000), &mut ctx);
        assert!(!fired.get());

        // Poll at/after deadline - should fire
        wheel.poll(epoch + Duration::from_millis(5100), &mut ctx);
        assert!(fired.get());
    }

    // ==================== Edge Cases ====================

    #[test]
    fn test_poll_same_instant_twice() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (timer, _) = OneShotTimer::new(1);
        wheel
            .insert(epoch + Duration::from_millis(10), timer)
            .unwrap();

        let mut ctx = Vec::new();
        let now = epoch + Duration::from_millis(20);

        // First poll fires the timer
        let r1 = wheel.poll(now, &mut ctx);
        assert_eq!(r1, 1);

        // Second poll at same instant should be no-op
        let r2 = wheel.poll(now, &mut ctx);
        assert_eq!(r2, 0);
    }

    #[test]
    fn test_poll_backwards_in_time() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (timer, fired) = OneShotTimer::new(1);
        wheel
            .insert(epoch + Duration::from_millis(50), timer)
            .unwrap();

        let mut ctx = Vec::new();

        // Advance to 100ms
        wheel.poll(epoch + Duration::from_millis(100), &mut ctx);
        assert!(fired.get());

        // "Go back" to 30ms - should be no-op (target_tick <= current_tick)
        let r = wheel.poll(epoch + Duration::from_millis(30), &mut ctx);
        assert_eq!(r, 0);
    }

    #[test]
    fn test_many_timers_stress() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<CounterTimer, 4, 1, 64, 10>> =
            BitWheel::boxed_with_epoch(epoch);

        // Insert 1000 timers at various delays
        for i in 0..1000 {
            let delay = (i % 500) + 1;
            wheel
                .insert(epoch + Duration::from_millis(delay as u64), CounterTimer)
                .unwrap();
        }

        let mut count = 0usize;
        wheel.poll(epoch + Duration::from_millis(1000), &mut count);

        assert_eq!(count, 1000);
        assert!(wheel.is_empty());
    }

    // ==================== Duration Until Next Tests ====================

    #[test]
    fn test_duration_until_next_empty() {
        let wheel: Box<BitWheel<OneShotTimer>> = BitWheel::boxed();
        assert!(wheel.duration_until_next().is_none());
    }

    #[test]
    fn test_duration_until_next_after_cancel() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (t1, _) = OneShotTimer::new(1);
        let (t2, _) = OneShotTimer::new(2);

        wheel.insert(epoch + Duration::from_millis(50), t1).unwrap();
        let h2 = wheel.insert(epoch + Duration::from_millis(10), t2).unwrap();

        // Next fire should be at 10ms
        let d1 = wheel.duration_until_next().unwrap();
        assert!(d1.as_millis() <= 10);

        // Cancel the earlier timer
        wheel.cancel(h2);

        // Note: next_fire_tick is NOT updated on cancel (stale is OK)
        // It gets fixed on next poll
    }

    // ==================== Configuration Validation ====================

    #[test]
    fn test_gear_for_delay_boundaries() {
        let epoch = Instant::now();
        let wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // Gear 0: 1-63
        assert_eq!(wheel.gear_for_delay(1), 0);
        assert_eq!(wheel.gear_for_delay(63), 0);

        // Gear 1: 64-4095
        assert_eq!(wheel.gear_for_delay(64), 1);
        assert_eq!(wheel.gear_for_delay(4095), 1);

        // Gear 2: 4096-262143
        assert_eq!(wheel.gear_for_delay(4096), 2);
        assert_eq!(wheel.gear_for_delay(262143), 2);

        // Gear 3: 262144+
        assert_eq!(wheel.gear_for_delay(262144), 3);
    }

    #[test]
    fn test_slot_for_tick() {
        let epoch = Instant::now();
        let wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // Gear 0: tick & 63
        assert_eq!(wheel.slot_for_tick(0, 0), 0);
        assert_eq!(wheel.slot_for_tick(0, 10), 10);
        assert_eq!(wheel.slot_for_tick(0, 63), 63);
        assert_eq!(wheel.slot_for_tick(0, 64), 0); // wraps

        // Gear 1: (tick >> 6) & 63
        assert_eq!(wheel.slot_for_tick(1, 64), 1);
        assert_eq!(wheel.slot_for_tick(1, 128), 2);
        assert_eq!(wheel.slot_for_tick(1, 4032), 63);

        // Gear 2: (tick >> 12) & 63
        assert_eq!(wheel.slot_for_tick(2, 4096), 1);
        assert_eq!(wheel.slot_for_tick(2, 8192), 2);
    }

    // ==================== Drop Behavior ====================

    #[test]
    fn test_drop_pending_timers() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        struct DropCounter(Arc<AtomicUsize>);

        impl Drop for DropCounter {
            fn drop(&mut self) {
                self.0.fetch_add(1, Ordering::SeqCst);
            }
        }

        impl Timer for DropCounter {
            type Context = ();
            fn fire(&mut self, _ctx: &mut ()) {}
        }

        let drop_count = Arc::new(AtomicUsize::new(0));
        let epoch = Instant::now();

        {
            let mut wheel: Box<BitWheel<DropCounter, 4, 1, 32, 3>> =
                BitWheel::boxed_with_epoch(epoch);

            for i in 0..10 {
                wheel
                    .insert(
                        epoch + Duration::from_millis((i + 1) * 100),
                        DropCounter(Arc::clone(&drop_count)),
                    )
                    .unwrap();
            }

            assert_eq!(drop_count.load(Ordering::SeqCst), 0);
            // wheel drops here
        }

        assert_eq!(drop_count.load(Ordering::SeqCst), 10);
    }

    // ==================== Additional Tests ====================

    #[test]
    fn test_next_fire_bitmap_wrap_around() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // Insert timer that lands in slot 60
        let (t1, _) = OneShotTimer::new(1);
        wheel.insert(epoch + Duration::from_millis(60), t1).unwrap();

        // Advance past slot 60
        let mut ctx = Vec::new();
        wheel.poll(epoch + Duration::from_millis(61), &mut ctx);

        // Insert timer in slot 5 (wraps around)
        let (t2, _) = OneShotTimer::new(2);
        wheel.insert(epoch + Duration::from_millis(70), t2).unwrap(); // tick 70, slot = 70 & 63 = 6

        // duration_until_next should find the wrapped slot
        assert!(wheel.duration_until_next().is_some());
    }

    #[test]
    fn test_multiple_gears_same_poll() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        let (t1, f1) = OneShotTimer::new(1);
        let (t2, f2) = OneShotTimer::new(2);
        let (t3, f3) = OneShotTimer::new(3);

        // Gear 0 timer
        wheel.insert(epoch + Duration::from_millis(10), t1).unwrap();
        // Gear 1 timer
        wheel
            .insert(epoch + Duration::from_millis(100), t2)
            .unwrap();
        // Gear 2 timer
        wheel
            .insert(epoch + Duration::from_millis(5000), t3)
            .unwrap();

        let mut ctx = Vec::new();

        // Poll far enough to fire all
        wheel.poll(epoch + Duration::from_millis(6000), &mut ctx);

        assert!(f1.get());
        assert!(f2.get());
        assert!(f3.get());
        assert_eq!(ctx.len(), 3);
    }

    #[test]
    fn test_gear_boundary_exact_64() {
        let epoch = Instant::now();
        let wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // delay = 63 → gear 0
        assert_eq!(wheel.gear_for_delay(63), 0);
        // delay = 64 → gear 1
        assert_eq!(wheel.gear_for_delay(64), 1);
    }

    #[test]
    fn test_gear_boundary_exact_4096() {
        let epoch = Instant::now();
        let wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // delay = 4095 → gear 1
        assert_eq!(wheel.gear_for_delay(4095), 1);
        // delay = 4096 → gear 2
        assert_eq!(wheel.gear_for_delay(4096), 2);
    }

    #[test]
    fn test_insert_after_cancel_reuses_slot() {
        let epoch = Instant::now();
        let mut wheel: Box<BitWheel<OneShotTimer, 4, 1, 2, 3>> = BitWheel::boxed_with_epoch(epoch);

        let when = epoch + Duration::from_millis(10);

        // Fill slot
        let (t1, _) = OneShotTimer::new(1);
        let (t2, _) = OneShotTimer::new(2);
        let h1 = wheel.insert(when, t1).unwrap();
        let h2 = wheel.insert(when, t2).unwrap();

        // Cancel one
        wheel.cancel(h1);

        // Should be able to insert into same slot again
        let (t3, _) = OneShotTimer::new(3);
        let h3 = wheel.insert(when, t3).unwrap();

        // Should get same slot as cancelled timer
        assert_eq!(h2.slot, h3.slot);
    }

    #[test]
    fn test_zero_delay_goes_to_gear0() {
        let epoch = Instant::now();
        let wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        assert_eq!(wheel.gear_for_delay(0), 0);
    }

    #[test]
    fn test_delay_beyond_max_gears_clamps() {
        let epoch = Instant::now();
        let wheel: Box<BitWheel<OneShotTimer, 4, 1, 32, 3>> = BitWheel::boxed_with_epoch(epoch);

        // Very large delay should clamp to highest gear (3)
        assert_eq!(wheel.gear_for_delay(u64::MAX), 3);
        assert_eq!(wheel.gear_for_delay(1_000_000_000), 3);
    }
}

#[cfg(test)]
mod latency_tests {
    use crate::timer::{BurstWheel, Wheel};

    use super::*;
    use hdrhistogram::Histogram;
    use std::time::{Duration, Instant};

    const WARMUP: u64 = 100_000;
    const ITERATIONS: u64 = 1_000_000;

    struct LatencyTimer;

    impl Timer for LatencyTimer {
        type Context = ();

        fn fire(&mut self, _ctx: &mut ()) {}
    }

    fn print_histogram(name: &str, hist: &Histogram<u64>) {
        println!("\n=== {} ===", name);
        println!("  count:  {}", hist.len());
        println!("  min:    {} ns", hist.min());
        println!("  max:    {} ns", hist.max());
        println!("  mean:   {:.1} ns", hist.mean());
        println!("  stddev: {:.1} ns", hist.stdev());
        println!("  p50:    {} ns", hist.value_at_quantile(0.50));
        println!("  p90:    {} ns", hist.value_at_quantile(0.90));
        println!("  p99:    {} ns", hist.value_at_quantile(0.99));
        println!("  p99.9:  {} ns", hist.value_at_quantile(0.999));
        println!("  p99.99: {} ns", hist.value_at_quantile(0.9999));
    }

    #[test]
    #[ignore]
    fn hdr_insert_latency() {
        let epoch = Instant::now();
        let mut wheel: Box<Wheel<LatencyTimer>> = BitWheel::boxed_with_epoch(epoch);

        let mut hist = Histogram::<u64>::new(3).unwrap();

        // Warmup
        for i in 0..WARMUP {
            let when = epoch + Duration::from_millis((i % 500) + 10);
            let handle = wheel.insert(when, LatencyTimer).unwrap();
            wheel.cancel(handle);
        }

        // Measure
        for i in 0..ITERATIONS {
            let when = epoch + Duration::from_millis((i % 500) + 10);

            let start = Instant::now();
            let handle = wheel.insert(when, LatencyTimer).unwrap();
            let elapsed = start.elapsed().as_nanos() as u64;

            hist.record(elapsed).unwrap();
            wheel.cancel(handle);
        }

        print_histogram("Insert Latency", &hist);
    }

    #[test]
    #[ignore]
    fn hdr_cancel_latency() {
        let epoch = Instant::now();
        let mut wheel: Box<Wheel<LatencyTimer>> = BitWheel::boxed_with_epoch(epoch);

        let mut hist = Histogram::<u64>::new(3).unwrap();

        // Warmup
        for i in 0..WARMUP {
            let when = epoch + Duration::from_millis((i % 500) + 10);
            let handle = wheel.insert(when, LatencyTimer).unwrap();
            wheel.cancel(handle);
        }

        // Measure
        for i in 0..ITERATIONS {
            let when = epoch + Duration::from_millis((i % 500) + 10);
            let handle = wheel.insert(when, LatencyTimer).unwrap();

            let start = Instant::now();
            let _ = wheel.cancel(handle);
            let elapsed = start.elapsed().as_nanos() as u64;

            hist.record(elapsed).unwrap();
        }

        print_histogram("Cancel Latency", &hist);
    }

    #[test]
    #[ignore]
    fn hdr_poll_empty() {
        let epoch = Instant::now();
        let mut wheel: Box<Wheel<LatencyTimer>> = BitWheel::boxed_with_epoch(epoch);

        let mut hist = Histogram::<u64>::new(3).unwrap();
        let mut ctx = ();

        // Warmup
        for i in 0..WARMUP {
            let now = epoch + Duration::from_millis(i);
            wheel.poll(now, &mut ctx);
        }

        // Measure
        for i in WARMUP..(WARMUP + ITERATIONS) {
            let now = epoch + Duration::from_millis(i);

            let start = Instant::now();
            wheel.poll(now, &mut ctx);
            let elapsed = start.elapsed().as_nanos() as u64;

            hist.record(elapsed).unwrap();
        }

        print_histogram("Poll Empty", &hist);
    }

    #[test]
    #[ignore]
    fn hdr_poll_pending_no_fires() {
        let epoch = Instant::now();
        let mut wheel: Box<Wheel<LatencyTimer>> = BitWheel::boxed_with_epoch(epoch);

        // Insert timers far in future
        for i in 0..1000 {
            let when = epoch + Duration::from_millis(100_000_000 + i);
            let _ = wheel.insert(when, LatencyTimer);
        }

        let mut hist = Histogram::<u64>::new(3).unwrap();
        let mut ctx = ();

        // Warmup
        for i in 0..WARMUP {
            let now = epoch + Duration::from_millis(i);
            wheel.poll(now, &mut ctx);
        }

        // Measure
        for i in WARMUP..(WARMUP + ITERATIONS) {
            let now = epoch + Duration::from_millis(i);

            let start = Instant::now();
            wheel.poll(now, &mut ctx);
            let elapsed = start.elapsed().as_nanos() as u64;

            hist.record(elapsed).unwrap();
        }

        print_histogram("Poll Pending (No Fires)", &hist);
    }

    #[test]
    #[ignore]
    fn hdr_poll_single_fire() {
        let epoch = Instant::now();
        let mut wheel: Box<Wheel<LatencyTimer>> = BitWheel::boxed_with_epoch(epoch);

        let mut hist = Histogram::<u64>::new(3).unwrap();
        let mut ctx = ();

        // Warmup
        for i in 0..WARMUP {
            let when = epoch + Duration::from_millis(i + 1);
            let _ = wheel.insert(when, LatencyTimer);
            let now = epoch + Duration::from_millis(i + 1);
            wheel.poll(now, &mut ctx);
        }

        // Measure: insert timer, advance time, poll fires it
        for i in 0..ITERATIONS {
            let tick = WARMUP + i;
            let when = epoch + Duration::from_millis(tick + 1);
            let _ = wheel.insert(when, LatencyTimer);

            let now = epoch + Duration::from_millis(tick + 1);

            let start = Instant::now();
            wheel.poll(now, &mut ctx);
            let elapsed = start.elapsed().as_nanos() as u64;

            hist.record(elapsed).unwrap();
        }

        print_histogram("Poll Single Fire", &hist);
    }

    #[test]
    #[ignore]
    fn hdr_trading_simulation() {
        let epoch = Instant::now();
        let mut wheel: Box<Wheel<LatencyTimer>> = BitWheel::boxed_with_epoch(epoch);

        let mut insert_hist = Histogram::<u64>::new(3).unwrap();
        let mut poll_hist = Histogram::<u64>::new(3).unwrap();
        let mut cancel_hist = Histogram::<u64>::new(3).unwrap();

        let mut handles = Vec::with_capacity(100);
        let mut ctx = ();
        let mut now = epoch;

        // Warmup
        for i in 0..WARMUP {
            now += Duration::from_millis(1);
            wheel.poll(now, &mut ctx);

            if i % 5 != 0 {
                let when = now + Duration::from_millis(50 + (i % 200));
                if let Ok(handle) = wheel.insert(when, LatencyTimer) {
                    if handles.len() < 100 {
                        handles.push(handle);
                    }
                }
            }

            if i % 20 == 0 {
                if let Some(handle) = handles.pop() {
                    let _ = wheel.cancel(handle);
                }
            }
        }

        // Measure
        for i in 0..ITERATIONS {
            now += Duration::from_millis(1);

            // Poll
            let start = Instant::now();
            wheel.poll(now, &mut ctx);
            poll_hist.record(start.elapsed().as_nanos() as u64).unwrap();

            // Insert 80%
            if i % 5 != 0 {
                let when = now + Duration::from_millis(50 + (i % 200));

                let start = Instant::now();
                if let Ok(handle) = wheel.insert(when, LatencyTimer) {
                    insert_hist
                        .record(start.elapsed().as_nanos() as u64)
                        .unwrap();

                    if handles.len() < 100 {
                        handles.push(handle);
                    }
                }
            }

            // Cancel 5%
            if i % 20 == 0 {
                if let Some(handle) = handles.pop() {
                    let start = Instant::now();
                    let _ = wheel.cancel(handle);
                    cancel_hist
                        .record(start.elapsed().as_nanos() as u64)
                        .unwrap();
                }
            }
        }

        print_histogram("Trading Sim - Insert", &insert_hist);
        print_histogram("Trading Sim - Poll", &poll_hist);
        print_histogram("Trading Sim - Cancel", &cancel_hist);
    }

    #[test]
    #[ignore]
    fn hdr_bursty_workload() {
        let epoch = Instant::now();
        let mut wheel: Box<BurstWheel<LatencyTimer>> = BitWheel::boxed_with_epoch(epoch);

        let mut hist = Histogram::<u64>::new(3).unwrap();
        let mut ctx = ();

        // Warmup
        for i in 0..WARMUP {
            let now = epoch + Duration::from_millis(i + 1);

            // Burst every 100 ticks
            if i % 100 == 0 {
                for j in 0..50 {
                    let when = now + Duration::from_millis(20 + j % 80);
                    let _ = wheel.insert(when, LatencyTimer);
                }
            }

            wheel.poll(now, &mut ctx);
        }

        // Measure
        for i in WARMUP..(WARMUP + ITERATIONS) {
            let now = epoch + Duration::from_millis(i + 1);

            // Burst every 100 ticks
            if i % 100 == 0 {
                for j in 0..50 {
                    let when = now + Duration::from_millis(20 + j % 80);
                    let _ = wheel.insert(when, LatencyTimer);
                }
            }

            let start = Instant::now();
            wheel.poll(now, &mut ctx);
            let elapsed = start.elapsed().as_nanos() as u64;

            hist.record(elapsed).unwrap();
        }

        print_histogram("Bursty (50 burst every 100ms)", &hist);
    }

    #[test]
    #[ignore]
    fn hdr_interleaved_insert() {
        let epoch = Instant::now();
        let mut wheel: Box<Wheel<LatencyTimer>> = BitWheel::boxed_with_epoch(epoch);

        let mut hist = Histogram::<u64>::new(3).unwrap();
        let mut ctx = ();
        let mut now = epoch;

        // Pre-populate: timers at regular intervals (every 10ms from 100-10000ms)
        for i in 0..10_000 {
            let when = epoch + Duration::from_millis(100 + i * 10);
            let _ = wheel.insert(when, LatencyTimer);
        }

        // Warmup - insert timers that land BETWEEN existing entries
        for i in 0..WARMUP {
            now += Duration::from_millis(1);
            wheel.poll(now, &mut ctx);

            let base = 100 + ((i % 990) * 10);
            let when = epoch + Duration::from_millis(base + 5);
            let _ = wheel.insert(when, LatencyTimer);
        }

        // Measure - every insert lands between two existing timers
        for i in 0..ITERATIONS {
            now += Duration::from_millis(1);
            wheel.poll(now, &mut ctx);

            // Replenish the "grid" timers as they fire
            if i % 10 == 0 {
                let when = now + Duration::from_millis(10000);
                let _ = wheel.insert(when, LatencyTimer);
            }

            // The measured insert: always between existing entries
            let base = ((now.duration_since(epoch).as_millis() as u64) % 9900) + 100;
            let offset = (i % 3) * 2 + 3; // 3, 5, or 7
            let when = epoch + Duration::from_millis(base + offset);

            let start = Instant::now();
            let _ = wheel.insert(when, LatencyTimer);
            let elapsed = start.elapsed().as_nanos() as u64;

            hist.record(elapsed).unwrap();
        }

        print_histogram("Interleaved Insert", &hist);
    }
}