From 2156e42d6e6a69878539b12641a7dfe52556368a Mon Sep 17 00:00:00 2001 From: Prime <215761102+Prime1997@users.noreply.github.com> Date: Sat, 28 Nov 2026 03:47:23 -0800 Subject: [PATCH] D3D12.X: retire idle command pools without waiting for presents Adapt iggyvilla PR #3110 to nvidia-dev. The narrow pending-binding retirement prerequisite addresses the ordering problem also covered by #2138/#2707. Keep live recordings and unfenced work owned; return to primary only after Reset succeeds. Four CPU fixtures added but not run. Independently source reviewed; no measured memory reduction claimed. Assisted-by: Codex (GPT-6) --- README.md | 4 + crates/xcl/src/hle/d3d12x/alloc_pool.rs | 184 +++++++++++++++++++++++- crates/xcl/src/hle/d3d12x/bind.rs | 3 + crates/xcl/src/hle/d3d12x/list_x.rs | 3 +- crates/xcl/src/hle/d3d12x/real.rs | 2 +- crates/xcl/src/hle/d3d12x/title_list.rs | 84 ++++++++++- 6 files changed, 266 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 978c73b613..0096267fa6 100644 --- a/README.md +++ b/README.md @@ -355,6 +355,10 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper. line with what the title did in that time (presents, submits, draws, dispatches, frame-event waits, DirectStorage requests), and a `memory:` line with the process's commit and the video memory in use. Where the lines stop changing is where the title stopped. + Idle spare command lists and XCL-owned alternate command allocators are reclaimed every + two seconds, including while loading stops presenting. Live recordings, unfinished + submissions and failed fence signals retain their owners. The `command pools` line reports + remaining and retired object counts, not measured memory savings. - `XCL_THREAD_DUMP_AT=s1,s2,…`: at each of those seconds after the start, the state of every thread of the title goes into `xcl.log`, for a title that stalls. - `XCL_TRACE_FAULTS=1` (any value): the first 32 access violations are logged as they are raised diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs index ea3794dc93..e2230e7ec9 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -26,6 +26,9 @@ use crate::util::HRESULT; struct Backing { /// PC lists whose recording is on this allocator users: HashSet, + /// Lists detached from this backing whose old binding blocks have not been retired yet. + retiring: HashSet, + unfenced: bool, /// queue → (fence, value after the last submit of one of them) marks: HashMap, } @@ -33,11 +36,11 @@ struct Backing { impl Backing { /// The GPU has passed every submit of a recording on this allocator. unsafe fn idle(&self) -> bool { - self.marks.values().all(|(f, v)| super::real::fence_completed(f) >= *v) + !self.unfenced && self.marks.values().all(|(f, v)| completed(super::real::fence_completed(f), *v)) } unsafe fn free(&self) -> bool { - self.users.is_empty() && self.idle() + self.users.is_empty() && self.retiring.is_empty() && self.idle() } } @@ -49,6 +52,8 @@ struct State { aside: HashMap>, backings: HashMap, of_list: HashMap, + retiring: HashMap>, + unfenced: HashSet, /// title allocator → the type of the lists on it types: HashMap, fences: HashMap, @@ -72,6 +77,71 @@ unsafe impl Send for State {} static STATE: Mutex> = Mutex::new(None); +fn completed(value: u64, wanted: u64) -> bool { + value != u64::MAX && value >= wanted +} + +/// Independent of PresentX: loading can wait minutes for a pipeline without presenting. +pub(super) fn start_retirement() { + #[cfg(not(test))] + { + static START: std::sync::Once = std::sync::Once::new(); + START.call_once(|| { + let _ = std::thread::Builder::new().name("xcl-command-retire".into()).spawn(|| { + let (mut lists, mut allocators, mut tick) = (0usize, 0usize, 0usize); + loop { + std::thread::sleep(std::time::Duration::from_secs(2)); + let (parked, retired) = unsafe { super::title_list::trim_idle_spares(2) }; + let (owners, aside, idle, freed) = unsafe { trim_idle_aside(1) }; + lists += retired; + allocators += freed; + tick += 1; + if tick % 10 == 0 && std::env::var_os("XCL_ACTIVITY").is_some() { + crate::xlog_info!("command pools: {parked} spare lists parked, {lists} retired; {owners} allocator owners, {aside} put aside ({idle} idle), {allocators} retired (objects, not bytes)"); + } + } + }); + }); + } +} + +/// Detach only XCL-owned, unused alternate allocators. The title's primary and the current +/// backing never leave here; no Reset or COM Release runs while STATE is held. +unsafe fn take_idle_aside(s: &mut State, keep: usize) -> (Vec, usize) { + let mut out = Vec::new(); + let mut idle = 0; + for (&owner, aside) in &mut s.aside { + let current = s.cur.get(&owner).copied().unwrap_or(owner); + let mut kept = 0; + aside.retain(|&raw| { + let free = raw != owner && raw != current && s.backings.get(&raw).is_some_and(|b| b.free()); + idle += usize::from(free); + if !free { return true; } + kept += 1; + if kept <= keep { return true; } + s.backings.remove(&raw); + out.push(raw); + false + }); + } + (out, idle) +} + +unsafe fn trim_idle_aside(keep: usize) -> (usize, usize, usize, usize) { + let (victims, owners, aside, idle) = { + let mut g = STATE.lock().unwrap(); + let Some(s) = g.as_mut() else { return (0, 0, 0, 0) }; + let (victims, idle) = take_idle_aside(s, keep); + (victims, s.aside.len(), s.aside.values().map(Vec::len).sum(), idle) + }; + let n = victims.len(); + for raw in victims { + super::console_list::allocator_reset(raw as *mut c_void); + drop(ID3D12CommandAllocator::from_raw(raw as *mut c_void)); + } + (owners, aside, idle.saturating_sub(n), n) +} + /// `XCL_ALLOC_SWAP=0` turns it off. pub fn enabled() -> bool { static ON: std::sync::OnceLock = std::sync::OnceLock::new(); @@ -83,6 +153,8 @@ fn move_list(s: &mut State, list: usize, backing: usize) { if old != backing { if let Some(b) = s.backings.get_mut(&old) { b.users.remove(&list); + b.retiring.insert(list); + s.retiring.entry(list).or_default().push(old); } } } @@ -95,8 +167,10 @@ pub(super) fn forget_list(list: usize) { let mut g = STATE.lock().unwrap(); let Some(s) = g.as_mut() else { return }; s.last_submit.remove(&list); + s.unfenced.remove(&list); s.recordings.remove(&list); unpin(s, list); + retirement_done_in(s, list); } /// The PC list's recording ended but the list stays (a split segment moved to the spares): its @@ -112,6 +186,22 @@ fn unpin(s: &mut State, list: usize) { if let Some(backing) = s.of_list.remove(&list) { if let Some(b) = s.backings.get_mut(&backing) { b.users.remove(&list); + b.retiring.insert(list); + s.retiring.entry(list).or_default().push(backing); + } + } +} + +/// Binding blocks have reached their fence-gated owner before the old allocator can leave. +pub(super) fn retirement_done(list: usize) { + let mut g = STATE.lock().unwrap(); + if let Some(s) = g.as_mut() { retirement_done_in(s, list); } +} + +fn retirement_done_in(s: &mut State, list: usize) { + if let Some(old) = s.retiring.remove(&list) { + for backing in old { + if let Some(b) = s.backings.get_mut(&backing) { b.retiring.remove(&list); } } } } @@ -127,6 +217,7 @@ pub(super) fn park_list(list: usize) { b.users.remove(&list); } } + retirement_done_in(s, list); } /// Reset `list` on the title's allocator `alloc`, committing its new recording association @@ -293,7 +384,15 @@ unsafe fn wait_for_previous_submits_within(lists: &[*mut c_void], limit: std::ti /// (`fence_completed`: XCL's own fence, without the title's hook, STATE held) pub fn in_flight(list: usize) -> bool { let g = STATE.lock().unwrap(); - g.as_ref().and_then(|s| s.last_submit.get(&list)).is_some_and(|l| unsafe { super::real::fence_completed(&l.fence) } < l.value) + g.as_ref().is_some_and(|s| s.unfenced.contains(&list) || s.last_submit.get(&list).is_some_and(|l| !completed(unsafe { super::real::fence_completed(&l.fence) }, l.value))) +} + +fn unfenced_submit(s: &mut State, lists: &[*mut c_void]) { + for &raw in lists { + let list = raw as usize; + s.unfenced.insert(list); + if let Some(backing) = s.of_list.get(&list).and_then(|b| s.backings.get_mut(b)) { backing.unfenced = true; } + } } /// `lists` were just submitted to `queue`. @@ -313,6 +412,7 @@ pub unsafe fn executed(queue: *mut c_void, lists: &[*mut c_void]) { let mut g = STATE.lock().unwrap(); let s = g.get_or_insert_with(State::default); let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&queue) else { + unfenced_submit(s, lists); drop(g); if !lists.is_empty() { super::bind::two_pass_submitted(queue, None, 0, lists); @@ -323,6 +423,7 @@ pub unsafe fn executed(queue: *mut c_void, lists: &[*mut c_void]) { if !s.fences.contains_key(&key) { let mut dev: Option = None; let Some(f) = q.GetDevice(&mut dev).ok().and(dev).and_then(|d| d.CreateFence::(0, D3D12_FENCE_FLAG_NONE).ok()) else { + unfenced_submit(s, lists); drop(g); if !lists.is_empty() { super::bind::two_pass_submitted(queue, None, 0, lists); @@ -333,14 +434,16 @@ pub unsafe fn executed(queue: *mut c_void, lists: &[*mut c_void]) { } let (fence, value) = { let (f, v) = s.fences.get_mut(&key).unwrap(); - if q.Signal(&*f, *v + 1).is_err() { + let next = v.checked_add(1).filter(|&value| value != u64::MAX); + if next.is_none() || q.Signal(&*f, next.unwrap()).is_err() { + unfenced_submit(s, lists); drop(g); if !lists.is_empty() { super::bind::two_pass_submitted(queue, None, 0, lists); } return; } - *v += 1; + *v = next.unwrap(); (f.clone(), *v) }; for &l in lists { @@ -360,6 +463,15 @@ pub unsafe fn executed(queue: *mut c_void, lists: &[*mut c_void]) { /// recorded on it are released (`list_x::on_release`), and `forget` gives back only what nothing /// holds. pub(super) fn unpin_users(alloc: usize) { + // Called only when the owning list is gone: its segments go next. Retire their binding + // blocks before detaching them; the allocator may otherwise leave on the sweep thread. + let users: HashSet = { + let g = STATE.lock().unwrap(); + let Some(s) = g.as_ref() else { return }; + s.cur.get(&alloc).into_iter().chain(s.aside.get(&alloc).into_iter().flatten()).chain([&alloc]) + .filter_map(|b| s.backings.get(b)).flat_map(|b| b.users.iter().chain(&b.retiring)).copied().collect() + }; + for list in users { super::bind::forget(list as *mut c_void); } let mut g = STATE.lock().unwrap(); if let Some(s) = g.as_mut() { unpin_users_in(s, alloc); @@ -436,6 +548,7 @@ pub unsafe fn forget(alloc: usize) { /// submits the GPU has passed – not for one put aside because the GPU still runs what was /// recorded on it (#1358). pub unsafe fn title_reset(alloc: *mut c_void) -> Option { + if enabled() { start_retirement(); } title_reset_with(alloc, |raw| super::real::reset_pc_allocator(raw)) } @@ -463,7 +576,7 @@ unsafe fn done_with(s: &State, a: usize) -> Vec { let cur = s.cur.get(&a).copied().unwrap_or(a); std::iter::once(cur) .chain(s.aside.get(&a).into_iter().flatten().copied()) - .filter(|b| s.backings.get(b).is_none_or(|x| x.idle())) + .filter(|b| s.backings.get(b).is_none_or(|x| x.retiring.is_empty() && x.idle())) .collect() } @@ -472,6 +585,19 @@ unsafe fn title_reset_in(s: &mut State, alloc: *mut c_void, mut reset: impl FnMu let a = alloc as usize; let c = s.cur.get(&a).copied().unwrap_or(a); if s.backings.get(&c).is_none_or(|b| b.free()) { + // Return to the primary only after its Reset succeeds. Keep the old alternate in + // aside for the retirement thread; dropping it here would re-enter STATE. + if c != a && s.aside.get(&a).is_some_and(|v| v.contains(&a)) + && s.backings.get(&a).is_some_and(|b| b.free()) + { + let hr = reset(alloc); + if hr < 0 { return Some(hr); } + s.aside.get_mut(&a).unwrap().retain(|&b| b != a); + s.aside.get_mut(&a).unwrap().push(c); + s.cur.remove(&a); + s.backings.get_mut(&a).unwrap().marks.clear(); + return Some(hr); + } if let Some(b) = s.backings.get_mut(&c) { b.marks.clear(); } @@ -528,6 +654,52 @@ unsafe fn title_reset_in(s: &mut State, alloc: *mut c_void, mut reset: impl FnMu mod tests { use super::*; + #[test] + fn idle_reclaim_waits_for_binding_retirement_and_keeps_live_owners() { + let mut s = State::default(); + s.cur.insert(1, 2); + s.aside.insert(1, vec![1, 3, 4, 5, 6]); + for b in 1..=6 { s.backings.insert(b, Backing::default()); } + move_list(&mut s, 10, 3); + move_list(&mut s, 10, 2); // old binding blocks have not reached their owner yet + move_list(&mut s, 11, 4); // another recording is still live + unfenced_submit(&mut s, &[11usize as *mut c_void]); + unpin(&mut s, 11); + retirement_done_in(&mut s, 11); // failed Signal still prevents release + let (first, idle) = unsafe { take_idle_aside(&mut s, 1) }; + assert_eq!((first, idle), (vec![6], 2)); + assert_eq!(s.aside[&1], [1, 3, 4, 5]); + retirement_done_in(&mut s, 10); + assert_eq!(unsafe { take_idle_aside(&mut s, 0) }.0, [3, 5]); + assert_eq!(s.aside[&1], [1, 4], "title primary and unfenced backing stay owned"); + assert_eq!(s.cur[&1], 2); + assert!(s.backings[&2].users.contains(&10)); + } + + #[test] + fn returning_to_primary_keeps_the_alternate_until_reset_succeeds() { + let mut s = State::default(); + s.cur.insert(1, 2); + s.aside.insert(1, vec![1]); + s.backings.insert(1, Backing::default()); + s.backings.insert(2, Backing::default()); + let a = 1usize as *mut c_void; + assert_eq!(unsafe { title_reset_in(&mut s, a, |_| crate::util::E_FAIL) }, Some(crate::util::E_FAIL)); + assert_eq!((s.cur[&1], &s.aside[&1]), (2, &vec![1])); + assert_eq!(unsafe { title_reset_in(&mut s, a, |raw| { assert_eq!(raw, a); 0 }) }, Some(0)); + assert!(!s.cur.contains_key(&1)); + assert_eq!(s.aside[&1], [2], "old alternate remains owned until the external release pass"); + assert_eq!(unsafe { take_idle_aside(&mut s, 0) }.0, [2]); + } + + #[test] + fn device_removal_is_not_allocator_completion() { + assert!(!completed(u64::MAX, 7)); + assert!(!completed(6, 7)); + assert!(completed(7, 7)); + assert!(!unsafe { Backing { unfenced: true, ..Default::default() }.idle() }); + } + /// When a title list goes, the segments that recorded its put-off dispatches on its own /// allocator hold neither the PC allocator it stands for now nor the ones put aside: `forget` /// then releases those the GPU is done with (they were left behind, with what was held on them). diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index e1608db069..ba5bb7fd6d 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -881,6 +881,7 @@ pub(super) fn reset(list: *mut c_void, allocator: usize, initial_pso: usize) { unmark_deferring(list); console_list::hold_until_reset(old, blocks); retire_recording_res(old, res_bufs, res_marks, res_untracked); + super::alloc_pool::retirement_done(list as usize); } impl State { @@ -946,6 +947,7 @@ pub(super) fn inherit_for_split(from: *mut c_void, to: *mut c_void, allocator: u unmark_deferring(to); console_list::hold_until_reset(old, blocks); retire_recording_res(old, res_bufs, res_marks, res_untracked); + super::alloc_pool::retirement_done(to as usize); } /// The title changed what descriptor heaps the list has: they are looked at again. @@ -1044,6 +1046,7 @@ pub(super) fn forget(list: *mut c_void) { s.res_retired.extend(batch); retire_recording_res(s.allocator, s.res_retired, s.res_marks, s.res_untracked); } + super::alloc_pool::retirement_done(list as usize); } #[cfg(test)] diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 7b0c265c82..c2f7441aae 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -3956,13 +3956,14 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { }; // (`primary` records on the PC allocator the title's allocator stands for: its Reset below) super::bind::inherit_for_split(this, primary, super::alloc_pool::current(alloc)); - for seg in reclaimed { + for &seg in &reclaimed { super::bind::reset(seg as *mut c_void, 0, 0); clear_segment_state(seg); // a spare now: it does not hold its allocator until it records again (after both // submits above, so the allocator keeps their marks) super::alloc_pool::park_list(seg); } + if let Some(wrapper) = kickoff_wrapper { super::title_list::retire_spares(wrapper, &reclaimed); } if let Some(s) = recording(primary as usize).lock().unwrap().as_mut().and_then(|m| m.get_mut(&(primary as usize))) { s.allocator = alloc; s.bound_pso = bound_pso; diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index ac2d041913..d147a32cd9 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -8856,8 +8856,8 @@ unsafe extern "system" fn cl_release(this: *mut c_void) -> u32 { let f: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(orig_fn(this, 2)); let n = f(this); if n == 0 { - super::list_x::on_release(this as usize); super::bind::forget(this); + super::list_x::on_release(this as usize); super::dred::forget_list(this); } n diff --git a/crates/xcl/src/hle/d3d12x/title_list.rs b/crates/xcl/src/hle/d3d12x/title_list.rs index c7aec707bf..0b61fa9258 100644 --- a/crates/xcl/src/hle/d3d12x/title_list.rs +++ b/crates/xcl/src/hle/d3d12x/title_list.rs @@ -46,6 +46,8 @@ struct SegmentBook { extra_active: Vec, /// Closed extra PC command lists from earlier recordings, kept for reuse on future splits. spares: Vec, + /// Published only after submission and recording cleanup; age alone cannot prove that. + retired: HashMap, /// A PC command list recording in place of `primary`: at the title's Reset the GPU had not /// finished primary's last submit, and the PC does not let a list be submitted again before /// that (#1452, debug layer id 553). A console does. @@ -196,16 +198,18 @@ pub(super) unsafe extern "system" fn reset(this: *mut c_void, alloc: usize, pso: } m.insert(active, this as usize); drop(wg); - for seg in moved { + for &seg in &moved { super::bind::reset(seg as *mut c_void, 0, 0); super::list_x::clear_segment_state(seg); // a spare records again only after its next Reset: it does not hold its // allocator meanwhile super::alloc_pool::park_list(seg); } + retire_spares(this, &moved); // nor does the previous recording a stand-in now replaces: it is not reset here either if stand_in.is_some() { - super::alloc_pool::park_list(primary as usize); + // Its bind state is still alive until a later real Reset or final Release. + super::alloc_pool::unpin_list(primary as usize); } } *((this as usize + F_RING_AT) as *mut usize) = 0; @@ -294,9 +298,60 @@ pub(super) fn can_split(wrapper: *mut c_void, n: usize) -> bool { /// (the last one whose submits the GPU has finished). pub(super) fn pop_spare(wrapper: *mut c_void) -> Option<*mut c_void> { let mut sg = SEGMENTS.lock().unwrap(); - let spares = &mut sg.as_mut()?.get_mut(&(wrapper as usize))?.spares; - let k = spares.iter().rposition(|&p| !super::alloc_pool::in_flight(p))?; - Some(spares.remove(k) as *mut c_void) + let book = sg.as_mut()?.get_mut(&(wrapper as usize))?; + let k = book.spares.iter().rposition(|&p| !super::alloc_pool::in_flight(p))?; + let spare = book.spares.remove(k); + book.retired.remove(&spare); + Some(spare as *mut c_void) +} + +/// Called after cleanup of every spare in this group, never before Kickoff submits them. +pub(super) fn retire_spares(wrapper: *mut c_void, lists: &[usize]) { + let mut sg = SEGMENTS.lock().unwrap(); + if let Some(b) = sg.as_mut().and_then(|m| m.get_mut(&(wrapper as usize))) { + let now = std::time::Instant::now(); + for &list in lists { if b.spares.contains(&list) { b.retired.insert(list, now); } } + } + drop(sg); + if !lists.is_empty() { super::alloc_pool::start_retirement(); } +} + +fn take_idle_spares(book: &mut SegmentBook, keep: usize, now: std::time::Instant, mut idle: impl FnMut(usize) -> bool) -> Vec { + let mut victims = Vec::new(); + let mut kept = 0; + book.spares.retain(|&list| { + if !book.retired.get(&list).is_some_and(|&t| now.saturating_duration_since(t).as_secs() >= 2) || !idle(list) { return true; } + kept += 1; + if kept <= keep { return true; } + book.retired.remove(&list); + victims.push(list); + false + }); + victims +} + +/// Only completed recordings whose caller has finished cleanup may leave. Removal from the +/// spare pool owns each victim, so neither reuse nor wrapper release can take it concurrently. +pub(super) unsafe fn trim_idle_spares(keep: usize) -> (usize, usize) { + if !super::alloc_pool::enabled() { return (0, 0); } + let (victims, left) = { + let mut sg = SEGMENTS.lock().unwrap(); + let Some(books) = sg.as_mut() else { return (0, 0) }; + let now = std::time::Instant::now(); + let mut out = Vec::new(); + for b in books.values_mut() { + out.extend(take_idle_spares(b, keep, now, |list| !super::alloc_pool::in_flight(list))); + } + (out, books.values().map(|b| b.spares.len()).sum()) + }; + let n = victims.len(); + for list in victims { + let raw = list as *mut c_void; + super::bind::forget(raw); + let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(pc_slot(raw, 2)); + release(raw); + } + (left, n) } /// Give back to `wrapper`'s spare pool a closed PC command list that did not become a segment. @@ -450,8 +505,8 @@ unsafe extern "system" fn release(this: *mut c_void) -> u32 { if let Some(b) = book { for seg in b.extra_active.into_iter().chain(b.spares.into_iter()).chain(b.first) { let raw = seg as *mut c_void; - super::alloc_pool::forget_list(seg); super::bind::forget(raw); + super::alloc_pool::forget_list(seg); let rel: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(pc_slot(raw, 2)); rel(raw); } @@ -466,6 +521,23 @@ unsafe extern "system" fn release(this: *mut c_void) -> u32 { #[cfg(test)] mod tests { + + #[test] + fn only_finished_aged_spares_leave_the_pool() { + use std::time::{Duration, Instant}; + let now = Instant::now(); + let old = now - Duration::from_secs(3); + let mut book = super::SegmentBook::default(); + book.spares = vec![1, 2, 3, 4, 5]; + // 1: Kickoff has published it but not completed submit/cleanup. 2: recent. + // 3: GPU pending. 4,5: complete and aged; keep one for reuse. + book.retired.extend([(2, now), (3, old), (4, old), (5, old)]); + assert_eq!(super::take_idle_spares(&mut book, 1, now, |p| p != 3), [5]); + assert_eq!(book.spares, [1, 2, 3, 4]); + assert!(!book.retired.contains_key(&5)); + assert_eq!(super::take_idle_spares(&mut book, 0, now, |p| p != 3), [4]); + assert_eq!(book.spares, [1, 2, 3]); + } use super::*; /// A stand-in PC object: slot 5 returns `this`, slots 0 / 2 count.