frankensearch-index 0.2.1

FSVI vector index, SIMD dot product, and top-k search for frankensearch
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
//! Index warm-up and adaptive page prefaulting for memory-mapped FSVI indices.
//!
//! Cold-start latency for mmap'd indices can be 10-100x higher than warm due to
//! page faults. This module provides controlled prefaulting to eliminate cold-start
//! variance.
//!
//! # Strategies
//!
//! - **None**: No prefaulting (default, current behavior).
//! - **Full**: Touch every page sequentially — simple but may waste I/O budget.
//! - **Header**: Prefault only the header and record table (smallest footprint).
//! - **Adaptive**: Heat-map based intelligent prefaulting that learns which pages
//!   are actually accessed during typical queries.
//!
//! # Example
//!
//! ```
//! use frankensearch_index::warmup::{WarmUpConfig, WarmUpStrategy, HeatMap};
//!
//! let config = WarmUpConfig::default();
//! assert!(matches!(config.strategy, WarmUpStrategy::None));
//!
//! let heat_map = HeatMap::new(1_000_000);
//! assert_eq!(heat_map.page_count(), 245); // ceil(1_000_000 / 4096)
//! ```

use std::sync::atomic::{AtomicU8, Ordering};

use serde::{Deserialize, Serialize};
use tracing::debug;

/// OS page size used for heat map granularity.
const PAGE_SIZE: usize = 4096;

/// Maximum heat value per page (u8 max).
const MAX_HEAT: u8 = 255;

// ─── Configuration ──────────────────────────────────────────────────────────

/// Configuration for index warm-up behavior.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WarmUpConfig {
    /// Which prefaulting strategy to use.
    pub strategy: WarmUpStrategy,
    /// Maximum bytes to prefault across all indices (default: 256 MB).
    pub max_bytes: usize,
    /// Number of concurrent prefault threads (default: 2).
    pub parallel_readers: usize,
}

impl Default for WarmUpConfig {
    fn default() -> Self {
        Self {
            strategy: WarmUpStrategy::None,
            max_bytes: 256 * 1024 * 1024,
            parallel_readers: 2,
        }
    }
}

impl WarmUpConfig {
    /// Create a config with the adaptive strategy using default parameters.
    #[must_use]
    pub fn adaptive() -> Self {
        Self {
            strategy: WarmUpStrategy::Adaptive(AdaptiveConfig::default()),
            ..Self::default()
        }
    }

    /// Create a config that prefaults all pages.
    #[must_use]
    pub fn full() -> Self {
        Self {
            strategy: WarmUpStrategy::Full,
            ..Self::default()
        }
    }

    /// Create a config that prefaults only the header region.
    #[must_use]
    pub fn header_only() -> Self {
        Self {
            strategy: WarmUpStrategy::Header,
            ..Self::default()
        }
    }

    /// Build warm-up config from environment variables.
    ///
    /// Supported variables:
    /// - `FRANKENSEARCH_WARMUP_STRATEGY`: `none|header|full|adaptive`
    /// - `FRANKENSEARCH_WARMUP_MAX_BYTES`: positive integer byte budget
    /// - `FRANKENSEARCH_WARMUP_PARALLEL_READERS`: positive integer
    /// - `FRANKENSEARCH_WARMUP_MIN_HEAT`: adaptive threshold in `[0.0, 1.0]`
    /// - `FRANKENSEARCH_WARMUP_HEAT_DECAY`: adaptive decay in `[0.0, 1.0]`
    #[must_use]
    pub fn from_env() -> Self {
        let mut config = Self::default();

        if let Ok(strategy) = std::env::var("FRANKENSEARCH_WARMUP_STRATEGY") {
            match strategy.trim().to_ascii_lowercase().as_str() {
                "" | "none" => config.strategy = WarmUpStrategy::None,
                "header" | "header_only" => config.strategy = WarmUpStrategy::Header,
                "full" => config.strategy = WarmUpStrategy::Full,
                "adaptive" => config.strategy = WarmUpStrategy::Adaptive(AdaptiveConfig::default()),
                _ => {}
            }
        }

        if let Ok(raw) = std::env::var("FRANKENSEARCH_WARMUP_MAX_BYTES")
            && let Ok(parsed) = raw.parse::<usize>()
            && parsed > 0
        {
            config.max_bytes = parsed;
        }

        if let Ok(raw) = std::env::var("FRANKENSEARCH_WARMUP_PARALLEL_READERS")
            && let Ok(parsed) = raw.parse::<usize>()
            && parsed > 0
        {
            config.parallel_readers = parsed;
        }

        if let WarmUpStrategy::Adaptive(ref mut adaptive) = config.strategy {
            if let Ok(raw) = std::env::var("FRANKENSEARCH_WARMUP_MIN_HEAT")
                && let Ok(parsed) = raw.parse::<f64>()
            {
                adaptive.min_heat = parsed;
            }
            if let Ok(raw) = std::env::var("FRANKENSEARCH_WARMUP_HEAT_DECAY")
                && let Ok(parsed) = raw.parse::<f64>()
            {
                adaptive.heat_decay = parsed;
            }
        }

        config
    }
}

