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

Closed
github-bot wants to merge 3 commits from gh-pr/2132/head into gh-pr/2132/base
Owner

@⁠r4tedx on GitHub · 2026-11-25 05:45 UTC · original
r4tedx:gpu/pipeline-library-mapped → main · +424 −25 · 2 files · 3 commits · closed without merging

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

**@⁠r4tedx** on GitHub · 2026-11-25 05:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/2132) `r4tedx:gpu/pipeline-library-mapped` → `main` · +424 −25 · 2 files · 3 commits · closed without merging ## 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)
D3D12 keeps borrowing the serialized library after creation, so reading the file kept a private copy for the whole run. Keep a read-only view and its handles beside the library and deny write and delete sharing.

Flush each save to a staging file and publish it under a fresh generation. The next open takes the newest generation and reclaims older files that are no longer held. The original filename is still accepted as the first generation.

GPU-free tests check the bytes and tag, empty and missing files, write and rename protection, and fresh opens after saves. An ignored synthetic test measures process-private memory for a 256 MiB blob.
Review follow-ups to mapping the pipeline library:

- A periodic save held its lock through the whole write and flush of a
  library of hundreds of MB, and the workers call it after every
  pipeline: each waited for the save. A periodic save now gives way to one
  under way; the shutdown save still waits for it.
- A save cut short left a staging file as big as the library, named for
  its process, which nothing removed. At the next load those of processes
  that are no longer running are removed.
- The load took the newest generation or nothing: an empty newest file,
  or one another process held for the moment, started an empty library
  though older ones were there. It now takes the newest file that opens,
  newest by when it was written and then by generation, so a library a
  build that writes only the original filename saved later is not
  outvoted by an older numbered file.
- Names compare without case, as the file system does, and the loops that
  look for a free staging name or generation are bounded.

Tests: mapped_blob_survives_saves now checks which staging files go, and
mapped_blob_takes_the_newest_file_that_opens covers the fallback and the
original filename written later.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Second review's follow-ups: a load removed every file but the one it
opened, so a newer one that did not open for the moment (another process
holding it, or a time before its real one after the clock was set back)
was lost. Only older ones go now. A staging file is no longer shared while
it is written, and only "no such process" counts as a process that ended;
the test sets the file times itself instead of relying on their
resolution.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:55:39 +00:00
github-bot closed this pull request 2026-11-26 04:55:39 +00:00
Author
Owner

@⁠Dathinel on GitHub · 2026-11-25 05:49 UTC · original

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.

**@⁠Dathinel** on GitHub · 2026-11-25 05:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/2132#issuecomment-7154169562) 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.
Author
Owner

@⁠r4tedx on GitHub · 2026-11-25 05:52 UTC · original

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).

**@⁠r4tedx** on GitHub · 2026-11-25 05:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/2132#issuecomment-7154213855) 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

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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!2132
No description provided.