diff --git a/crates/victory-gpu/src/shadow.rs b/crates/victory-gpu/src/shadow.rs index 0020ed5..7c9b773 100644 --- a/crates/victory-gpu/src/shadow.rs +++ b/crates/victory-gpu/src/shadow.rs @@ -1412,9 +1412,36 @@ fn last_difference(a: &[u8], b: &[u8]) -> Option { .map(|i| chunk * DIFF_CHUNK + i) } +/// Cut backwards to a piece boundary inside the span being scanned. The +/// first cut of a span that does not start on a piece is the span's start: +/// subtracting its offset into the piece would run below it (and wrap). +fn scan_boundary(start: usize, from: usize, at: usize) -> usize { + at.saturating_sub((start + at) % PIECE).max(from) +} + #[cfg(test)] mod difference_tests { - use super::{first_difference, last_difference}; + use super::{PIECE, first_difference, last_difference, scan_boundary}; + + #[test] + fn unaligned_parallel_scan_covers_the_first_piece() { + let span = 48 << 20; + for start in [0, 1, 31, PIECE - 1] { + for from in [0, 1, 31, PIECE - 1, PIECE + 3] { + let end = from + span; + let cuts = [ + scan_boundary(start, from, from), + scan_boundary(start, from, from + span / 3), + scan_boundary(start, from, from + 2 * span / 3), + end, + ]; + assert_eq!(cuts[0], from); + assert!(cuts.windows(2).all(|pair| pair[0] < pair[1])); + assert_eq!((start + cuts[1]) % PIECE, 0); + assert_eq!((start + cuts[2]) % PIECE, 0); + } + } + } /// The chunk searches agree with a byte-wise scan across chunk boundaries. #[test] @@ -3766,7 +3793,7 @@ impl Shadow { return to; } let at = from + (to - from) / threads * i; - (at - (start + at) % PIECE).max(from) + scan_boundary(start, from, at) }; let scan = &scan; std::thread::scope(|s| {