GPU: add opt-in idle shader cache trimming #1583

Open
srijann07 wants to merge 2 commits from upstream-pr/1584 into main
srijann07 commented 2026-11-24 19:28:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by srijann07 on 2026-11-24T19:28:25Z.


Motivation

Related to #471. The shader cache retains every unique shader container for the lifetime of the process, even after all pipelines using it have been released.

Changes

  • Add opt-in VICTORY_TRIM_IDLE_SHADERS=1.
  • Sweep cache-only shader entries every 256 new cache entries.
  • Preserve shaders owned by active pipelines and workers.
  • Keep default behavior and disk shader cache unchanged.
  • Add documentation and three regression tests.

Verification

Limitations

Experimental and disabled by default. Not a hard memory cap or VRAM optimization. Cache sweeps run under a mutex and only on new entries. Evicted shaders may require additional translation. Synthetic payload savings are not measured process RAM savings. No in-game measurements or game assets used.

AI-assisted development.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6589710119 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1584). Originally posted by [srijann07](https://github.com/srijann07) on 2026-11-24T19:28:25Z. --- ## Motivation Related to #471. The shader cache retains every unique shader container for the lifetime of the process, even after all pipelines using it have been released. ## Changes - Add opt-in `VICTORY_TRIM_IDLE_SHADERS=1`. - Sweep cache-only shader entries every 256 new cache entries. - Preserve shaders owned by active pipelines and workers. - Keep default behavior and disk shader cache unchanged. - Add documentation and three regression tests. ## Verification - Standard and diagnostics Clippy: passed. - Workspace tests: 345 passed, 0 failed, 6 ignored. - All three new regression tests passed. - Synthetic retained shader payload reduced from 16 MiB to 64 KiB. - CI: https://github.com/srijann07/VIctory/actions/runs/45469217973 ## Limitations Experimental and disabled by default. Not a hard memory cap or VRAM optimization. Cache sweeps run under a mutex and only on new entries. Evicted shaders may require additional translation. Synthetic payload savings are not measured process RAM savings. No in-game measurements or game assets used. AI-assisted development.
Marvisak commented 2026-11-25 02:06:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:31:31Z.


Automated triage for the maintainers.

Merge candidate. Adds an opt-in VICTORY_TRIM_IDLE_SHADERS=1 that turns the SHADERS map in crates/victory-gpu/src/pipeline.rs into a ShaderCache which, every 256 new containers, drops entries whose Arc is held only by the cache. I checked that every other holder of an Arc<Shader> (pipelines, pipeline/make.rs pending jobs, pipeline/host.rs) keeps its own strong reference, so live shaders keep their identity and their translation; an evicted container is simply rebuilt (and retranslated or read from the disk cache) on next use. The default path is unchanged and the three tests exercise the cache directly. Note that no memory saving in the title has been measured yet; the README section says so.

Thanks for the rebase: it now merges cleanly with main (940d32f), so this is a full review of the new head.

CI: passing (check).


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149645088 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1584#issuecomment-7149645088). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:31:31Z. --- <!-- victory-triage v1 head=1ba845ee1287321c87c247109a26807250fecc48 verdict=merge-candidate at=2026-11-25T06:20:58Z --> Automated triage for the maintainers. **Merge candidate.** Adds an opt-in `VICTORY_TRIM_IDLE_SHADERS=1` that turns the `SHADERS` map in `crates/victory-gpu/src/pipeline.rs` into a `ShaderCache` which, every 256 new containers, drops entries whose `Arc` is held only by the cache. I checked that every other holder of an `Arc<Shader>` (pipelines, `pipeline/make.rs` pending jobs, `pipeline/host.rs`) keeps its own strong reference, so live shaders keep their identity and their translation; an evicted container is simply rebuilt (and retranslated or read from the disk cache) on next use. The default path is unchanged and the three tests exercise the cache directly. Note that no memory saving in the title has been measured yet; the README section says so. Thanks for the rebase: it now merges cleanly with `main` (940d32f), so this is a full review of the new head. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
srijann07 commented 2026-11-25 07:03:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by srijann07 on 2026-11-25T06:09:16Z.


Rebased onto upstream main (940d32f). All conflicts have been resolved, and fresh Windows CI, both Clippy configurations and tests passed.

CI: https://github.com/srijann07/VIctory/actions/runs/45557268421

Ready for review. Thanks!

<!-- greyfinch-sync:Marvisak/VIctory:comment:7154497387 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1584#issuecomment-7154497387). Originally posted by [srijann07](https://github.com/srijann07) on 2026-11-25T06:09:16Z. --- Rebased onto upstream main (940d32f). All conflicts have been resolved, and fresh Windows CI, both Clippy configurations and tests passed. CI: https://github.com/srijann07/VIctory/actions/runs/45557268421 Ready for review. Thanks!
fuzeofficlal commented 2026-11-26 01:04:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T01:03:00Z.


Code review (concurrency/refcount audit) and RTX 5090 + WARP verification at head 1ba845ee1287 merged cleanly onto main (63bcca43c6a0):

  • Code review (crates/victory-gpu/src/pipeline.rs:953-964):
    • Opt-in (VICTORY_TRIM_IDLE_SHADERS == "1" cached in OnceLock<bool>), completely silent by default.
    • Thread safety of trim_idle() (Arc::strong_count(shader) > 1): because ShaderCache is exclusively guarded by SHADERS: Mutex<Option<ShaderCache>> and new clones can only originate from a lookup holding SHADERS or from an existing external Arc, observing strong_count == 1 under the lock guarantees no live external reference exists, preventing premature eviction with no nested lock acquisition. Amortized every 256 cache misses with shrink_to_fit() only on large shrink.
  • Verified (Windows 11, NVIDIA RTX 5090 + WARP, VICTORY_D3D12_DEBUG=1, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy --locked -p victory-gpu --tests -- -D warnings (default + --features diagnostics): 0 warnings
    • shader_cache pipeline::: 30/30 passed default on RTX 5090; 33/33 passed (--features diagnostics) on both RTX 5090 and WARP (including all 3 new idle-trim tests).

LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7179379045 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1584#issuecomment-7179379045). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T01:03:00Z. --- Code review (concurrency/refcount audit) and RTX 5090 + WARP verification at head `1ba845ee1287` merged cleanly onto `main` (`63bcca43c6a0`): - **Code review (`crates/victory-gpu/src/pipeline.rs:953-964`)**: - Opt-in (`VICTORY_TRIM_IDLE_SHADERS == "1"` cached in `OnceLock<bool>`), completely silent by default. - Thread safety of `trim_idle()` (`Arc::strong_count(shader) > 1`): because `ShaderCache` is exclusively guarded by `SHADERS: Mutex<Option<ShaderCache>>` and new clones can only originate from a lookup holding `SHADERS` or from an existing external `Arc`, observing `strong_count == 1` under the lock guarantees no live external reference exists, preventing premature eviction with no nested lock acquisition. Amortized every 256 cache misses with `shrink_to_fit()` only on large shrink. - **Verified (Windows 11, NVIDIA RTX 5090 + WARP, `VICTORY_D3D12_DEBUG=1`, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy --locked -p victory-gpu --tests -- -D warnings` (default + `--features diagnostics`): 0 warnings - `shader_cache pipeline::`: **30/30 passed** default on RTX 5090; **33/33 passed** (`--features diagnostics`) on both RTX 5090 and WARP (including all 3 new idle-trim tests). LGTM.
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/1584:upstream-pr/1584'
git switch 'upstream-pr/1584'

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/1584'
git switch 'upstream-pr/1584'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1584'
git switch 'upstream-pr/1584'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1584'
git switch 'main'
git merge --squash 'upstream-pr/1584'
git switch 'main'
git merge --ff-only 'upstream-pr/1584'
git switch 'main'
git merge 'upstream-pr/1584'
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
Marvisak/VIctory!1583
No description provided.