diff --git a/crates/ml-features/src/mbp10_loader.rs b/crates/ml-features/src/mbp10_loader.rs index 08273ffc4..b49decd36 100644 --- a/crates/ml-features/src/mbp10_loader.rs +++ b/crates/ml-features/src/mbp10_loader.rs @@ -363,22 +363,14 @@ pub fn get_snapshots_for_timestamp( return &[]; } - // Find the first snapshot with timestamp >= target_ts - let mut start_idx = None; - for (i, snap) in snapshots.iter().enumerate() { - if snap.timestamp >= target_ts { - start_idx = Some(i); - break; - } - } - - match start_idx { - Some(idx) => { - let end_idx = (idx + window_size).min(snapshots.len()); - &snapshots[idx..end_idx] - } - None => &[], // Target timestamp is after all snapshots + // Binary search for the first snapshot with timestamp >= target_ts + // Snapshots are pre-sorted by timestamp (from DBN file ordering) + let idx = snapshots.partition_point(|s| s.timestamp < target_ts); + if idx >= snapshots.len() { + return &[]; // Target timestamp is after all snapshots } + let end_idx = (idx + window_size).min(snapshots.len()); + &snapshots[idx..end_idx] } /// Get the most recent N snapshots ending at the given index