perf(gpu): reclaim resources held by released dynamic descriptor slots #579
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!579
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-579"
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?
@tommiehansen on GitHub · 2026-12-05 11:17 UTC · original
tommiehansen:perf/idle-dynamic-reclamation→main· +454 −16 · 4 files · 8 commits · openWhy
Released dynamic descriptor slots can retain resource references until reused.
What changes
Opt-in bounded reclamation of free slots, null descriptors outside allocator lock, existing delayed retirement. Rotating cursor, 128-slot batches, up to 4096 per sweep, bounded unproductive scans and soft time limit; runs as separate sixth maintenance step.
Benchmark results
32,768 allocator operations/arm: waits >400 µs 228→0; worst wait 537.7→169.9 µs.
Why this should be merged / current decision
Reduces scan/allocator contention and releases stale references, not measured VRAM bytes. Ready for review, default off.
Validation
19c66a6CI passed all-targets, switch checks, four native tests and paired workload tests. Independent review motivated shorter lock windows.Compatibility
Largest registered view heap initially. Does not replace #572/#509/#364/#558 or existing fences.
Archived detailed evidence and review history.
@Kal0s-design on GitHub · 2026-12-05 11:47 UTC · original
Verified head
22b8d26on native Windows with MSVC, on one NVIDIA desktop card and on WARP. The branch sits directly on8bf4878, the current main.On the PR the victory-gpu lib suite (skipping views_die_with_their_resource) gives 354 passed, 5 failed and 24 ignored. Main gives 352 passed, 5 failed and 23 ignored. Both fail the same five tests, which are known native failures here. Those are the two indirect host_list tests, the two pipeline::host library loads and the flaky watch the_rest_of_the_one_asking_has_a_number_of_its_own. The difference is the two new tests passing plus the new ignored benchmark. With VICTORY_RECLAIM_DYNAMIC=1 the PR gets the same numbers. I didn't confirm that the sweep actually reached a registered view heap during that run, since the switch only matters inside the submit sweep. The victory-core menu tests that parse run.conf.default also pass with the new entry.
The three new tests pass on the card and on WARP, with and without the diagnostics feature, and the device test prints "device reclamation executed" on both. With the debug layer on (diagnostics plus VICTORY_D3D12_DEBUG=1) the drained info queue held no errors after them. As a check that the layer was really active, I made one deliberately invalid SRV, and the same drain did report it. In a release build the benchmark puts 128 reclaimed slots at roughly 36 to 46 microseconds per sweep on the card.
I also wrote two local tests, not pushed. The first runs claim_idle_dynamic over ring sizes from 1 to 262144 with several scan and limit pairs, starting with the cursor past the end. The returned cursor always stays below the ring length. The function never claims a taken or clean slot or the same slot twice, and repeated calls end up claiming exactly the free dirty slots.
The second runs reclaim_free_dynamic in a tight loop on one thread while three threads take 1 to 8 slots with dynamic(), write raw views, yield, check that every held slot still has its resource, then release. The allocators' search start is pinned inside the sweep's window, so the two really compete for the same slots. About 20,000 to 35,000 slots per run were reclaimed by the sweep and then handed out again by dynamic(). This was on the card and on WARP, with the debug layer on and off. No held slot ever lost its resource, and the free count came back to its starting value. After a full drain no free dynamic slot held a reference, and the debug layer reported nothing.
From reading the code, dynamic slots go back to the pool in two places. One is tables_done when a flight lands (host.rs around 4163, list.rs:198, draw.rs:606). The other is the Drop of a State that was never submitted (draw.rs:566). The arena run in memory.rs:1764 is held for the heap's life and never becomes free. So a free slot isn't named by any list in flight, except for the replay-after-release case the body already calls unsupported. The old resource goes through retire_resource with the newest submission number, queued ones included, and bury() only drops it BURY_LAG (128) submissions after that has finished. The resource is never freed while the GPU can still read it.
The claim marks slots taken under the same lock that dynamic() searches under, so the sweep and the allocator can't hold the same slot at once. Only the holder writes a slot, so make_null can't race a create into it. The try_lock in the predicate only skips a busy slot and can't deadlock against the allocator lock. With the switch unset the only changes are a 4-byte cursor field and a OnceLock check once a second, so behavior matches main.
The point I'd raise most is throughput. The sweep runs at most once a second and reclaims at most 128 slots, while the dynamic ring here has about a million slots. The code comments talk about some 10,000 tables a frame. When the ring is busy, allocation wraps it and overwrites old references much faster than the sweep can. When it's quiet, 128 a second would take hours to clear a ring-sized backlog. So as written I'd expect the effect on retained references to be small. Gameplay numbers for how many references are held before and after would settle it either way.
A few smaller notes.
The claim scan does up to 8192 try_locks while holding the allocator lock that the recording threads contend on. When nothing in the window was dirty, that took about 76 microseconds in a release build on the card. It's once a second, but it rises with the window size if the limits are raised to address the throughput point.
with_view_heap only picks the view heap with the most title slots, so any other view heap is never swept. The body says so already.
The switch is read with a plain env == "1" check (host.rs:349) rather than crate::switch::on, so a value like "true" counts as off. VICTORY_POOL_MOVE does the same, so there's precedent. Also, run.conf.default writes VICTORY_RECLAIM_DYNAMIC=0 where most neighbouring opt-ins leave the value empty.
While the claimed slots are being nulled, free is lower by up to 128. A dynamic() call in that short window could fail and set too_long, but the return step resets it, and with a million slots it shouldn't matter.
The pinned CI run is green, and its native job ran the device test for real ("device reclamation executed"). I haven't measured gameplay or VRAM, which matches the body's own "no claim yet".
AI-assisted: yes
@tommiehansen on GitHub · 2026-12-05 12:28 UTC · original
Thanks for testing the actual device and allocator contention! I agree the original sweep cap was too conservative for a large backlog.
cc192f2adds a bounded multi-batch sweep, backlog benchmark and debug telemetry. It remains opt-in; actual reclamation throughput and submit overhead still need measurement.@Kal0s-design on GitHub · 2026-12-05 12:49 UTC · original
Re-checked head
cc192f2on native Windows with MSVC, on one NVIDIA desktop card and on WARP.The new sweep runs up to 32 of the old batches, stops after 8 empty ones in a row, and checks a 1 ms deadline between batches. So a sweep can reclaim at most 4096 slots and look at up to 262,144, or 65,536 when nothing is dirty. In practice the deadline is what stops it. In a release build on the card the backlog benchmark reclaimed 2432 to 3584 slots per sweep in 1.00 to 1.04 ms, against a flat 128 for the old path. My own runs gave 3712 to 3968 for a dense backlog of 8192 and 1152 to 1280 when one slot in 64 was dirty. With the ring here at about 1M dynamic slots, a fully dirty ring now clears in roughly four to seven minutes instead of a bit over two hours, so somewhere between 19 and 30 times faster. That answers most of my throughput point.
Correctness looks unchanged to me. The new code only calls reclaim_free_dynamic in a loop, so the claim under the allocator lock, the nulling outside it, retirement through the submission lag and the cursor handling are the same as before. The suite gives 355 passed, 5 failed and 25 ignored, default and with VICTORY_RECLAIM_DYNAMIC=1. Against the last head that's one more pass and one more ignored, which are the new budget test and the new backlog benchmark. The failures are the same known native ones. Across several runs either way the flaky watch test was sometimes joined by one or two other watch tests, with and without the switch, so I don't think that's related. I also re-ran my local race test with the sweep thread switched to reclaim_free_dynamic_budgeted. That was on the card and on WARP, with the debug layer on and off. No held slot lost its resource, the free count came back, a full drain left no references, and the debug layer reported nothing. On the card a deliberately invalid SRV did show up in the same drain, so the layer was active.
The one new thing I'd look at is the allocator lock. Each batch does let go of it, but when a window is clean the loop takes it again straight away, with nothing in between. I timed each batch's hold in release. A clean window of 8192 slots holds the lock for about 64 to 72 microseconds, a sparse window for about 65, and a dense one for 1 to 3. A sweep over a clean ring is 8 batches back to back, about 530 to 560 microseconds in total. The std mutex isn't fair, so a waiting thread often doesn't get in between them. I had one thread doing dynamic(4) and release in a loop while sweeps ran every 5 ms. Without sweeps its worst wait was about 50 microseconds. With them, over two runs of 55 sweeps each, 13 to 16 of its calls waited more than 400 microseconds, so roughly one sweep in three blocked it for the whole sweep. The worst waits were 567 and 890 microseconds. That probe takes the lock nonstop, so one in three is an upper bound. In the game it would mean a thread that's recording at that moment can stall for around half a millisecond, once a second. Capping the clean slots scanned per sweep would bound that whatever the lock does, and a smaller window would help too. A yield between batches might help but doesn't guarantee the waiter wins. The submit thread also now spends up to about 1 ms in the sweep instead of about 40 microseconds, and the SHADER_HEAPS read guard is held for that whole time, though heap creation and drop are rare.
Two small things. The deadline is only checked between batches, so a sweep can run over it by one batch, about 40 microseconds at most in my runs. And the new debug line only fires when something was reclaimed, so the clean-ring sweep, which is the one that holds the lock longest, never shows up in it. The backlog benchmark also only covers a dense backlog.
I still haven't measured gameplay or VRAM.
AI-assisted: yes
@tommiehansen on GitHub · 2026-12-06 00:05 UTC · original
Thanks for the follow-up, especially for measuring the allocator contention! :) The bounded scan is now in the PR. Latest native CI at
19c66a6shows waits over 400 µs going from 228 to 0, with the worst wait down from 537.7 to 169.9 µs. Dense reclamation still clears 4,096 slots per sweep.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.