/// Prefaulting strategy.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WarmUpStrategy {
    /// No prefaulting (current behavior).
    None,
    /// Touch every page sequentially.
    Full,
    /// Prefault header + record table only (smallest footprint).
    Header,
    /// Heat-map based intelligent prefaulting.
    Adaptive(AdaptiveConfig),
}

/// Configuration for the adaptive prefaulting strategy.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptiveConfig {
    /// Exponential decay factor applied to heat after each search cycle.
    /// Must be in [0.0, 1.0]. Default: 0.95.
    pub heat_decay: f64,
    /// Minimum normalized heat (0.0-1.0) to prefault a page.
    /// Pages below this threshold are not prefaulted. Default: 0.1.
    pub min_heat: f64,
}

impl Default for AdaptiveConfig {
    fn default() -> Self {
        Self {
            heat_decay: 0.95,
            min_heat: 0.1,
        }
    }
}

impl AdaptiveConfig {
    /// Clamp `heat_decay` to `[0.0, 1.0]`.
    ///
    /// Non-finite values (NaN/Inf from deserialization) fall back to 0.95.
    #[must_use]
    pub const fn clamped_heat_decay(&self) -> f64 {
        if self.heat_decay.is_finite() {
            self.heat_decay.clamp(0.0, 1.0)
        } else {
            0.95
        }
    }

    /// Clamp `min_heat` to `[0.0, 1.0]`.
    ///
    /// Non-finite values (NaN/Inf from deserialization) fall back to 0.1.
    #[must_use]
    pub const fn clamped_min_heat(&self) -> f64 {
        if self.min_heat.is_finite() {
            self.min_heat.clamp(0.0, 1.0)
        } else {
            0.1
        }
    }
}

// ─── Heat Map ───────────────────────────────────────────────────────────────

/// Per-page heat tracker for adaptive prefaulting.
///
/// Each 4 KB page gets an `AtomicU8` heat counter (0-255). Heat is incremented
/// on every page access detected via [`record_access`](Self::record_access),
/// and decayed exponentially per search cycle via [`decay`](Self::decay).
///
/// Memory overhead: ~1 byte per 4 KB page = ~256 KB for a 1 GB index.
pub struct HeatMap {
    /// Per-page heat values. Index = page number.
    pages: Vec<AtomicU8>,
    /// Total data size this heat map covers.
    total_bytes: usize,
}

impl HeatMap {
    /// Create a new heat map covering `total_bytes` of data.
    ///
    /// The heat map allocates one `AtomicU8` per 4 KB page.
    #[must_use]
    pub fn new(total_bytes: usize) -> Self {
        let page_count = pages_for_bytes(total_bytes);
        let pages = (0..page_count).map(|_| AtomicU8::new(0)).collect();
        Self { pages, total_bytes }
    }

    /// Number of tracked pages.
    #[must_use]
    pub const fn page_count(&self) -> usize {
        self.pages.len()
    }

    /// Total bytes this heat map covers.
    #[must_use]
    pub const fn total_bytes(&self) -> usize {
        self.total_bytes
    }

    /// Record an access to the byte range `[byte_offset, byte_offset + len)`.
    ///
    /// Increments heat for all pages overlapping the accessed range.
    /// Heat saturates at 255 (no overflow).
    pub fn record_access(&self, byte_offset: usize, len: usize) {
        if len == 0 || self.pages.is_empty() {
            return;
        }
        let end = byte_offset.saturating_add(len).min(self.total_bytes);
        let start_page = byte_offset / PAGE_SIZE;
        let end_page = end.saturating_sub(1) / PAGE_SIZE;

        for page in start_page..=end_page.min(self.pages.len() - 1) {
            // Saturating increment: load, add, store. Race conditions are
            // acceptable (heat is approximate).
            let current = self.pages[page].load(Ordering::Relaxed);
            if current < MAX_HEAT {
                self.pages[page].store(current.saturating_add(1), Ordering::Relaxed);
            }
        }
    }

