GPU: the pipeline library is mapped, not read into memory #2130

Closed
r4tedx wants to merge 3 commits from upstream-pr/2132 into main
r4tedx commented 2026-11-25 05:45:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-25T05:45:56Z.


What is wrong

pipeline/library.rs loads the host pipeline library with std::fs::read(host-pipelines.bin) and keeps the Vec<u8> beside the ID3D12PipelineLibrary1 for the whole run (Library::_blob), because D3D12 borrows the serialized bytes for as long as the library object lives. The file grows with every run's new pipelines, to hundreds of megabytes, and all of it is private committed memory of the process for the run.

What changes

  • The file is mapped read-only instead (MappedBlob: file handle, section and view kept beside the library). Its pages are file-backed, so they cost no private memory and the system can drop them.
  • The file is opened sharing read access only: while the library is alive nobody, this process included, can write, truncate, rename or delete it. On this machine an in-place write and a truncation fail, and the old save, a rename over the file, fails with ERROR_ACCESS_DENIED. That is what makes a mapping safe to keep under D3D12.
  • So a save no longer replaces the file. It writes and flushes a staging file, then publishes it under a new name, host-pipelines.bin.<n> with n one higher than any present (MoveFileExW without replace, so two savers cannot take the same name). Older files are removed where they can be; the one this run maps stays until it ends.
  • A start opens the newest file that opens: newest by when it was written, then by number. An empty file, or one held for the moment by another process, leaves the next older one to try. Only files older than the one opened are removed. The original host-pipelines.bin counts as the first, so an existing library is picked up.
  • Staging files left by a process that ended during a save (each as big as the library) are removed at the next start. A running process's are left alone.
  • A periodic save gives way to one already under way instead of waiting for its write: the pipeline workers call save(false) after every pipeline. The shutdown save still waits.

The pre-built list (tag) is still computed over the bytes the library was made from.

Measured (synthetic, no GPU and no title)

A 256 MiB library file, private memory of the test process (PrivateUsage):

before load after load
read into a Vec (as on main) 1.57 MiB 258.09 MiB
mapped (this PR) 1.59 MiB 2.09 MiB

About 256 MiB less private memory for a 256 MiB library, for as long as the run lasts. The title's own library size varies by machine and run; I did not run the title.

Tests

GPU-free, in pipeline::library::tests:

  • mapped_blob_matches_file: the mapped bytes and their tag are the file's.
  • mapped_blob_survives_saves: with a mapping held, an in-place write, a truncation and a rename over the file all fail and the mapped bytes stay. Saves publish new generations, and a fresh load sees the newest. Older files go once unmapped. A dead process's staging file goes; this process's stays.
  • mapped_blob_skips_missing_and_empty_files.
  • mapped_blob_takes_the_newest_file_that_opens: an empty newest generation falls back to the one before. An original-name file written after the numbered ones (a build that writes only that name) wins.
  • mapped_blob_private_memory (ignored): the measurement above.

The two existing tests that build a library on a device now go through a mapped temporary file.

cargo test -p victory-gpu --features diagnostics with VICTORY_D3D12_WARP=1 (software adapter): 150 passed, 0 failed, 3 ignored. cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings is clean.

Limits

  • Disk: while a run lives, its mapped generation plus the newest save are on disk (three files during a save). The next start removes the old ones.
  • A build without this change reads only host-pipelines.bin, which this one removes once it has published a numbered file. Switching such a build in starts with an empty library, which costs pipeline compilation, not correctness.
  • A read error of the disk under the mapping now surfaces when the driver reads the page, not at start-up.

Credit

The fork fxceless/victory-fork maps this file and reports 1,352 MB saved. It also traced a device loss to the file being replaced while mapped, which is what the read-only share mode and the numbered files rule out here. The idea is theirs; the code is written for main.

