GPU: a full scratch pool frees its oldest chunks to keep the one given back (#930) #973

Open
jptorres96 wants to merge 2 commits from upstream-pr/973 into main
jptorres96 commented 2026-11-28 10:25:32 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by jptorres96 on 2026-11-28T10:25:32Z.


Fixes #930 (reported by @moduz87; the test cases come from the discussion there, credit to kibagami-nc and ANDREANIADS).

Problem. Spare::give handed the returned scratch chunk back to be freed once the kept bytes would pass the limit. take only 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::give frees the chunks kept longest (the front of kept: 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.
  • The periodic trim frees only the chunks that were already kept at the last trim and have not been taken since. Spare counts them by chunk (old, a prefix of kept), 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=0 keeps the old behaviour for A/B.
  • SPARE_EVICTED counts the bytes freed this way.
  • Counters (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.
  • The two existing tests, updated for the new signature.
  • 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 one take returns.
  • 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=1 458 passed, 0 failed; cargo clippy -p victory-gpu --all-targets [--all-features] --no-deps -- -D warnings clean apart from main's own manual_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=0 for comparison, that would confirm it.

🤖 Generated with Claude Code

https://claude.ai/code/session_01H4Yjw2JbmwkmbLrWRuREWT

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5539168141 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/973). Originally posted by [jptorres96](https://github.com/jptorres96) on 2026-11-28T10:25:32Z. --- Fixes #930 (reported by @moduz87; the test cases come from the discussion there, credit to kibagami-nc and ANDREANIADS). **Problem.** `Spare::give` handed the returned scratch chunk back to be freed once the kept bytes would pass the limit. `take` only 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::give` frees the chunks kept longest (the front of `kept`: 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. - The periodic trim frees only the chunks that were already kept at the last trim and have not been taken since. `Spare` counts them by chunk (`old`, a prefix of `kept`), 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=0` keeps the old behaviour for A/B. - `SPARE_EVICTED` counts the bytes freed this way. - Counters (`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. - The two existing tests, updated for the new signature. - `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 one `take` returns. - `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=1` 458 passed, 0 failed; `cargo clippy -p victory-gpu --all-targets [--all-features] --no-deps -- -D warnings` clean apart from main's own `manual_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=0` for comparison, that would confirm it. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01H4Yjw2JbmwkmbLrWRuREWT
ANDREANIADS commented 2026-11-28 15:26:54 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by ANDREANIADS on 2026-11-28T15:26:54Z.


At d3eb20c9, the periodic trim can still discard the chunk just returned. I reproduced this on CPU with Spare and integer IDs at the 1 GiB cap.

  1. Give ID 0 (256 KiB), then call trim() to open the observation window.
  2. Give IDs 1 through 256 (4 MiB each), then ID 257 (256 KiB). Make no take calls between trims.
  3. Call trim(), then take(256 KiB).

With eviction enabled, the last large return evicts ID 0 and the small return evicts ID 1. low is still 256 KiB, so trim() skips the 255 large chunks and frees ID 257. The take misses. On base 63b902fe, or with VICTORY_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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7241473975 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/973#issuecomment-7241473975). Originally posted by [ANDREANIADS](https://github.com/ANDREANIADS) on 2026-11-28T15:26:54Z. --- At `d3eb20c9`, the periodic trim can still discard the chunk just returned. I reproduced this on CPU with `Spare` and integer IDs at the 1 GiB cap. 1. Give ID 0 (256 KiB), then call `trim()` to open the observation window. 2. Give IDs 1 through 256 (4 MiB each), then ID 257 (256 KiB). Make no `take` calls between trims. 3. Call `trim()`, then `take(256 KiB)`. With eviction enabled, the last large return evicts ID 0 and the small return evicts ID 1. `low` is still 256 KiB, so `trim()` skips the 255 large chunks and frees ID 257. The take misses. On base `63b902fe`, or with `VICTORY_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 commented 2026-11-28 17:11:05 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by ANDREANIADS on 2026-11-28T17:11:05Z.


Confirmed at c83b4b9 through give_chunks and take_chunk with 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 at d3eb20c9.

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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7242819247 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/973#issuecomment-7242819247). Originally posted by [ANDREANIADS](https://github.com/ANDREANIADS) on 2026-11-28T17:11:05Z. --- Confirmed at `c83b4b9` through `give_chunks` and `take_chunk` with 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 at `d3eb20c9`. 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 commented 2026-11-28 17:11:48 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by jptorres96 on 2026-11-28T17:11:48Z.


@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. kept is 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. Spare now 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_last is 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, and take(256 KiB) returns it. Without eviction the old small chunk (ID 0) is what the trim frees, as on 63b902f.
  • a_trim_after_the_old_chunk_was_taken_keeps_the_new_one is 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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7242828544 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/973#issuecomment-7242828544). Originally posted by [jptorres96](https://github.com/jptorres96) on 2026-11-28T17:11:48Z. --- @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.** `kept` is 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. `Spare` now 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_last` is 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, and `take(256 KiB)` returns it. Without eviction the old small chunk (ID 0) is what the trim frees, as on `63b902f`. - `a_trim_after_the_old_chunk_was_taken_keeps_the_new_one` is 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.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/973:upstream-pr/973'
git switch 'upstream-pr/973'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/973'
git switch 'upstream-pr/973'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/973'
git switch 'upstream-pr/973'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/973'
git switch 'main'
git merge --squash 'upstream-pr/973'
git switch 'main'
git merge --ff-only 'upstream-pr/973'
git switch 'main'
git merge 'upstream-pr/973'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
trustno1-coder/VIctory-NVIDIA!973
No description provided.