    /// Apply exponential decay to all page heats.
    ///
    /// Called once per search cycle: `heat = (heat * decay_factor) as u8`.
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    pub fn decay(&self, decay_factor: f64) {
        let factor = if decay_factor.is_finite() {
            decay_factor.clamp(0.0, 1.0)
        } else {
            0.0 // Non-finite decay → zero out all heat (safe reset)
        };

        for page in &self.pages {
            let current = page.load(Ordering::Relaxed);
            if current > 0 {
                let decayed = (f64::from(current) * factor) as u8;
                page.store(decayed, Ordering::Relaxed);
            }
        }
    }

    /// Get the heat value for a specific page.
    ///
    /// Returns 0 if the page index is out of bounds.
    #[must_use]
    pub fn heat_at(&self, page_index: usize) -> u8 {
        self.pages
            .get(page_index)
            .map_or(0, |p| p.load(Ordering::Relaxed))
    }

    /// Get normalized heat (0.0-1.0) for a specific page.
    #[must_use]
    pub fn normalized_heat_at(&self, page_index: usize) -> f64 {
        f64::from(self.heat_at(page_index)) / f64::from(MAX_HEAT)
    }

    /// Return page indices with heat above `min_heat` (normalized 0.0-1.0),
    /// sorted by heat descending (hottest first), capped at `max_bytes` budget.
    #[must_use]
    pub fn hot_pages(&self, min_heat: f64, max_bytes: usize) -> Vec<usize> {
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let min_raw = (min_heat * f64::from(MAX_HEAT)) as u8;
        let max_pages = max_bytes / PAGE_SIZE;

        let mut hot: Vec<(usize, u8)> = self
            .pages
            .iter()
            .enumerate()
            .filter_map(|(idx, page)| {
                let heat = page.load(Ordering::Relaxed);
                if heat >= min_raw {
                    Some((idx, heat))
                } else {
                    None
                }
            })
            .collect();

        // Sort by heat descending (hottest first).
        hot.sort_unstable_by_key(|&(_, heat)| std::cmp::Reverse(heat));

        // Cap at budget.
        hot.truncate(max_pages);

        hot.into_iter().map(|(idx, _)| idx).collect()
    }

    /// Reset all heat values to zero.
    pub fn reset(&self) {
        for page in &self.pages {
            page.store(0, Ordering::Relaxed);
        }
    }

    /// Count of pages with non-zero heat.
    #[must_use]
    pub fn warm_page_count(&self) -> usize {
        self.pages
            .iter()
            .filter(|p| p.load(Ordering::Relaxed) > 0)
            .count()
    }
}

impl std::fmt::Debug for HeatMap {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HeatMap")
            .field("pages", &format_args!("[AtomicU8; {}]", self.pages.len()))
            .field("total_bytes", &self.total_bytes)
            .field("warm_pages", &self.warm_page_count())
            .finish()
    }
}

// ─── Warm-Up Execution ─────────────────────────────────────────────────────

/// Result of a warm-up operation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WarmUpResult {
    /// Number of pages touched.
    pub pages_touched: usize,
    /// Total bytes prefaulted.
    pub bytes_touched: usize,
    /// Strategy that was applied.
    pub strategy_name: String,
    /// Whether the budget was fully consumed.
    pub budget_exhausted: bool,
}

/// Warm up a byte slice by reading through targeted pages.
///
/// For heap-allocated data (e.g., `Vec<u8>` from `fs::read`), this ensures
/// pages are resident in the process's address space. For mmap'd data, this
/// triggers page faults that load pages from disk into the OS page cache.
///
/// The function reads one byte per targeted page, which is sufficient to
/// trigger a page fault and make the page resident.
#[must_use]
pub fn warm_up_bytes(
    data: &[u8],
    header_end: usize,
    config: &WarmUpConfig,
    heat_map: Option<&HeatMap>,
) -> WarmUpResult {
    if data.is_empty() {
        return empty_result(&config.strategy);
    }

    match &config.strategy {
        WarmUpStrategy::None => empty_result(&WarmUpStrategy::None),
        WarmUpStrategy::Full => warm_up_bytes_full(data, config),
        WarmUpStrategy::Header => warm_up_bytes_header(data, header_end, config),
        WarmUpStrategy::Adaptive(adaptive_config) => {
            warm_up_bytes_adaptive(data, header_end, config, adaptive_config, heat_map)
        }
    }
}

fn warm_up_bytes_full(data: &[u8], config: &WarmUpConfig) -> WarmUpResult {
    let max_pages = config.max_bytes / PAGE_SIZE;
    let total_pages = pages_for_bytes(data.len());
    let pages_to_touch = total_pages.min(max_pages);
    let touched = touch_pages(data, 0..pages_to_touch);
    let budget_exhausted = total_pages > max_pages;

    debug!(
        target: "frankensearch.warmup",
        pages_touched = touched,
        total_pages,
        budget_exhausted,
        "full warm-up complete"
    );

    WarmUpResult {
        pages_touched: touched,
        bytes_touched: touched * PAGE_SIZE,
        strategy_name: "full".into(),
        budget_exhausted,
    }
}

