GPU: every pipeline library load runs under one mutex; startup loads take minutes to drain (fix on a fork) #1615

Open
opened 2026-11-26 04:38:28 +00:00 by github-bot · 3 comments
Owner

@⁠Vaximous on GitHub · 2026-11-24 19:48 UTC · original

Problem

Loading cached pipelines at startup is serialised. With a warm cache, the 1,225 library loads of the early list took about 3.5 minutes to drain. The world then started with about 800 pipelines still queued, so draws were dropped for missing pipelines.

Cause: in crates/victory-gpu/src/pipeline/library.rs (from 35b054e), one Mutex<Library> covers every LoadGraphicsPipeline / LoadComputePipeline / StorePipeline call, and a load can take up to seconds. A periodic save also holds that mutex while it serialises the library, which reaches gigabyte size. As a result, the pipeline workers load one at a time and wait for each other.

D3D12 does not require this. Per ID3D12Device1::CreatePipelineLibrary ("Thread Safety"), the library is free-threaded and synchronises itself. The only exception is that loads of the same pipeline name must not overlap.

Fix

  • Loads and stores take one of 64 striped mutexes, chosen by name. Two loads of the same name never overlap, and loads of different names run in parallel.
  • Stores also take a shared RwLock. A save takes it exclusively, so the serialised size can't change between GetSerializedSize and Serialize. Loads don't touch it, so a save never blocks them.
  • A periodic save that finds another save running is skipped (try_lock). Periodic saves back off: 20, 40, 80, then every 160 s, and at least 30x the last save's duration. The final save at the end of a run is unchanged.

One file, crates/victory-gpu/src/pipeline/library.rs, +127/-46, with a test for the save schedule.

Branch

https://github.com/Vaximous/VIctory/compare/main...fix/pipeline-library-locks — commit 5fd3b27, based on ffe310e.

