perf(gpu): offload D3D12 retirement and pool trims to bounded zero-box reaper #4138
No reviewers
Labels
No labels
AMD
audio
bug
confirmed
crash
cutscene
dlss
documentation
duplicate
enhancement
fixed-on-main
frame-generation
good first issue
help wanted
input
INTEL
invalid
lighting
linux-proton
memory
needs-info
needs-logs
needs-rebase
NVIDIA
performance
pr-diagnostics
pr-docs
pr-feature
pr-fix
pr-performance
pr-refactor
pr-tests
question
RAYTRACING
rendering
report
series-s
shaders
spam
startup
upstream
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/trustno1-coder--VIctory-NVIDIA!4138
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-4138"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@steven-seagull27 on GitHub · 2026-11-30 16:38 UTC · original
steven-seagull27:perf/async-sweep-and-d3d12-reaper→main· +447 −107 · 5 files · 1 commits · closed without mergingWhat it changes
Synchronous D3D12 COM
Release()and WDDMdxgkrnlunmap/decommit calls during command-list resets, pool evictions, and 1-Hz trims can stall the submitting and recording threads or extend pool/staging mutex hold times:bury()batches had"victory-bury"(MAX_PENDING_BATCHES = 64) andsend::let_gohad"victory-send-free",Deferredunpooleduploads/prelude_uploads/placed_cbvon command-list reset/drop,texture::let_go_stagingevictions (which previously ranRelease()while holding theSTAGINGmutex),texture::trim_spare_linear,list::trim_idle/recyclecommand-allocator destruction,texbackreadback trims,uav_poolevictions/trims, andgive_chunks_to/give_uploads/trim_spare_uploads/ 1-Hzsweep_spare_chunksstill dropped D3D12 allocations synchronously on the caller thread.Solution
Narrowed per review feedback strictly to the bounded, normal-priority, zero-box background D3D12 reaper (
VICTORY_REAPER=0opt-out), leaving synchronoussweep(),VICTORY_SWEEP_AHEAD(default off),keeps_under_pressure()(8 GiB), andsend::let_gountouched:"victory-reaper"worker at normal thread priority (MAX_PENDING_REAP = 64):bury_pool("victory-bury") withstd::sync::mpsc::sync_channel::<ReapItem>(64)drained by a normal-priority"victory-reaper"thread (VICTORY_REAPER=0opt-out).TrySendError::FullorTrySendError::Disconnected, drops the item inline on the caller thread — preservingbury()'s exact 64-batch backpressure bound so saturated CPUs (e.g. 6-core hosts paired with 12 GB GPUs near VRAM budget) never accumulate unbounded released resources in a background queue.ON_REAPERdetection runs any nestedreap_*calls triggered insideDropimplementations inline on"victory-reaper"without consuming extra channel slots.ReapItemenum with zero per-resource boxing & inlinebury_lag() == 0path:ReapItem::{Resource(ID3D12Resource), Resources(Vec<ID3D12Resource>), Retired(Vec<Retired>), Boxed(Box<dyn Send>)}moves single resources,Vec<ID3D12Resource>batches, and fencedVec<Retired>(Retired::Resourceunboxed) across the channel without per-resourceBoxallocations;reap_batch<T>(Vec<T>)returns early when empty and uses at most a single box for an entire coarse trimVec<T>.retire()andretire_resource()keep their immediate inlinedropwhenbury_lag() == 0and no submission is in flight.Deferred::clear()andDrop for Deferred: runsself.held.clear()synchronously first so completion guards execute before scratch chunks recycle, then sends non-emptyuploads(reap_resources),prelude_uploads(reap_batch), andplaced_cbv(reap_resource) to"victory-reaper".give_chunks_to,give_uploads, 1-Hzsweep_spare_chunks,uav_pool::give_uavs, andtexture::trim_spare_linearviareap_resources;trim_spare_uploads,uav_pooltrims,list::trim_idle/recycle,texbacktrims, andtexture::let_go_staging(cloned out before releasing theSTAGINGmutex) viareap_batch.pub fn trim_spare_chunks()forVirtualAlloc(MEM_COMMIT)/ hard-flush callers andfree_all_spares()forE_OUTOFMEMORYupload retries) continue dropping synchronously so retried allocations immediately observe freed pages.How it was tested
victory-gpu --lib):host::batch::reap_helpers_drop_asynchronously_and_honor_opt_out— verifies"victory-reaper"asynchronous drops forreap_batchandreap_retired(plusreap_resource/reap_resourceswhen a D3D12 device is present), inline nested drops viaON_REAPER, synchronousDeferred::heldguard execution on bothclear()andDrop, andVICTORY_REAPER=0synchronous opt-out.host::batch::reap_backpressure_bound_falls_back_to_inline_drop_without_leaking— verifies filling async_channel::<ReapItem>(MAX_PENDING_REAP)(64) enqueues the first 64 items and falls back cleanly to inlinedroppast 64 (as well as on disconnected/unavailable channels) without blocking or leaking items.host::bury_tests::bury_drops_retired_objects_asynchronously, plus module suites (host::,list::,texture::,texback::),cargo clippy -p victory-gpu --lib --tests, andcargo run -p xtask -- policy --staged.cargo testpassed for the crates it touches: yesChecked for overlap
VICTORY_ASYNC_SWEEP),VICTORY_SWEEP_AHEAD,keeps_under_pressure(), andsend::let_go(697590ad) are untouched, avoiding any overlap with#4226.e7ad054e4etoc876444b35@IRennie74 on GitHub · 2026-11-30 18:22 UTC · original · edited 2026-11-30 18:44 UTC
This bundles four independent changes. Each changes a default, and the PR has no game measurement for any of them. Concrete concerns, by part:
The bury queue lost its bound.
bury()used to push at mostMAX_PENDING_BATCHES(64) batches tovictory-buryand drop inline beyond that.reap()sends to an unboundedmpscchannel, drained by one thread atTHREAD_PRIORITY_BELOW_NORMAL. On a saturated CPU that thread can fall behind, and released resources (VRAM included) pile up in the channel.On a Ryzen 5 5600X + RTX 3060 12 GB (Series S, DLSS Performance), the title uses about 3 cores, the list threads 1.7 and ours the rest, so below-normal threads do get starved there. On a 12 GB card running near its texture budget, late releases cost the most. Keeping a bound, and dropping inline past it, would keep the old guarantee.
retire/retire_resourcenow box every object. The immediate path callsreap(object): aBox::newand a channel send per object.Retired::Resourceexists to avoid exactly that ("20,000 a frame, without a box each"). Batching these asburydoes, or keeping the inline drop for single resources, would avoid the regression.The async sweep holds hot locks at below-normal priority.
run_sweeponvictory-sweeprunsresource::let_go_idleandtexture::let_go_idleunder the texture table's andSTAGING's locks. #4227 measuredlet_go_idleat 39-75 ms on a full card. A below-normal thread holding those locks while normal-priority recording threads and the submitting thread wait for them is a priority inversion, and it gets worse the fewer cores there are.Also, the sweep at rest relied on "what the sweep lets go of is named by no list, and nothing runs now" (the comment above
SWEPTinHost::submit). The PR should say why eachlet_go_idlestays safe when it overlaps the submitting thread's own preparation, not only the recording threads.Two default flips that deserve their own PRs:
VICTORY_SWEEP_AHEADon: on the 5600X/3060 rig above it was within noise, 16.5-17.9 fps against 17.3-18.5 for baselines of the same runs (standing, A/B/A).keeps_under_pressure8 GiB -> 2 GiB free: the comment this replaces records why 8 GiB was chosen ("on a 16 GB PC keeping them cost some 40 ms a frame of waits for flights", PR #57). A 16 GB PC under load commonly has 2-8 GiB free, so 2 GiB brings that case back. It needs a measurement on a 16 GB PC before it changes.Splitting this into four PRs would let the safe parts land: the reaper with a bound, the async sweep opt-in, the SWEEP_AHEAD default, and the spare threshold. #4227 already discusses how its change 2 interacts with part 3.
@Zeyphers on GitHub · 2026-11-30 20:20 UTC · original
Merged onto main
84bd61e2and benched on an RTX 2080 Super 8 GB and Ryzen 7 5800X (Series S, DLSS Quality, standing scene, 180 s): 10.0 fps median vs 9.2–9.7 baseline, so no measurable change on this CPU-bound rig. Two baseline runs differed by about 0.5 fps, so that's the noise. Only one run per PR, in a standing scene, not a walk.@paales on GitHub · 2026-11-30 20:51 UTC · original
Written by 🤖:
In-game numbers for this PR. PC: RTX 5070 Ti 16 GB, Ryzen 9 5900X, 32 GB, Windows 11. Series X, 1440p, DLSS Performance, frame generation off. Each run: a new game (settings save, no story save), one A press, the first cutscene for 60 s from its first frame. On this PC the same build's submitting thread took 24 ms a frame in one run and 35 ms in another half an hour later, so only large differences count. 30 fps cap.
Base:
main84bd61e2with #3918, #1727, #3553, #3043 and #4207 (VICTORY_FILL_AHEAD=1). One run each.Fewer long spikes; the rest is within this PC's spread. No crash in about 15 runs since. It is in the build I play with.
perf(gpu): offload D3D12 retirement and periodic sweeps to background threadsto perf(gpu): offload D3D12 retirement and pool trims to bounded zero-box reaperc876444b35to9fb5f56acd@steven-seagull27 on GitHub · 2026-11-30 23:04 UTC · original
Thank you @IRennie74 for the thorough review and hardware analysis. I have rebased onto
origin/mainand narrowed this PR strictly to the bounded, zero-box background D3D12 reaper, addressing all four points:"victory-reaper"now usesstd::sync::mpsc::sync_channel::<ReapItem>(64)(MAX_PENDING_REAP = 64) at normal thread priority (noTHREAD_PRIORITY_BELOW_NORMAL, matching"victory-bury"). OnTrySendError::FullorTrySendError::Disconnected,try_enqueue_reapdrops the item inline on the caller thread so 6-core CPUs paired with 12 GB cards near VRAM budget never starve the reaper or accumulate released VRAM in the channel. Added a synthetic backpressure unit test (reap_backpressure_bound_falls_back_to_inline_drop_without_leaking) verifying exact inline fallback past 64 queued items without blocking or leaking.Boxallocations and restored inlinebury_lag() == 0drops: ReplacedBox<dyn Send>per call with a concreteReapItemenum (Resource(ID3D12Resource),Resources(Vec<ID3D12Resource>),Retired(Vec<Retired>), andBoxed(Box<dyn Send>)strictly for non-empty coarseVec<T>trim batches viareap_batch).retire()andretire_resource()once again drop inline whenbury_lag() == 0and no submission is in flight, while fencedbury()batches moveVec<Retired>(Retired::Resourceunboxed) directly throughReapItem::Retired. Also kept synchronous drops on the OOM-retry paths (pub fn trim_spare_chunks()andfree_all_spares()) and leftsend::let_go(697590ad) untouched.VICTORY_ASYNC_SWEEPfrom this PR:sweep()remains synchronous onmain, avoiding lock priority inversion on the texture andSTAGINGlocks and any overlap with#4226.VICTORY_SWEEP_AHEADandkeeps_under_pressure): Both remain atmain's defaults (VICTORY_SWEEP_AHEADoff unless set, andkeeps_under_pressureat8 GiBfree physical RAM) so any future changes there can be evaluated in separate PRs with dedicated measurements.9fb5f56acdto1f0d90d6ce@trustno1-coder on GitHub · 2026-12-01 18:46 UTC · original
Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.
Pull request closed