fn warm_up_bytes_header(data: &[u8], header_end: usize, config: &WarmUpConfig) -> WarmUpResult {
    let header_bytes = header_end.min(data.len());
    let header_pages = pages_for_bytes(header_bytes);
    let max_pages = config.max_bytes / PAGE_SIZE;
    let pages_to_touch = header_pages.min(max_pages);
    let touched = touch_pages(data, 0..pages_to_touch);

    debug!(
        target: "frankensearch.warmup",
        pages_touched = touched,
        header_bytes,
        "header warm-up complete"
    );

    WarmUpResult {
        pages_touched: touched,
        bytes_touched: touched * PAGE_SIZE,
        strategy_name: "header".into(),
        budget_exhausted: header_pages > max_pages,
    }
}

fn warm_up_bytes_adaptive(
    data: &[u8],
    header_end: usize,
    config: &WarmUpConfig,
    adaptive_config: &AdaptiveConfig,
    heat_map: Option<&HeatMap>,
) -> WarmUpResult {
    let header_fallback = || {
        warm_up_bytes(
            data,
            header_end,
            &WarmUpConfig {
                strategy: WarmUpStrategy::Header,
                ..*config
            },
            None,
        )
    };

    let Some(heat_map) = heat_map else {
        debug!(target: "frankensearch.warmup", "adaptive: no heat map, falling back to header");
        return header_fallback();
    };

    let min_heat = adaptive_config.clamped_min_heat();
    let hot_pages = heat_map.hot_pages(min_heat, config.max_bytes);

    if hot_pages.is_empty() {
        debug!(target: "frankensearch.warmup", "adaptive: no hot pages, falling back to header");
        return header_fallback();
    }

    let mut touched = 0;
    for &page in &hot_pages {
        let offset = page * PAGE_SIZE;
        if offset < data.len() {
            std::hint::black_box(data[offset]);
            touched += 1;
        }
    }

    let budget_pages = config.max_bytes / PAGE_SIZE;
    let budget_exhausted = hot_pages.len() >= budget_pages;

    debug!(
        target: "frankensearch.warmup",
        pages_touched = touched,
        hot_page_count = hot_pages.len(),
        min_heat,
        budget_exhausted,
        "adaptive warm-up complete"
    );

    WarmUpResult {
        pages_touched: touched,
        bytes_touched: touched * PAGE_SIZE,
        strategy_name: "adaptive".into(),
        budget_exhausted,
    }
}

/// Touch one byte per page in the given range to trigger page faults.
fn touch_pages(data: &[u8], page_range: std::ops::Range<usize>) -> usize {
    let mut touched = 0;
    for page in page_range {
        let offset = page * PAGE_SIZE;
        if offset < data.len() {
            std::hint::black_box(data[offset]);
            touched += 1;
        }
    }
    touched
}

/// Warm up a memory-mapped file using `madvise(MADV_WILLNEED)`.
///
/// This requests the OS kernel to asynchronously read targeted pages into the
/// page cache, avoiding page faults on subsequent access.
///
/// # Errors
///
/// Returns `Err` if the madvise call fails (unlikely in practice).
pub fn warm_up_mmap(
    mmap: &memmap2::Mmap,
    header_end: usize,
    config: &WarmUpConfig,
    heat_map: Option<&HeatMap>,
) -> Result<WarmUpResult, std::io::Error> {
    if mmap.is_empty() {
        return Ok(empty_result(&config.strategy));
    }

    match &config.strategy {
        WarmUpStrategy::None => Ok(empty_result(&WarmUpStrategy::None)),
        WarmUpStrategy::Full => mmap_warm_up_full(mmap, config),
        WarmUpStrategy::Header => mmap_warm_up_header(mmap, header_end, config),
        WarmUpStrategy::Adaptive(ac) => {
            mmap_warm_up_adaptive(mmap, header_end, config, ac, heat_map)
        }
    }
}

#[inline]
fn advise_willneed(mmap: &memmap2::Mmap, offset: usize, len: usize) -> Result<(), std::io::Error> {
    #[cfg(unix)]
    {
        mmap.advise_range(memmap2::Advice::WillNeed, offset, len)?;
        Ok(())
    }
    #[cfg(not(unix))]
    {
        let _ = (mmap, offset, len);
        Ok(())
    }
}

