Reuse returned scratch chunks: the spare gives up its oldest instead of refusing the newest (patch against fe7c6e5) #441
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
Imported from TaynanGT/Vice-Tay. Originally posted by einfachama on 2026-11-29T12:38:37Z.
I am not opening a fork, so this hands the change over as a patch: the full diff is at the end, under Patch. It applies with
git applytomainatfe7c6e5219. It also applies, with line offsets only, toreview/local-integration-20261129as of today.Summary
A host list's scratch memory comes in chunks: the first one 256 KiB (
SCRATCH_FIRST), the rest 4 MiB (SCRATCH_CHUNK). Chunks a list gives back are kept in a spare for the next list, up to 128 MiB (SPARE_MOST;SPARE_KEPTon PCs with more than 32 GB), andSpare::takehands out a kept chunk of exactly the size asked for.Spare::giverefused a chunk that would take the spare past its bound. Once the spare was full of 4 MiB chunks, which happens within the first minutes, every 256 KiB first chunk a list gave back was refused and freed, and the next list made a new one. On my PC, standing in one place in my retail title, that was about 58,000 new upload buffers a minute, about 1,000 a second. Each was waited for until the memory was resident ("new memory waited for": about 61,000 waits and 20 s a minute, spread over the list translation threads). Meanwhile the spare held about 127 MB in 33 to 35 chunks that nobody asked for.With this change
Spare::givekeeps the chunk it is given and frees the chunks it has kept longest, as many as needed to stay within the bound.Spare::takealready hands out the chunk given last, so the ones freed are the idle ones. A chunk larger than the bound by itself is still refused. Whatever is not kept goes back to the caller with its size and is freed as before, outside the spare's lock. The bound, the two chunk sizes,SPARE_KEPTand the trim under RAM pressure (trim_spare_chunks) are unchanged. The chunk counters in the memory line count every chunk that leaves the spare.Related work. The same jam has been found in other forks of this code:
VICTORY_SCRATCH_SPARE_MB).Spare::give: at capacity, returned buffers are discarded while older, unused ones stay kept. Under their test conditions it reports about 415 MB/s of needless allocations, and 7.5 to 14.1 FPS with an eviction policy that removes the oldest kept buffers.VICTORY_SPARE_EVICT=0).This change was written independently of #930 and #973, against this tree, and makes no claim about who was first. No code from any of them is in it.
This change differs from #1367 and its ports in three ways:
It was written for a 32 GB PC with about 3 GB of RAM free in play, where 384 MiB more idle spare and 4 MiB chunks for 256 KiB lists would cost memory that is not there. A fallback to a larger kept chunk only when no chunk of the right size is kept would combine with this. It is not part of this change, so that the runs below measure one thing.
Every other user of
Spare<T>was checked:give_chunkspath takes them back from a dropped list's scratch, from a list reset (now on the landing helper thread since #284), and from movie-plane uploads. Plane uploads have sizes no later request matches, so before this they could also fill the spare.ReadbackPool(opt-in,VICTORY_READBACK_POOL=1): at its 256 MiB idle bound it now frees the oldest idle read-back buffers instead of the returning one. With the pool off (bound 0) everything is refused as before.host/send.rs's spare andpool.rs'sPool: separate types, unchanged.trim_spare_chunks. Under RAM pressure it frees every kept chunk, at most once a second, the chunk just given back included. That is what it did before, and it is unchanged.SPARE_KEPTtoSPARE_MOSTbecause host RAM becomes limited, the next give frees the oldest chunks down to 128 MiB and keeps the one given back.Spare::trim. It frees from the front ofkeptand stops at the first chunk given back too recently. Gives append and takes remove without reordering, so with this change a chunk just given back is still never trimmed, except in that PR's panic-memory mode, which frees past the warm ceiling regardless. Merging both needs a small textual merge inSpare::give, for the extra timestamp field.The diff is one file,
crates/victory-gpu/src/host.rs: 107 lines added, 32 removed, most of them doc comments and tests.Measurements
Ryzen 7 5800X, RTX 4060 8 GB, 32 GB RAM, Windows 11, Series S (about 1280x720 inside a 1920x1080 window), diagnostics builds of
151b7dbwithout and with this change, built the same way. The shader translator's fingerprint is the same in both builds, so both start from the same shader cache and pipeline library. Same settings for every run: the-Machine 4070set with the 8 GB card sizes (card buffers 1024 MB, windows 512 MB, texture room 85 %), except run 2, which had pipeline workers 8 andVICTORY_MAP_THREADS=auto. Each run started from the same save and stood in the same place for 15 minutes (run 1: 25 minutes, of which minutes 6 to 15 are used). NVIDIA Smooth Motion was on in runs 1 and 2 and off in runs 3 to 5.Everything is taken from minute 6 to minute 15:
Run 1 made fewer new buffers than runs 2 and 3 (35,800 against about 58,000 a minute) with the same code. How fast the spare fills with 4 MiB chunks differs from run to run. The frame time of run 1 is still in line with runs 2 and 3.
What this does not cover
SPARE_KEPTbound, which this change treats the same way but which was not run.151b7db. This branch is the same change onfe7c6e5, where it applies without conflict. None of the 53 commits between them touchSpare,take_chunk,give_chunksor the scratch call sites. #284 moved the list reset, which gives the chunks back, to the landing helper thread, which takes the same lock.UPLOAD_POOL).Visual review
All five runs were played by hand. No recording was made.
I saw no difference in the picture between the runs with and without the change.
Verification
On this branch (
fe7c6e5plus the change), as CI runs it:cargo test -p victory-gpu --locked: 529 passed, 0 failed, 45 ignored.cargo test -p victory-gpu --all-features --locked -- --test-threads=1 spare readback_pool: 9 passed, among them the D3D12 test of the read-back pool.cargo clippy --workspace --all-targets --locked -- -D warningsand the same with--all-features: no warnings.cargo check --workspace --profile diag --all-features --locked: clean.cargo xtask policy: 322 files ok.git diff --check: clean.New tests in
host.rs'ssparemodule:a_full_spare_keeps_a_small_thing_and_gives_up_the_oldest: a spare filled with 32 4 MiB chunks keeps a 256 KiB chunk, gives up the oldest 4 MiB one, and hands the small chunk back on the next take of that size. The byte count matches the kept chunks throughout.the_oldest_go_first_and_the_bytes_stay_right: chunks leave oldest first. A chunk that fills the spare exactly is kept, a chunk larger than the bound is refused, and a bound of 0 keeps nothing.On
151b7db, the build that was measured:cargo test -p victory-gpu --lockedpassed (498 passed, 43 ignored), and so did both clippy runs.AI use
Claude Code (Claude Opus 5.5) found the cause in the run logs and the source, and wrote the change and its tests. It also read the related issues and pull requests above and checked this tree's trims against the #973 review remark. Claude Fable 5.2, in the Claude app, planned the runs and checked the numbers and the patch. I played the runs.
Patch
Against
mainatfe7c6e5219(one file,crates/victory-gpu/src/host.rs, 107 lines added, 32 removed). Save it as a file and rungit applyon it from the repository root.Imported from TaynanGT/Vice-Tay. Originally posted by Kal0s-design on 2026-11-29T13:03:02Z.
Applied the patch to
fe7c6e5on Windows and checked it:cargo test -p victory-gpu --lib: 529 passed, 45 ignored.cargo test -p victory-gpu --lib --all-features -- --test-threads=1 spare readback_pool: 9 passed.cargo clippy --workspace --all-targets --locked -D warnings, with and without--all-features: clean.cargo xtask policy: ok.git apply --checkalso passes on #439's branch atcfd5ebd.I read the change too. The eviction loop always ends before running past
kept, becausesize <= mostis checked first. The ALIVE counters now add the given chunk to the spare and take back whatever comes out, so they stay balanced.trim_spare_chunksstill empties the whole spare, so the "trim frees the chunk just given" problem from the other tree doesn't apply here.One thing worth stating in the PR text:
ReadbackLease::dropuses the sameSpare::give, so the read-back pool also switches to keeping the newest idle buffer and dropping the oldest when full. It drops the returnedVecoutside the lock just as it dropped the oldOption, so that's fine, but it is a behaviour change there too.If you'd rather not open a fork, I'm happy to carry it as a PR with your authorship kept on the commit, if you and the maintainer want that.
AI-assisted: yes
Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-29T13:23:29Z.
Thanks for the detailed patch and measurements. The oldest-idle eviction change is now adapted in my local working tree, including an overflow-safe capacity comparison. I am testing it before committing; the readback pool behavior and memory-pressure trim also need validation. Your reported stationary measurements are useful, but I will not treat them as proof of improvement during driving.
Imported from TaynanGT/Vice-Tay. Originally posted by einfachama on 2026-11-29T13:26:45Z.
Thanks for applying and checking it, and for reading the change.
Yes, please carry it as a PR. I am fine with that if the maintainer is. For the commit's author, please use
einfachama <[email protected]>.On the read-back pool: the issue text has it under "Every other user of
Spare<T>was checked", but that is easy to miss. Please state it in the PR's summary as a behaviour change of its own: withVICTORY_READBACK_POOL=1, a full pool now keeps the returning buffer and frees the oldest idle ones. My runs do not cover it, they had the pool off.The runs in the issue are on
151b7db. If it helps the PR, I can repeat the same place with and without the change on the PR's head.AI use: drafted with Claude Fable 5.2 in the Claude app. I read it before posting.
Imported from TaynanGT/Vice-Tay. Originally posted by Kal0s-design on 2026-11-29T14:21:29Z.
Thanks. Since the maintainer has already adapted it in his local tree (with an overflow-safe capacity check), a separate PR from me would only duplicate that, so I'll hold off. If it turns out he'd rather take it as a PR, I'll open one with your authorship as you gave it.
Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-29T14:39:00Z.
The patch is now in my local history as
d5ee68e57, with einfachama as the original author. The commit records 12 passing targeted all-feature tests. The readback pool also keeps the returning buffer and evicts the oldest idle buffers when full with VICTORY_READBACK_POOL=1. No duplicate PR is needed. A matched driving run is still required before claiming a performance improvement.Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-30T17:00:04Z.
mainhas moved to a new line of the code and everything is now named Vice-Tay:vice-tay.exe play, andVICE_TAY_*switches instead ofVICTORY_*(details in #468). Could you check whether this still happens on the currentmainand say so here, with a log if you can?Imported from TaynanGT/Vice-Tay. Originally posted by einfachama on 2026-11-30T23:49:02Z.
Checked on
mainat6e7e7873, on the PC from the report (Ryzen 7 5800X, RTX 4060 8 GB, 32 GB RAM, Windows 11 build 26300, driver 621.24).Short answer: the jam this issue describes does not show on this line, and the patch above does not apply to it and is not what it needs. But the current
maindoes not fit this PC at all, so the run is not comparable to the ones in the report.The jam. In the 71 memory lines of a 7-minute diagnostics run the spare was never at its bound: it moved between 2 and 115 MB (bound 128 MiB), so
Spare::giveis not what turns chunks away there. Chunks are still made all the time, for another reason as far as I can tell: the PC has under 8 GiB of memory free for the whole run, sokeeps_under_pressure()is false and the 1 Hz sweep trims the spares to the floor (spare_trim_to). Per minute, from second 120 to 420:With the PC this short of memory I would not read more into those numbers. Evicting at the bound would change nothing here, so I withdraw the patch for this line.
What does not fit. Built from source (
--profile diag --features diagnostics), started withrunand the settingsplaypicks for this PC (Series X, 1440p, DLSS Balanced), frame generation off, Windows' own Direct3D 12 runtime (noD3D12folder; the card reports shader model 6.8), Streamline 2.14.16, shaders and pipelines preloaded, nothing else open (25.2 GB of memory available at the start), standing still in one place in my retail title:A second run with what
machine_settingssets under 24 GB of RAM (VICE_TAY_INPLACE=2,VICE_TAY_WINDOW_KB=64,VICE_TAY_WINDOW_TAILS=1, noVICE_TAY_CARD_BUFFERS_MIN_KB) took 25 to 27 GB of commit at 1.5 to 6.6 fps and lost the device after 274 s (0x887A0005; "when the host device went: the submitting thread was preparing a submission (nothing of ours was running on the card)"). A first try with other programs open (22.6 GB available at the start) had under 1 GB available 67 s in, and my watcher stopped it.playrefuses Series S on this build, so I did not try it. For comparison: one day earlier this PC ran trustno1-coder/VIctory-NVIDIA v2026.11.29.05 in Series S at 720p at the same place at about 18 fps, with 20 to 22 GB of process commit, 6.6 GB on the card and the right picture. So for an 8 GB card with 32 GB of RAM, Series S was the profile that fit.Four lines of the 7-minute run (seconds 240 and 360)
I can open a separate issue for the memory fit with the issue form, and attach more of the log, if that is more useful than this comment.
AI use: Claude Code (Claude Opus 5.5) cloned and built
main, read the scratch code and set up the run. Claude Fable 5.2 in the Claude app planned the runs, read the logs and drafted this comment. I ran the runs and read this before posting.Imported from TaynanGT/Vice-Tay. Originally posted by einfachama on 2026-12-01T00:50:00Z.
Two things changed on
mainafter my runs, so a correction to my last comment.6e7e787, the commit I tested, is the merge of #477, which6855370has since reverted because the first cutscene lost the device with it. The device loss in my second run (274 s, 0x887A0005) may be that and not the small-RAM switches. I have not rerun without #477. The memory numbers stand as measured, but I cannot say how much of them #477 accounts for.series-s/, the earlier line as of Nov 27. As I readseries-s/crates/victory-gpu/src/host.rs,Spare::givethere is the one this issue is about: it hands the chunk back when the kept bytes would passmost. So what I wrote about withdrawing the patch holds for the NVIDIA line only. On the Series S build the jam and the patch should both still apply; I have not built it yet.#482 lists the boat route on Series S as not covered. That is the place my runs in this issue stand at, and Series S is the profile that fits this PC. I will build
series-s/, check that the patch applies there, and run that place with and without it, same save and settings, then post the numbers and the patch againstseries-s/here.AI use: drafted with Claude Fable 5.2 in the Claude app, which read the commits and the file on GitHub. I read it before posting.