GPU: a full scratch pool frees its oldest chunks to keep the one given back (#930) #973
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!973
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-973"
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?
@jptorres96 on GitHub · 2026-11-28 10:25 UTC · original
jptorres96:spare-evict-oldest→main· +215 −51 · 1 files · 2 commits · openFixes #930 (reported by @moduz87; the test cases come from the discussion there, credit to kibagami-nc and ANDREANIADS).
Problem.
Spare::givehanded the returned scratch chunk back to be freed once the kept bytes would pass the limit.takeonly matches the exact size, so chunks of a size nobody asks for any more held the room, and every chunk of the size in use was made and freed again on each list. The issue measured ~415 MB/s of allocations and 7.5 -> 14.1 FPS with eviction.Change.
Spare::givefrees the chunks kept longest (the front ofkept: a take removes, a give appends, so it is least-recently returned first) until the returned one fits, and returns what it did not keep. A chunk larger than the limit alone is still not kept.Sparecounts them by chunk (old, a prefix ofkept), not by bytes, so a chunk given back since is never trimmed as "never asked for", whatever its size. The count by bytes this replaces could free such a chunk, with eviction (ANDREANIADS's case below) and also without it.VICTORY_SPARE_EVICT=0keeps the old behaviour for A/B.SPARE_EVICTEDcounts the bytes freed this way.ALIVE) move the same way for the evicted chunks as for a refused one.Tests (
host::spare, CPU only, no game needed):a_full_pool_frees_its_oldest_to_keep_the_one_given_back: only as many as needed go, oldest first, a take/give makes a chunk newest again, oversize is refused.chunks_of_a_new_size_are_reused_at_capacity: the 128 MiB pool of 512 x 256 KiB from the issue, then 64 take/give of 4 MiB. Old behaviour 0/64 reuses, new 63/64.one_big_chunk_into_a_full_pool_and_the_trim_after_it: the regression case from the issue. One 4 MiB chunk evicts the 16 oldest, 128 MiB stay kept, the next trim frees the other 496 (124 MiB) and leaves the 4 MiB chunk to take.a_trim_never_frees_what_was_given_back_since_the_last: ANDREANIADS's case from the review. A small chunk is kept at a trim, then a full pool of 4 MiB chunks and one more small chunk are given back. It runs at 1 GiB (256 big chunks) and at 128 MiB (32), with eviction and without. The small chunk just given back survives the trim and is the onetakereturns.a_trim_after_the_old_chunk_was_taken_keeps_the_new_one: the same failure without eviction, where the old chunk is taken in the window.On Windows (RTX 4080 SUPER, rustc 1.98.1):
cargo test -p victory-gpu --lib -- --test-threads=1458 passed, 0 failed;cargo clippy -p victory-gpu --all-targets [--all-features] --no-deps -- -D warningsclean apart from main's ownmanual_is_multiple_of.Not measured: in-game FPS (I don't have the game). @moduz87, if you can run your benchmark on this branch, and with
VICTORY_SPARE_EVICT=0for comparison, that would confirm it.🤖 Generated with Claude Code
https://claude.ai/code/session_01H4Yjw2JbmwkmbLrWRuREWT
@ANDREANIADS on GitHub · 2026-11-28 15:26 UTC · original
At
d3eb20c9, the periodic trim can still discard the chunk just returned. I reproduced this on CPU withSpareand integer IDs at the 1 GiB cap.trim()to open the observation window.takecalls between trims.trim(), thentake(256 KiB).With eviction enabled, the last large return evicts ID 0 and the small return evicts ID 1.
lowis still 256 KiB, sotrim()skips the 255 large chunks and frees ID 257. The take misses. On base63b902fe, or withVICTORY_SPARE_EVICT=0, the trim frees ID 0 and the take returns ID 257.The same sequence reproduces at 128 MiB with 32 large IDs. I checked the caller counters in debug and optimized builds using ID-only resources. No pressure drain occurs in this sequence. This would be a regression case for the stated guarantee that a newly returned chunk survives that trim.
@ANDREANIADS on GitHub · 2026-11-28 17:11 UTC · original
Confirmed at
c83b4b9throughgive_chunksandtake_chunkwith integer resource IDs. With default eviction, the second periodic trim freed 0 bytes at both caps. The next 256 KiB take returned ID 33 at 128 MiB and ID 257 at 1 GiB. Both missed atd3eb20c9.With that ID held outside the pool, a separate pressure drain released exactly the remaining 124 MiB or 1,020 MiB. Live and pooled byte/count counters matched the IDs, and the drain left the periodic-trim and eviction counters unchanged. Below capacity, taking and returning the small ID also preserved it through the next periodic trim with eviction both on and off. CPU-only, debug and optimized, rustc 1.98.1. Sizes were logical; no GPU allocations or frame timing.
@jptorres96 on GitHub · 2026-11-28 17:11 UTC · original
@ANDREANIADS Thanks. You're right, and it was worse than the eviction case. Fixed in
c83b4b9.The trim went by a count of bytes, the fewest kept since the last trim, and took from the front any chunk that fit in that count. It never checked that the chunk had been kept the whole window. Once the chunks kept at the last trim were gone, the count still held bytes, and a chunk given back since, small enough to fit, was freed in their place. Eviction is one way to lose them, as in your sequence. A plain take is another, so the same happens with
VICTORY_SPARE_EVICT=0: keep a 4 MiB chunk, trim, give back a 1 MiB one, take the 4 MiB one, and the next trim frees the 1 MiB chunk.The fix.
keptis in the order chunks were given back: gives append, and takes and evictions remove without reordering. So the chunks already kept at the last trim are always a prefix of it.Sparenow counts that prefix by chunk (old), taking away the ones a take or an eviction removes, and the trim frees exactly the prefix.Tests:
a_trim_never_frees_what_was_given_back_since_the_lastis your sequence at 1 GiB with 256 big chunks and at 128 MiB with 32, each with eviction and without. The small chunk just given back survives the trim, andtake(256 KiB)returns it. Without eviction the old small chunk (ID 0) is what the trim frees, as on63b902f.a_trim_after_the_old_chunk_was_taken_keeps_the_new_oneis the case without eviction above.Both fail on
d3eb20c(yours with[257]freed at 1 GiB) and pass now. The earlier tests are unchanged and still pass.victory-gpu: 458 passed. clippy (--no-deps, both builds): clean.View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.