fn mmap_warm_up_full(
    mmap: &memmap2::Mmap,
    config: &WarmUpConfig,
) -> Result<WarmUpResult, std::io::Error> {
    let total_pages = pages_for_bytes(mmap.len());
    let max_pages = config.max_bytes / PAGE_SIZE;
    let budget_exhausted = total_pages > max_pages;
    let actual_bytes = (total_pages.min(max_pages) * PAGE_SIZE).min(mmap.len());

    if actual_bytes > 0 {
        advise_willneed(mmap, 0, actual_bytes)?;
    }

    let pages_touched = pages_for_bytes(actual_bytes);
    debug!(target: "frankensearch.warmup", pages_touched, total_pages, budget_exhausted, "mmap full warm-up");

    Ok(WarmUpResult {
        pages_touched,
        bytes_touched: actual_bytes,
        strategy_name: "full".into(),
        budget_exhausted,
    })
}

fn mmap_warm_up_header(
    mmap: &memmap2::Mmap,
    header_end: usize,
    config: &WarmUpConfig,
) -> Result<WarmUpResult, std::io::Error> {
    let header_bytes = header_end.min(mmap.len());
    let max_bytes = config.max_bytes.min(mmap.len());
    let actual = header_bytes.min(max_bytes);

    if actual > 0 {
        advise_willneed(mmap, 0, actual)?;
    }

    let pages_touched = pages_for_bytes(actual);
    debug!(target: "frankensearch.warmup", pages_touched, header_bytes, "mmap header warm-up");

    Ok(WarmUpResult {
        pages_touched,
        bytes_touched: actual,
        strategy_name: "header".into(),
        budget_exhausted: header_bytes > max_bytes,
    })
}

fn mmap_warm_up_adaptive(
    mmap: &memmap2::Mmap,
    header_end: usize,
    config: &WarmUpConfig,
    adaptive_config: &AdaptiveConfig,
    heat_map: Option<&HeatMap>,
) -> Result<WarmUpResult, std::io::Error> {
    let header_fallback = || {
        warm_up_mmap(
            mmap,
            header_end,
            &WarmUpConfig {
                strategy: WarmUpStrategy::Header,
                ..*config
            },
            None,
        )
    };

    let Some(heat_map) = heat_map else {
        return header_fallback();
    };

    let min_heat = adaptive_config.clamped_min_heat();
    let hot_pages = heat_map.hot_pages(min_heat, config.max_bytes);

    if hot_pages.is_empty() {
        return header_fallback();
    }

    let mut touched = 0;
    for &page in &hot_pages {
        let offset = page * PAGE_SIZE;
        let len = PAGE_SIZE.min(mmap.len().saturating_sub(offset));
        if len > 0 {
            advise_willneed(mmap, offset, len)?;
            touched += 1;
        }
    }

    let budget_pages = config.max_bytes / PAGE_SIZE;
    let budget_exhausted = hot_pages.len() >= budget_pages;

    debug!(
        target: "frankensearch.warmup",
        pages_touched = touched,
        hot_page_count = hot_pages.len(),
        budget_exhausted,
        "mmap adaptive warm-up"
    );

    Ok(WarmUpResult {
        pages_touched: touched,
        bytes_touched: touched * PAGE_SIZE,
        strategy_name: "adaptive".into(),
        budget_exhausted,
    })
}

// ─── Helpers ────────────────────────────────────────────────────────────────

/// Number of pages needed to cover `bytes`.
#[must_use]
const fn pages_for_bytes(bytes: usize) -> usize {
    bytes.div_ceil(PAGE_SIZE)
}

/// Create a zero-work result for empty data or no-op strategies.
fn empty_result(strategy: &WarmUpStrategy) -> WarmUpResult {
    WarmUpResult {
        pages_touched: 0,
        bytes_touched: 0,
        strategy_name: strategy_name(strategy),
        budget_exhausted: false,
    }
}