Tested

  • cargo test -p victory-gpu: 51 passed, 0 failed, 1 ignored, including pipeline::library::tests::*. Clippy -D warnings is clean with and without diagnostics.
  • Runtime (RTX 4070 Ti, Windows 11): measured on our local integration build (main plus a set of open PRs, including the save-retry PRs #72/#347), not on plain main. Two runs of 330 s each, from the same starting cache, before vs after:
    • Early library loads (1,225): drained in about 3.5 min vs about 30 s.
    • Pipelines still queued when the world started: about 800 vs 0.
    • Longest worker lock wait: 2-5 s vs 0-0.6 s.
    • Draws dropped for a missing pipeline: 13.7k vs 7.9k.
    • Library saves: 13 vs 4.

#275 moves periodic saves to a background thread, but loads still share one mutex. #780 covers pipeline library name mismatches (separate). #72 and #640 retry saves.

AI use

Written with Claude Code (Claude Opus 5.5); tested locally.

**@⁠Vaximous** on GitHub · 2026-11-24 19:48 UTC · [original](https://github.com/Marvisak/VIctory/issues/1615) ## Problem Loading cached pipelines at startup is serialised. With a warm cache, the 1,225 library loads of the early list took about 3.5 minutes to drain. The world then started with about 800 pipelines still queued, so draws were dropped for missing pipelines. Cause: in `crates/victory-gpu/src/pipeline/library.rs` (from 35b054e), one `Mutex<Library>` covers every `LoadGraphicsPipeline` / `LoadComputePipeline` / `StorePipeline` call, and a load can take up to seconds. A periodic save also holds that mutex while it serialises the library, which reaches gigabyte size. As a result, the pipeline workers load one at a time and wait for each other. D3D12 does not require this. Per `ID3D12Device1::CreatePipelineLibrary` ("Thread Safety"), the library is free-threaded and synchronises itself. The only exception is that loads of the same pipeline name must not overlap. ## Fix - Loads and stores take one of 64 striped mutexes, chosen by name. Two loads of the same name never overlap, and loads of different names run in parallel. - Stores also take a shared `RwLock`. A save takes it exclusively, so the serialised size can't change between `GetSerializedSize` and `Serialize`. Loads don't touch it, so a save never blocks them. - A periodic save that finds another save running is skipped (`try_lock`). Periodic saves back off: 20, 40, 80, then every 160 s, and at least 30x the last save's duration. The final save at the end of a run is unchanged. One file, `crates/victory-gpu/src/pipeline/library.rs`, +127/-46, with a test for the save schedule. ## Branch https://github.com/Vaximous/VIctory/compare/main...fix/pipeline-library-locks — commit 5fd3b27, based on ffe310e. ## Tested - `cargo test -p victory-gpu`: 51 passed, 0 failed, 1 ignored, including `pipeline::library::tests::*`. Clippy `-D warnings` is clean with and without `diagnostics`. - Runtime (RTX 4070 Ti, Windows 11): measured on our local integration build (main plus a set of open PRs, including the save-retry PRs #72/#347), not on plain main. Two runs of 330 s each, from the same starting cache, before vs after: - Early library loads (1,225): drained in about 3.5 min vs about 30 s. - Pipelines still queued when the world started: about 800 vs 0. - Longest worker lock wait: 2-5 s vs 0-0.6 s. - Draws dropped for a missing pipeline: 13.7k vs 7.9k. - Library saves: 13 vs 4. ## Related #275 moves periodic saves to a background thread, but loads still share one mutex. #780 covers pipeline library name mismatches (separate). #72 and #640 retry saves. ## AI use Written with Claude Code (Claude Opus 5.5); tested locally.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:38:28 +00:00
Author
Owner

@⁠Revens2 on GitHub · 2026-11-24 21:47 UTC · original

publish: branch for #1615

Branch: https://github.com/Revens2/VIctory/tree/work/issue-1615
Commit: 1bf07ce5
Tests:

Refs #1615

Branch: https://github.com/Revens2/VIctory/tree/work/issue-1615
Commit: 1bf07ce
Base: 70564ae upstream/main
Tests (run on this machine):

  • cargo test --workspace --locked: 129 passed, 2 failed, 5 ignored
  • cargo xtask policy: ok
  • cargo build --release -p victory: ok 0s
    AI-assisted: yes (harness victory-ops); reviewed by a second model before publishing.
    Refs #1615
**@⁠Revens2** on GitHub · 2026-11-24 21:47 UTC · [original](https://github.com/Marvisak/VIctory/issues/1615#issuecomment-7145393610) publish: branch for #1615 Branch: https://github.com/Revens2/VIctory/tree/work/issue-1615 Commit: 1bf07ce5 Tests: Refs #1615 Branch: https://github.com/Revens2/VIctory/tree/work/issue-1615 Commit: 1bf07ce Base: 70564ae upstream/main Tests (run on this machine): - cargo test --workspace --locked: 129 passed, 2 failed, 5 ignored - cargo xtask policy: ok - cargo build --release -p victory: ok 0s AI-assisted: yes (harness victory-ops); reviewed by a second model before publishing. Refs #1615
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 12:49 UTC · original

Automated triage for the maintainers.

main has changed here since ffe310e. In crates/victory-gpu/src/pipeline/library.rs, the load path (the function that calls load and then create) now takes the Library mutex only to clone the pipeline library interface, and runs LoadGraphicsPipeline / LoadComputePipeline outside it; a per-name hold (NAMES.hold) keeps loads of the same name from overlapping. So loads of different names already run in parallel on main (landed with 256d7b9). What is still as described: save holds the same mutex across GetSerializedSize and Serialize, so during a periodic save (every 20 s) new loads wait at that brief clone, and StorePipeline also waits. The save backoff and try_lock skipping from your branch would still apply there.

Pull requests are open to everyone: please rebase the save-side part of the branch onto current main and open it as a PR so it can be reviewed here (#275, which moves periodic saves to a background thread, is related). The second branch posted in the comments has no description of what it changes beyond this issue.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 12:49 UTC · [original](https://github.com/Marvisak/VIctory/issues/1615#issuecomment-7162913942) <!-- victory-issue-triage v1 verdict=answered seen=2026-11-24T21:47:33Z at=2026-11-25T12:49:34Z --> Automated triage for the maintainers. `main` has changed here since `ffe310e`. In `crates/victory-gpu/src/pipeline/library.rs`, the load path (the function that calls `load` and then `create`) now takes the `Library` mutex only to clone the pipeline library interface, and runs `LoadGraphicsPipeline` / `LoadComputePipeline` outside it; a per-name hold (`NAMES.hold`) keeps loads of the same name from overlapping. So loads of different names already run in parallel on `main` (landed with `256d7b9`). What is still as described: `save` holds the same mutex across `GetSerializedSize` and `Serialize`, so during a periodic save (every 20 s) new loads wait at that brief clone, and `StorePipeline` also waits. The save backoff and `try_lock` skipping from your branch would still apply there. Pull requests are open to everyone: please rebase the save-side part of the branch onto current `main` and open it as a PR so it can be reviewed here (#275, which moves periodic saves to a background thread, is related). The second branch posted in the comments has no description of what it changes beyond this issue. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠pes7 on GitHub · 2026-11-26 15:21 UTC · original

This is one of the two issues #1653 is about (the other is what a save writes).

The two halves of the mutex problem, as they stand here:

  1. A load waited out a whole save. A save holds the library's lock while it serialises the entire library — 230-600 MB in the world — and that was the same lock every load took. #2179 took the first step by keeping the library a second time beside the lock, so loads take the interface and never the lock. That change is on main and #1653 now ports it onto the sharded code, with #2179's own test (a_pipeline_loads_while_a_save_holds_the_library — it holds a shard's lock and requires a stored pipeline to load within 30 s; the version that cloned under the lock times out).

  2. One lock for everything. Even with loads out of the way, stores and saves of unrelated pipelines serialised: a save of the whole library blocked every store, and two saves could not overlap. #1653 splits the library into 16 files by pipeline-name hash, so a save serialises and writes only the shards that gained pipelines, and each shard has its own lock: a save of shard 3 blocks stores of shard 3, not the other fifteen. The shard a pipeline lands in is a hash of its name, so it is stable across runs — which is what lets a save write only the files that changed.

The startup side of it (the first-run pre-build, where all ~2 900 title pipelines are made) is unaffected by either: it is one long store sequence with nothing to load yet.

Worth knowing for anyone merging in this area: #2328 ("saves run one at a time"), #3181 and #3188 ("save the early list without re-serialising") are outcomes #1653 gives by construction — snapshot+publish are one transaction under a single guard, and a shard that gained nothing is not serialised at all — so those three and #1653 are alternatives rather than neighbours. The full overlap map of that file (six other open PRs) is in #1633.

**@⁠pes7** on GitHub · 2026-11-26 15:21 UTC · [original](https://github.com/Marvisak/VIctory/issues/1615#issuecomment-7196143102) This is one of the two issues #1653 is about (the other is what a save writes). The two halves of the mutex problem, as they stand here: 1. **A load waited out a whole save.** A save holds the library's lock while it serialises the entire library — 230-600 MB in the world — and that was the same lock every load took. #2179 took the first step by keeping the library a second time beside the lock, so loads take the interface and never the lock. That change is on `main` and #1653 now ports it onto the sharded code, with #2179's own test (`a_pipeline_loads_while_a_save_holds_the_library` — it holds a shard's lock and requires a stored pipeline to load within 30 s; the version that cloned under the lock times out). 2. **One lock for everything.** Even with loads out of the way, stores and saves of unrelated pipelines serialised: a save of the whole library blocked every store, and two saves could not overlap. #1653 splits the library into 16 files by pipeline-name hash, so a save serialises and writes only the shards that gained pipelines, and each shard has its own lock: a save of shard 3 blocks stores of shard 3, not the other fifteen. The shard a pipeline lands in is a hash of its name, so it is stable across runs — which is what lets a save write only the files that changed. The startup side of it (the first-run pre-build, where all ~2 900 title pipelines are made) is unaffected by either: it is one long store sequence with nothing to load yet. Worth knowing for anyone merging in this area: #2328 ("saves run one at a time"), #3181 and #3188 ("save the early list without re-serialising") are outcomes #1653 gives by construction — snapshot+publish are one transaction under a single guard, and a shard that gained nothing is not serialised at all — so those three and #1653 are alternatives rather than neighbours. The full overlap map of that file (six other open PRs) is in #1633.
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#1615
No description provided.