AI-assisted (Claude Code, with a Codex sub-agent for the first version). No game data was used.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6598103754 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2132). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-25T05:45:56Z. --- ## What is wrong `pipeline/library.rs` loads the host pipeline library with `std::fs::read(host-pipelines.bin)` and keeps the `Vec<u8>` beside the `ID3D12PipelineLibrary1` for the whole run (`Library::_blob`), because D3D12 borrows the serialized bytes for as long as the library object lives. The file grows with every run's new pipelines, to hundreds of megabytes, and all of it is private committed memory of the process for the run. ## What changes - The file is **mapped read-only** instead (`MappedBlob`: file handle, section and view kept beside the library). Its pages are file-backed, so they cost no private memory and the system can drop them. - The file is opened sharing **read access only**: while the library is alive nobody, this process included, can write, truncate, rename or delete it. On this machine an in-place write and a truncation fail, and the old save, a rename over the file, fails with `ERROR_ACCESS_DENIED`. That is what makes a mapping safe to keep under D3D12. - So a save no longer replaces the file. It writes and flushes a staging file, then publishes it under a new name, `host-pipelines.bin.<n>` with `n` one higher than any present (`MoveFileExW` without replace, so two savers cannot take the same name). Older files are removed where they can be; the one this run maps stays until it ends. - A start opens the newest file that opens: newest by when it was written, then by number. An empty file, or one held for the moment by another process, leaves the next older one to try. Only files older than the one opened are removed. The original `host-pipelines.bin` counts as the first, so an existing library is picked up. - Staging files left by a process that ended during a save (each as big as the library) are removed at the next start. A running process's are left alone. - A periodic save gives way to one already under way instead of waiting for its write: the pipeline workers call `save(false)` after every pipeline. The shutdown save still waits. The pre-built list (`tag`) is still computed over the bytes the library was made from. ## Measured (synthetic, no GPU and no title) A 256 MiB library file, private memory of the test process (`PrivateUsage`): | | before load | after load | |---|---|---| | read into a `Vec` (as on `main`) | 1.57 MiB | 258.09 MiB | | mapped (this PR) | 1.59 MiB | 2.09 MiB | About 256 MiB less private memory for a 256 MiB library, for as long as the run lasts. The title's own library size varies by machine and run; I did not run the title. ## Tests GPU-free, in `pipeline::library::tests`: - `mapped_blob_matches_file`: the mapped bytes and their `tag` are the file's. - `mapped_blob_survives_saves`: with a mapping held, an in-place write, a truncation and a rename over the file all fail and the mapped bytes stay. Saves publish new generations, and a fresh load sees the newest. Older files go once unmapped. A dead process's staging file goes; this process's stays. - `mapped_blob_skips_missing_and_empty_files`. - `mapped_blob_takes_the_newest_file_that_opens`: an empty newest generation falls back to the one before. An original-name file written after the numbered ones (a build that writes only that name) wins. - `mapped_blob_private_memory` (ignored): the measurement above. The two existing tests that build a library on a device now go through a mapped temporary file. `cargo test -p victory-gpu --features diagnostics` with `VICTORY_D3D12_WARP=1` (software adapter): 150 passed, 0 failed, 3 ignored. `cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings` is clean. ## Limits - Disk: while a run lives, its mapped generation plus the newest save are on disk (three files during a save). The next start removes the old ones. - A build without this change reads only `host-pipelines.bin`, which this one removes once it has published a numbered file. Switching such a build in starts with an empty library, which costs pipeline compilation, not correctness. - A read error of the disk under the mapping now surfaces when the driver reads the page, not at start-up. ## Credit The fork `fxceless/victory-fork` maps this file and reports 1,352 MB saved. It also traced a device loss to the file being replaced while mapped, which is what the read-only share mode and the numbered files rule out here. The idea is theirs; the code is written for `main`. AI-assisted (Claude Code, with a Codex sub-agent for the first version). No game data was used. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-25 06:02:38 +00:00
catminou4 commented 2026-11-25 06:02:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T05:49:03Z.

Review: COMMENTED


Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Pipeline library mapped, not read into memory — possible duplicate of #2129; if both exist, one should be closed. Mapping avoids a full read+alloc, good for startup memory.

<!-- greyfinch-sync:Marvisak/VIctory:review:6052484900 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2132#pullrequestreview-6052484900). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T05:49:03Z. **Review:** COMMENTED --- Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff. Pipeline library mapped, not read into memory — **possible duplicate of #2129**; if both exist, one should be closed. Mapping avoids a full read+alloc, good for startup memory.
Dathinel commented 2026-11-25 06:02:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dathinel on 2026-11-25T05:49:15Z.


One integration detail for the pipeline-cache PRs: at 226ab31c, loading uses the new mapped backing, while save() still allocates vec![0u8; l.library.GetSerializedSize()] before calling Serialize. The load-side memory measurement therefore leaves the separate save-time allocation in #2007 open.

I ran git merge-tree --write-tree <left> <right> on the three pairs of PR heads. Each reports a content conflict in crates/victory-gpu/src/pipeline/library.rs. If this generation-based version lands, the fallible reservation from #2007 needs to be reapplied to its save path. #2129 takes a different route there (mapped staging file with a fallible buffer fallback). This is a source and merge check only; I didn't measure peak save memory. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7154169562 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2132#issuecomment-7154169562). Originally posted by [Dathinel](https://github.com/Dathinel) on 2026-11-25T05:49:15Z. --- One integration detail for the pipeline-cache PRs: at `226ab31c`, loading uses the new mapped backing, while `save()` still allocates `vec![0u8; l.library.GetSerializedSize()]` before calling `Serialize`. The load-side memory measurement therefore leaves the separate save-time allocation in #2007 open. I ran `git merge-tree --write-tree <left> <right>` on the three pairs of PR heads. Each reports a content conflict in `crates/victory-gpu/src/pipeline/library.rs`. If this generation-based version lands, the fallible reservation from #2007 needs to be reapplied to its save path. #2129 takes a different route there (mapped staging file with a fallible buffer fallback). This is a source and merge check only; I didn't measure peak save memory. AI-assisted.
r4tedx commented 2026-11-25 06:02:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-25T05:52:02Z.


Closing as a duplicate of #2129, which was opened a few minutes before this one and also removes the save-time copy that @Dathinel points out above. What this branch adds beyond it (read-only sharing of the mapped file, numbered files instead of a rename, removal of staging files left by ended runs, a periodic save that gives way to one under way) is written up on #2129; the branch stays at r4tedx:gpu/pipeline-library-mapped if any of it is wanted there.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7154213855 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2132#issuecomment-7154213855). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-25T05:52:02Z. --- Closing as a duplicate of #2129, which was opened a few minutes before this one and also removes the save-time copy that @Dathinel points out above. What this branch adds beyond it (read-only sharing of the mapped file, numbered files instead of a rename, removal of staging files left by ended runs, a periodic save that gives way to one under way) is written up on #2129; the branch stays at `r4tedx:gpu/pipeline-library-mapped` if any of it is wanted there. AI-assisted (Claude Code).

Pull request closed

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!2130
No description provided.