/// Human-readable strategy name.
fn strategy_name(strategy: &WarmUpStrategy) -> String {
    match strategy {
        WarmUpStrategy::None => "none".into(),
        WarmUpStrategy::Full => "full".into(),
        WarmUpStrategy::Header => "header".into(),
        WarmUpStrategy::Adaptive(_) => "adaptive".into(),
    }
}

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

    // ─── HeatMap basics ────────────────────────────────────────────────

    #[test]
    fn heat_map_page_count() {
        assert_eq!(HeatMap::new(0).page_count(), 0);
        assert_eq!(HeatMap::new(1).page_count(), 1);
        assert_eq!(HeatMap::new(4096).page_count(), 1);
        assert_eq!(HeatMap::new(4097).page_count(), 2);
        assert_eq!(HeatMap::new(1_000_000).page_count(), 245);
    }

    #[test]
    fn heat_map_record_and_read() {
        let hm = HeatMap::new(100_000);
        assert_eq!(hm.heat_at(0), 0);

        // Access first page.
        hm.record_access(0, 100);
        assert_eq!(hm.heat_at(0), 1);

        // Access again — heat increments.
        hm.record_access(500, 200);
        assert_eq!(hm.heat_at(0), 2);

        // Access second page.
        hm.record_access(4096, 10);
        assert_eq!(hm.heat_at(1), 1);

        // First page unchanged.
        assert_eq!(hm.heat_at(0), 2);
    }

    #[test]
    fn heat_map_spanning_access() {
        let hm = HeatMap::new(20_000);
        // Access spanning pages 0, 1, and 2.
        hm.record_access(3000, 6000); // 3000..9000 → pages 0, 1, 2
        assert_eq!(hm.heat_at(0), 1);
        assert_eq!(hm.heat_at(1), 1);
        assert_eq!(hm.heat_at(2), 1);
        assert_eq!(hm.heat_at(3), 0);
    }

    #[test]
    fn heat_map_saturates_at_max() {
        let hm = HeatMap::new(4096);
        for _ in 0..300 {
            hm.record_access(0, 1);
        }
        assert_eq!(hm.heat_at(0), MAX_HEAT);
    }

    #[test]
    fn heat_map_decay() {
        let hm = HeatMap::new(4096);
        for _ in 0..100 {
            hm.record_access(0, 1);
        }
        assert_eq!(hm.heat_at(0), 100);

        hm.decay(0.5);
        assert_eq!(hm.heat_at(0), 50);

        hm.decay(0.5);
        assert_eq!(hm.heat_at(0), 25);

        // Decay to zero.
        for _ in 0..20 {
            hm.decay(0.5);
        }
        assert_eq!(hm.heat_at(0), 0);
    }

    #[test]
    fn heat_map_decay_zero_factor() {
        let hm = HeatMap::new(4096);
        hm.record_access(0, 1);
        assert_eq!(hm.heat_at(0), 1);

        hm.decay(0.0);
        assert_eq!(hm.heat_at(0), 0);
    }

    #[test]
    fn heat_map_decay_one_factor() {
        let hm = HeatMap::new(4096);
        for _ in 0..50 {
            hm.record_access(0, 1);
        }
        assert_eq!(hm.heat_at(0), 50);

        hm.decay(1.0);
        assert_eq!(hm.heat_at(0), 50);
    }

    #[test]
    fn heat_map_hot_pages_sorted_by_heat() {
        let hm = HeatMap::new(20_000); // 5 pages

        // Page 0: 10 accesses.
        for _ in 0..10 {
            hm.record_access(0, 1);
        }
        // Page 2: 50 accesses.
        for _ in 0..50 {
            hm.record_access(8192, 1);
        }
        // Page 4: 30 accesses.
        for _ in 0..30 {
            hm.record_access(16384, 1);
        }

        let hot = hm.hot_pages(0.01, usize::MAX);
        assert_eq!(hot.len(), 3);
        // Sorted by heat descending: page 2 (50), page 4 (30), page 0 (10).
        assert_eq!(hot[0], 2);
        assert_eq!(hot[1], 4);
        assert_eq!(hot[2], 0);
    }

    #[test]
    fn heat_map_hot_pages_respects_min_heat() {
        let hm = HeatMap::new(8192); // 2 pages
        for _ in 0..100 {
            hm.record_access(0, 1);
        }
        hm.record_access(4096, 1);

        // min_heat = 0.2 → min_raw = 51. Page 0 (100) passes, page 1 (1) does not.
        let hot = hm.hot_pages(0.2, usize::MAX);
        assert_eq!(hot.len(), 1);
        assert_eq!(hot[0], 0);
    }

    #[test]
    fn heat_map_hot_pages_respects_budget() {
        let hm = HeatMap::new(20_000);
        for page in 0..5 {
            for _ in 0..10 {
                hm.record_access(page * PAGE_SIZE, 1);
            }
        }

        // Budget for 2 pages only.
        let hot = hm.hot_pages(0.01, 2 * PAGE_SIZE);
        assert_eq!(hot.len(), 2);
    }

    #[test]
    fn heat_map_reset() {
        let hm = HeatMap::new(20_000);
        for page in 0..5 {
            hm.record_access(page * PAGE_SIZE, 1);
        }
        assert_eq!(hm.warm_page_count(), 5);

        hm.reset();
        assert_eq!(hm.warm_page_count(), 0);
    }

    #[test]
    fn heat_map_normalized_heat() {
        let hm = HeatMap::new(4096);
        assert!((hm.normalized_heat_at(0) - 0.0).abs() < f64::EPSILON);

        for _ in 0..255 {
            hm.record_access(0, 1);
        }
        assert!((hm.normalized_heat_at(0) - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn heat_map_empty_index() {
        let hm = HeatMap::new(0);
        assert_eq!(hm.page_count(), 0);
        assert_eq!(hm.warm_page_count(), 0);
        hm.record_access(0, 100); // Should not panic.
        hm.decay(0.5); // Should not panic.
        assert!(hm.hot_pages(0.0, usize::MAX).is_empty());
    }

    #[test]
    fn heat_map_access_beyond_bounds() {
        let hm = HeatMap::new(4096);
        // Access beyond the tracked region — should be clamped, not panic.
        hm.record_access(10_000, 100);
        assert_eq!(hm.heat_at(0), 0);
    }

    #[test]
    fn heat_map_zero_length_access() {
        let hm = HeatMap::new(4096);
        hm.record_access(0, 0);
        assert_eq!(hm.heat_at(0), 0);
    }

    #[test]
    fn heat_map_index_smaller_than_page() {
        // Index smaller than one page (< 4 KB).
        let hm = HeatMap::new(100);
        assert_eq!(hm.page_count(), 1);
        hm.record_access(0, 50);
        assert_eq!(hm.heat_at(0), 1);
    }

    // ─── Warm-up execution ─────────────────────────────────────────────

    #[test]
    fn warm_up_none_is_noop() {
        let data = vec![0u8; 10_000];
        let result = warm_up_bytes(&data, 100, &WarmUpConfig::default(), None);
        assert_eq!(result.pages_touched, 0);
        assert_eq!(result.bytes_touched, 0);
        assert_eq!(result.strategy_name, "none");
        assert!(!result.budget_exhausted);
    }

    #[test]
    fn warm_up_full_touches_all_pages() {
        let data = vec![42u8; 20_000]; // 5 pages
        let config = WarmUpConfig::full();
        let result = warm_up_bytes(&data, 100, &config, None);
        assert_eq!(result.pages_touched, 5);
        assert_eq!(result.strategy_name, "full");
        assert!(!result.budget_exhausted);
    }

    #[test]
    fn warm_up_full_respects_budget() {
        let data = vec![42u8; 20_000]; // 5 pages
        let config = WarmUpConfig {
            strategy: WarmUpStrategy::Full,
            max_bytes: 2 * PAGE_SIZE, // Budget for 2 pages.
            parallel_readers: 1,
        };
        let result = warm_up_bytes(&data, 100, &config, None);
        assert_eq!(result.pages_touched, 2);
        assert!(result.budget_exhausted);
    }

    #[test]
    fn warm_up_header_only_touches_header() {
        let data = vec![42u8; 100_000]; // Many pages
        let header_end = 5000; // Header spans ~2 pages.
        let config = WarmUpConfig::header_only();
        let result = warm_up_bytes(&data, header_end, &config, None);
        assert_eq!(result.pages_touched, 2); // ceil(5000 / 4096) = 2
        assert_eq!(result.strategy_name, "header");
    }

    #[test]
    fn warm_up_adaptive_uses_heat_map() {
        let data = vec![42u8; 100_000]; // ~25 pages
        let hm = HeatMap::new(data.len());

        // Heat up pages 3 and 7.
        for _ in 0..50 {
            hm.record_access(3 * PAGE_SIZE, 100);
            hm.record_access(7 * PAGE_SIZE, 100);
        }

        let config = WarmUpConfig::adaptive();
        let result = warm_up_bytes(&data, 100, &config, Some(&hm));
        assert_eq!(result.pages_touched, 2);
        assert_eq!(result.strategy_name, "adaptive");
    }

    #[test]
    fn warm_up_adaptive_falls_back_without_heat_map() {
        let data = vec![42u8; 20_000];
        let config = WarmUpConfig::adaptive();
        let result = warm_up_bytes(&data, 5000, &config, None);
        // Falls back to header strategy.
        assert_eq!(result.strategy_name, "header");
        assert_eq!(result.pages_touched, 2);
    }

    #[test]
    fn warm_up_adaptive_falls_back_with_empty_heat_map() {
        let data = vec![42u8; 20_000];
        let hm = HeatMap::new(data.len());
        // No accesses recorded → empty heat map → falls back to header.
        let config = WarmUpConfig::adaptive();
        let result = warm_up_bytes(&data, 5000, &config, Some(&hm));
        assert_eq!(result.strategy_name, "header");
    }

    #[test]
    fn warm_up_empty_data() {
        let data: Vec<u8> = vec![];
        let result = warm_up_bytes(&data, 0, &WarmUpConfig::full(), None);
        assert_eq!(result.pages_touched, 0);
    }

    // ─── Config ────────────────────────────────────────────────────────

    #[test]
    fn config_defaults() {
        let config = WarmUpConfig::default();
        assert!(matches!(config.strategy, WarmUpStrategy::None));
        assert_eq!(config.max_bytes, 256 * 1024 * 1024);
        assert_eq!(config.parallel_readers, 2);
    }

    #[test]
    fn adaptive_config_clamping() {
        let ac = AdaptiveConfig {
            heat_decay: -0.5,
            min_heat: 2.0,
        };
        assert!((ac.clamped_heat_decay() - 0.0).abs() < f64::EPSILON);
        assert!((ac.clamped_min_heat() - 1.0).abs() < f64::EPSILON);

        let ac2 = AdaptiveConfig {
            heat_decay: 1.5,
            min_heat: -0.3,
        };
        assert!((ac2.clamped_heat_decay() - 1.0).abs() < f64::EPSILON);
        assert!((ac2.clamped_min_heat() - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn config_serde_roundtrip() {
        let config = WarmUpConfig::adaptive();
        let json = serde_json::to_string(&config).unwrap();
        let deserialized: WarmUpConfig = serde_json::from_str(&json).unwrap();
        assert!(matches!(deserialized.strategy, WarmUpStrategy::Adaptive(_)));
        assert_eq!(deserialized.max_bytes, config.max_bytes);
    }

    // ─── HeatMap concurrent access ─────────────────────────────────────

    #[test]
    fn heat_map_concurrent_access_no_panic() {
        use std::sync::Arc;

        let hm = Arc::new(HeatMap::new(100_000));
        let handles: Vec<_> = (0..4)
            .map(|t| {
                let hm = Arc::clone(&hm);
                std::thread::spawn(move || {
                    for i in 0..1000 {
                        hm.record_access((t * 10_000 + i * 10) % 100_000, 100);
                    }
                })
            })
            .collect();

        for h in handles {
            h.join().expect("thread should not panic");
        }

        // Some pages should have heat.
        assert!(hm.warm_page_count() > 0);
    }

    // ─── Pages-for-bytes helper ────────────────────────────────────────

    #[test]
    fn pages_for_bytes_calculation() {
        assert_eq!(pages_for_bytes(0), 0);
        assert_eq!(pages_for_bytes(1), 1);
        assert_eq!(pages_for_bytes(4095), 1);
        assert_eq!(pages_for_bytes(4096), 1);
        assert_eq!(pages_for_bytes(4097), 2);
        assert_eq!(pages_for_bytes(8192), 2);
        assert_eq!(pages_for_bytes(1_073_741_824), 262_144); // 1 GB
    }

    // ─── HeatMap debug formatting ──────────────────────────────────────

    #[test]
    fn heat_map_debug() {
        let hm = HeatMap::new(20_000);
        hm.record_access(0, 100);
        let debug = format!("{hm:?}");
        assert!(debug.contains("HeatMap"));
        assert!(debug.contains("AtomicU8; 5"));
        assert!(debug.contains("warm_pages: 1"));
    }

    // ─── Warm-up result ────────────────────────────────────────────────

    #[test]
    fn warm_up_result_serde() {
        let result = WarmUpResult {
            pages_touched: 10,
            bytes_touched: 40960,
            strategy_name: "adaptive".into(),
            budget_exhausted: false,
        };
        let json = serde_json::to_string(&result).unwrap();
        let deserialized: WarmUpResult = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.pages_touched, 10);
        assert_eq!(deserialized.strategy_name, "adaptive");
    }

    #[test]
    fn adaptive_config_nan_heat_decay_uses_default() {
        let config = AdaptiveConfig {
            heat_decay: f64::NAN,
            min_heat: 0.1,
        };
        let decay = config.clamped_heat_decay();
        assert!(decay.is_finite(), "NaN heat_decay must fallback to default");
        assert!((decay - 0.95).abs() < f64::EPSILON);
    }

    #[test]
    fn adaptive_config_nan_min_heat_uses_default() {
        let config = AdaptiveConfig {
            heat_decay: 0.85,
            min_heat: f64::NAN,
        };
        let min_heat = config.clamped_min_heat();
        assert!(
            min_heat.is_finite(),
            "NaN min_heat must fallback to default"
        );
        assert!((min_heat - 0.1).abs() < f64::EPSILON);
    }

    #[test]
    fn heat_map_decay_nan_factor_zeroes_heat() {
        let map = HeatMap::new(PAGE_SIZE * 4);
        map.record_access(0, PAGE_SIZE);
        assert!(map.heat_at(0) > 0, "heat should be recorded");

        map.decay(f64::NAN);
        // NaN decay factor → falls back to 0.0 → heat zeroed
        assert_eq!(map.heat_at(0), 0, "NaN decay factor should zero out heat");
    }
}