Dynamic descriptor slots: the search goes past the cursor as far as it should

The scan was cut to the heap's length, which left out the slots past the
cursor a run that crosses it needs; upstream's corrected test (072fde0).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 23:52:47 -06:00
1 parent 4e3e0654bf
commit 1a93f0fdd1
1 file changed
+22 -15
+22 -15
View File
@@ -870,7 +870,7 @@ impl HostHeap {
let mut run = 0; let mut run = 0;
let mut found = None; let mut found = None;
let mut last = usize::MAX; let mut last = usize::MAX;
for at in starts.take(if enough { DYNAMIC as usize } else { 0 }) { for at in starts.take(if enough { usize::MAX } else { 0 }) {
// A run cannot wrap past the end. // A run cannot wrap past the end.
if at != last.wrapping_add(1) { if at != last.wrapping_add(1) {
run = 0; run = 0;
@@ -2841,23 +2841,30 @@ mod tests {
assert_eq!(heap.dynamic(40), Some(4 + heap.dynamic - 40)); assert_eq!(heap.dynamic(40), Some(4 + heap.dynamic - 40));
} }
/// Contiguous free dynamic slot ranges that cross the search cursor are found.
#[test] #[test]
fn dynamic_allocation_finds_contiguous_range_crossing_search_cursor() { fn dynamic_search_can_find_a_run_across_the_cursor() {
let Some(heap) = HostHeap::new(false, 4) else { let Some(heap) = HostHeap::new(false, 4) else {
return; // no host GPU eprintln!("no host descriptor heap; skipped");
return;
}; };
// Allocate slots 0..8, advancing cursor to 8. let first = heap.dynamic(1).unwrap();
let a = heap.dynamic(8).unwrap(); let left = heap.dynamic(2).unwrap();
// Allocate slots 8..16, advancing cursor to 16. let right = heap.dynamic(2).unwrap();
let b = heap.dynamic(8).unwrap(); let rest = heap.dynamic(heap.dynamic - 5).unwrap();
assert_eq!((a, b), (4, 12)); assert_eq!((first, left, right, rest), (4, 5, 7, 9));
// Release slots 10..14 (crossing cursor at 12). // Reallocating the left half puts the cursor back in the middle.
// Before the fix, a request of 4 slots starting search at 12 scanned 12..end then 0..12, heap.release(left, 2);
// missing the contiguous run at 10..14. assert_eq!(heap.dynamic(2), Some(left));
heap.release(14, 4); heap.release(left, 2);
let c = heap.dynamic(4); heap.release(right, 2);
assert_eq!(c, Some(14)); assert_eq!(heap.dynamic(4), Some(left));
assert_eq!(heap.dynamic(1), None);
// A free slot at each physical end does not form a two-slot range.
heap.release(first, 1);
let last = first + heap.dynamic - 1;
heap.release(last, 1);
assert_eq!(heap.dynamic(2), None);
} }
} }