Skip to content

victory-gpu: split the pipeline library into shards so a save writes only what changed (was 600 MB per save) - #1653

Open
pes7 wants to merge 1 commit into
Marvisak:mainfrom
pes7:perf/pipeline-library-shards
Open

pes7 wants to merge 1 commit into
Marvisak:mainfrom
pes7:perf/pipeline-library-shards

Conversation

@pes7

@pes7 pes7 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Every save writes the whole 600 MB pipeline library. The host can only serialise a library whole, so splitting it into shards - one file each, chosen by the pipeline's name hash - lets a save write only the shards that gained pipelines. owner: GPU-PIPELINE-LIBRARY-SIZE in #1633. ## The problem, as the task states it The host pipeline library is one serialised blob, over 600 MB in the world, and ID3D12PipelineLibrary::Serialize has no per-entry form: the whole thing is copied into one buffer and written, every time a save happens (every 20 s while new pipelines are still being made, and at shutdown). Serialize itself copies 600 MB, then write_atomically writes 600 MB to a temp file and renames it. ## What changes The library becomes 16 shards, host-pipelines.00.bin ... host-pipelines.15.bin: - shard_of(name) hashes the pipeline's name (already a hash of its whole host description) and picks a shard. Stable across runs, so a pipeline always lands in the same file - that is what makes a partial save correct. - libraries() opens every shard from its own file; a missing file is an empty library, a rejected blob is an empty library, exactly as the single file behaved. - pipeline() looks a pipeline up in its own shard only (no extra lookups: the routing is deterministic) and stores it there on a miss. The per-shard mutex also means pipeline creation contends 16 ways instead of one. - save() serialises and writes only the shards with added > 0, and writes the early list only when it changed. The log line reports shards = K, bytes = B instead of one blob's size. A save that adds pipelines spread over K shards writes K/16 of the library instead of all of it. In the steady state a run touches one or two shards, so a 600 MB write becomes roughly 40-75 MB. ## Measured What I can measure here, without the title: - a_pipeline_lands_in_one_shard_and_the_shards_spread: the same name always maps to the same shard, and 4 096 names reach all 16 shards, so a save is a fraction rather than all or nothing. - The bytes a save writes are printed by the existing log line (host pipeline library saved), which now carries shards and bytes - so the first run in the world gives the number directly: the old line reported bytes тХд╨ТтХи╨йтХи╨и 600 MB, the new one reports the sum of the changed shards. Not measured: the wall time of a save in the world. It is Serialize + one write per changed shard, so it scales with the same fraction. ## Correctness Rewritten onto main 940d32f, where this module gained the pre-build's prebuilt list. That list is now one file per shard (prebuilt-pipelines.NN.txt), each tagged with its own shard's bytes: a single list tagged with the whole library would be invalidated by any shard's write, which would undo the point of sharding. The concurrency added upstream at the same time is kept as it is: one thread at a time per pipeline name (Names/Held), the interface cloned out of the lock so loads do not hold it, and the SAVE lock inside write_atomically - the sharding only decides which of the sixteen libraries a name is looked up in.

  • Same names, same descriptions, same lookups: a pipeline is created, stored and found in one deterministic shard. - a_rejected_blob_gives_an_empty_library still passes, and the existing name test is unchanged. - One-time cost on upgrade: the old single host-pipelines.bin is no longer read, so the first run after this rebuilds the library once (and the old file can be deleted). cargo test --workspace --locked, both clippy configurations with -D warnings, cargo xtask policy. No game data used.

Correction, from aycow's control

Sharding wins only while few shards are dirty. aycow measured a 16 MiB synthetic payload through the same publisher:

dirty shards sharded whole file
1 1.56 ms 8.21 ms
4 5.59 ms 7.85 ms
16 24.15 ms 8.55 ms

So the first run after this lands - every shard new, i.e. all sixteen dirty - is worse than one blob, because each shard pays its own serialisation. Every save after that, in the steady state of one to four dirty shards, is 1.4x to 5.3x better. Native PSO serialisation is unmeasured in both directions.

Two persistence holes aycow found are fixed in the current head (9c60694): a shard's dirty count is cleared only after its file is published (a failed write retries instead of losing the pipelines), and one SAVING lock covers serialisation and publication together (two saves cannot publish an older snapshot over a newer one).

What it gives (to decide the merge): a save writes only the library shards that gained pipelines, instead of the whole 926 MB (1.7 GB on NVIDIA) every 20 s while holding the library's lock; a cache made before the split is still read, and what it holds moves into the shards as it is used.

State: MERGEABLE, CI check green, rebased onto today's main (f3578e3).

Risk: the library format moves from one file to 16; a rejected blob still gives an empty shard, and a failed write leaves the shard dirty for the next save.

@aycow

aycow commented Nov 24, 2026

Copy link
Copy Markdown

Independent checks on exact 88b678b found two persistence concerns. Dirty flags are cleared before successful file publication, and its save-order lock does not cover serialization and publication together. The former can lose retries and the latter allows an older complete snapshot to replace a newer one. #1660's unique temporary paths address a separate failure: the unchanged #1653 writer failed 35/256 concurrent synthetic replacements on Windows, while the #1660 writer passed 256/256.

For a 16 MiB synthetic serialized-payload control using the same atomic publisher in both modes, one dirty shard took median 1.56 ms versus 8.21 ms whole-file, four took 5.59 ms versus 7.85 ms, and all 16 took 24.15 ms versus 8.55 ms. These timings isolate payload preparation and publication. Native PSO serialization and title performance are unmeasured. I also reran all seven #1651 tests, including a real WARP PSO cache round-trip. Those comparisons may help your save-order and fallback choices.

AI-assisted using Codex.

pes7 added a commit to pes7/VIctory that referenced this pull request Nov 24, 2026
… one at a time

aycow's review of Marvisak#1653 found two persistence holes, both real.

`added` was cleared when the shard was *serialised*, before the write: a
failed write left the shard marked clean, so its pipelines were never
retried and died with the process. It is cleared after the file is
published now, by the count that was serialised - pipelines stored while
the write ran stay dirty and go out next time.

The save-order interval lock was released before serialisation and
publication, so two saves could interleave and publish an older snapshot
over a newer one. One `SAVING` lock now covers both, per save.
@pes7

pes7 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks - both are real, and fixed in f26e6ae:

  • added is cleared only after write_atomically succeeded, by the count that was serialised, so a failed write leaves the shard dirty and the next save retries it (and pipelines stored while the write ran stay dirty too).
  • a SAVING mutex now spans serialisation and publication, so two saves cannot interleave and publish an older snapshot over a newer one.

Your control also changes the claim in this description, so I have corrected it rather than leaving "K/16" standing as if it held everywhere:

dirty shards sharded whole file
1 1.56 ms 8.21 ms
4 5.59 ms 7.85 ms
16 24.15 ms 8.55 ms

So sharding wins in the steady state - one to four dirty shards - and loses when every shard is dirty, which is exactly the first world load, because each shard pays its own serialisation. Anyone reading this should expect the first run after the upgrade to be worse and every later save to be better; the sentence in the description now says that.

On the #1660 point: each shard publishes through its own temporary path (host-pipelines.NN.tmp) and the SAVING lock serialises writers inside the process, so the fixed-path concurrent-replacement failure you measured should not reach this code. If you can point your 256-replacement test at this branch it would settle it rather than my assuming it - I would rather have the number.

@pes7

pes7 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@aycow - your two findings are still addressed, and the branch is rewritten on today's main (70564ae), which changed this module underneath it (prebuilt lists keyed by a tag of the library bytes, Used split into before/now). The rebase conflicted in seven places, so rather than resolving a three-way merge I re-applied the sharding to the current file; head is 1e79f14.

Both of your concerns, on the new code:

  • a shard's added counter and prebuilt_changed flag are cleared only after write_atomically returned Ok, by what was serialised (l.added -= added; l.prebuilt_changed &= !marked;), so a failed write leaves the shard dirty for the next save and a store during the write keeps it dirty;
  • SAVING is held across serialising and publishing every shard, so two saves cannot interleave and publish an older complete snapshot over a newer one.

Two changes from the revision you reviewed, both because the file moved:

  • the pre-build's list is now one file per shard (prebuilt-pipelines.NN.txt), each tagged with its own shard's bytes - a single list tagged with the whole library would be invalidated by any shard's write, which would undo the point of sharding;
  • the temporary path per shard (host-pipelines.NN.tmp) stays as it was.

Your control still decides the shape of the claim, and the description now says it plainly: 1.56 / 5.59 / 24.15 ms for 1 / 4 / 16 dirty shards against 8.21 / 7.85 / 8.55 ms for one blob, so sharding wins in the steady state and loses on the first save after the upgrade, when every shard is dirty.

cargo test --workspace --locked (79 tests in this crate, including a new one that a corpus of names spreads over all SHARDS), both clippy configurations with -D warnings, cargo xtask policy. Native PSO serialisation and title performance are still unmeasured - no game copy here.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Pipeline library split into shards: only shards that gained pipelines are serialised and written on save; shard_file() maps shard→file. 1 test. Pairs with #1651 (save only changed shards) — check whether these two conflict or compose.

@pes7

pes7 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@catminou4 - good catch, and they do not compose: #1651 (avt0m4t48) and this one are two takes on the same change, both splitting pipeline/library.rs into sixteen files and both writing only the shards that gained pipelines. Only one can land. The differences, so the owner can pick:

#1651 this one
shard chosen by first hex digit of the content hash shard_of(name) over the name's bytes (same idea, one small function)
existing host-pipelines.bin kept, misses fall back to it and promote into a shard, so warm legacy entries survive not read: one rebuild after the upgrade, then it can be deleted
pre-build's list not mentioned per shard, tagged with that shard's bytes
state CONFLICTING, +506/-95, new library/disk.rs, and it lists #275/#1041/#640 conflicts in this file MERGEABLE, +146/-65, one file
verified on Linux tests + a Windows WARP run of the real cache round-trip Windows hardware, full workspace + a shard-spread test

So: #1651 is the more thorough one (legacy fallback, cross-process note, a dedicated module), mine is the smaller one that applies today. I am not going to argue for mine - if you would rather have the legacy fallback, take #1651 and say so, and I will close this one the same way I closed #1556 and #1656 when main had moved past them.

What I would keep from this one either way, since #1651's description does not mention it: the per-shard pre-build list. A single list tagged with the whole library is invalidated by any shard's write, so after sharding it would be rewritten almost every save and would undo part of the win. It is a small change on top of whichever version lands.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work. The re-application onto current main (crates/victory-gpu/src/pipeline/library.rs) keeps Shared, zeroed and write_atomically, and it adds a read-only fallback to the one-file library, which is a good idea. But it loses the fix this PR had at 013da61:

  • Pipelines stored during a save are lost from the dirty count again. save serialises a shard under its lock (serialise), releases the lock to write the file, and then sets l.added = 0; l.prebuilt_changed = false; unconditionally. A pipeline stored, or a pre-build key noted, in that window is cleared without being in the published bytes. Unless that shard gains another pipeline, it is never written, not even at the shutdown save. This is the race andrewpozdnakov7-jpg reproduced. Please acknowledge only what was serialised (subtract the added count you took, and compare prebuilt.len() with the snapshot), and bring back his save_shard regression test, which is also no longer in the branch.
  • Minor: USED_CHANGED is swapped to false before the early list is written. If that write fails, the change is not retried. main had the same shape, but the generation check you described for 013da61 is not in this head either.
  • Two smaller points. The legacy library stays mapped in memory for the whole run (926 MB in your measurement) alongside the shards; please say whether that matters on smaller cards. If any shard fails to open, libraries() returns None with no warning, where main logged "no host pipeline library".

The choice between this PR and #1651 (the same sharding) is still open for the maintainers. Push the fix and this will be reviewed again.

CI: passing (check).


Generated by Claude Code

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I reproduced a remaining publication race at 1e79f14. The shard snapshots its prebuilt keys and releases the mutex to write the files. If a new key is recorded during that write while the dirty flag was already true, acknowledging the first snapshot clears the flag for the new key too. A second save can then skip that shard even though its prebuilt file is missing the new key.

Fix and regression on the exact PR head. It compares the current number of prebuilt keys with the snapshot count after both files publish. The test inserts a key at the start of the write, checks that the first sidecar contains just the original key, then verifies that the second save writes both.

On native Windows/WARP the same test failed before the fix and passed after it. The GPU library suite went from 81 passed, 2 failed, 1 ignored to 82 passed, 1 failed, 1 ignored. The placed-view texture-format test failed in both runs. Clippy passed with diagnostics and warnings denied.

If you fold the fix or test into your commit, please retain this trailer.

Co-authored-by: andrewpozdnakov7-jpg andrewpozdnakov7-jpg@users.noreply.github.com

AI-assisted. Synthetic data only.

zhaibingye pushed a commit to zhaibingye/VIctory that referenced this pull request Nov 25, 2026
A load still took the library's lock to clone the interface, and a save
holds that lock while it serialises the whole library (230-600 MB in the
world, Marvisak#1683, Marvisak#1653). Saves run every 20 s while pipelines are being
made, some on the pipeline workers themselves, so every load waited out
each save: the part of Marvisak#1615 that Marvisak#1000 left.

The library is kept a second time beside its lock, and loads use that
one without locking. A load adds nothing to the library, so the size a
save asks for still holds when it serialises; the library is
free-threaded but for loads of one name, which NAMES keeps apart. Stores
and saves keep the lock.

New test: a stored pipeline loads while the lock is held as a save holds
it. With the old locking it times out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pes7
pes7 force-pushed the perf/pipeline-library-shards branch from 1e79f14 to 9c60694 Compare November 25, 2026 12:12
@pes7

pes7 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Re-applied onto main 940d32f, which changed this file again: Names/Held (one thread at a time per name) and a SAVE lock inside write_atomically. Head is 9c60694; the diff is still one file.

Two conflicts, both resolved by keeping your code and routing it through a shard:

let _held = NAMES.hold(name);
// The interface alone under the lock: loads take up to seconds. Its
// blob lives as long as the library, which is never dropped.
let loads = libraries[shard_of(name)]
    .lock()
    .unwrap_or_else(|e| e.into_inner())
    .library
    .clone();

so the per-name hold, the clone-out-of-the-lock trick and the SAVE lock all still do what you made them do; the only change is which of the sixteen libraries a name is looked up in. libraries() opens one file per shard and save() serialises and writes only the shards that gained pipelines, clearing added by what was serialised after the bytes are published.

cargo test --workspace --locked passes, including your three new concurrency tests (different_names_are_held_at_the_same_time, one_name_is_held_by_one_thread_at_a_time, pipelines_load_on_many_threads_at_once) and the new one here (a_pipeline_lands_in_one_shard_and_the_shards_spread). Both clippy configurations with -D warnings, cargo xtask policy.

Still the open question from the description: #1651 is the same change from another author, so this one only needs a decision, not more work.

pes7 added a commit to pes7/VIctory that referenced this pull request Nov 25, 2026
A save snapshots a shard's prebuilt keys, releases the lock and writes the
files. A key recorded while it writes was not in the file, but clearing the
flag by "was it set at snapshot time" marked it saved anyway, so the next
save skipped the shard and the published list never gained the key.

Acknowledge only what was published: the shard stays dirty when its key
count changed while the write ran. `save_shard` is split out so the race is
testable, and the regression test injects a key from inside the write - it
fails on the old check and passes on this one.

`USED_CHANGED` had the same shape, so the early list is acknowledged the
same way: a generation counter bumped by `note_used` decides whether the
list that was written is still the whole list.

Fix and regression test by andrewpozdnakov7-jpg, from the analysis on Marvisak#1653.

Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com>
@pes7

pes7 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@andrewpozdnakov7-jpg @Marvisak - taken, with the trailer, at 013da61.

The race was real and I had introduced it rewriting save() for shards: acknowledging "was the flag set when I snapshotted" instead of "is what I published still everything". Fixed the way you wrote it - the shard stays dirty when its key count changed while the write ran:

// Acknowledge only successfully published changes. Updates that arrived
// while the write ran must stay dirty for the next save.
let mut l = library.lock().unwrap_or_else(|e| e.into_inner());
l.added -= added;
l.prebuilt_changed = l.prebuilt.len() != prebuilt_count;

Your save_shard(.., write) split is what makes it testable, so I kept it verbatim; your test is in the PR as sent. I also checked it can fail: with the old check put back it panics on assert!(second.is_some(), ...) and with the fix it passes, so it is a regression test and not just documentation.

USED_CHANGED had the same shape, as the triage said, so the early list is acknowledged the same way - note_used bumps a generation counter and a save clears the flag only if the generation it wrote is still current. Missing that one only costs a rewrite of the early list, not a lost pipeline, but it is the same bug.

cargo test --workspace --locked (10 library tests, including yours), both clippy configurations with -D warnings, cargo xtask policy.

@pes7

pes7 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Measured on the title, and it is the case this PR was written for - with a bigger number than the description had.

The Xbox build's own pipelines, warmed by translate --cache --pipelines: 27 160 pipelines, and the library they make is 970 507 898 bytes (926 MB), not the 600 MB the README estimates. Every pipeline is ~28 KB of serialised state on average.

Two lines from one run --execute, two seconds apart:

host pipeline library loaded bytes=970507898 ms=57
host pipeline library saved  bytes=970507898 early=1 made=0 loaded=1 ...

The load is cheap (57 ms: the driver maps it). The save is not: the whole 926 MB was written again with made=0 - no pipeline was added, the pre-build's early list changed by one entry, and the writer had no way to write less than the whole blob. That is the 20-second save on main doing 926 MB of I/O for nothing, at every start and every time the early list moves.

With the shards in this PR that save writes no library bytes at all: every shard has added == 0 && !prebuilt_changed, so save_shard returns None for all sixteen and only the early list is published. The 926 MB is not rewritten; the same run writes a few kilobytes.

The rest of the launch, for context: the first frame still takes 21 s of preparing, but the shader cache is not what it waits for (hits=7 misses=0, the warm-up does reach the cache the run reads), and the copies in it are 25 ms for 91 MB - so the wait is pipeline making and the first title-memory snapshots, not translation and not this.

Caveat unchanged from the description: the first save after the upgrade is still the slow one (all sixteen shards dirty), and the native PSO serialisation itself is not what these numbers measure - they are the payload and the I/O around it.

@pes7

pes7 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

A device removal costs more than the session: it takes the pipeline library with it, and the next run starts from nothing.

Measured on this machine just now. The library was 970 507 898 bytes (926 MB) and loaded in 57-73 ms. After the removal that every run ends in (a TDR, #1633), the next run says:

host pipeline library loaded bytes=626800 ms=...

i.e. the blob is 627 KB - the write of the new library at that start-up never finished, and what is on disk is a fragment. From there, every pipeline the title needs is translated and compiled again: the warm-up that took translate --cache --pipelines 7.5 minutes for 27 160 of them (shaders_cached=52496 shaders_translated=0 on a warm one), and made=23057 on a cold one.

Two things about that for this PR:

  • the shards here are what makes it survivable. A shard is written atomically on its own, so a removal mid-save leaves the shards that were written and the ones that were not, not a truncated whole. The USED_GENERATION / SAVING bookkeeping that is already in the branch is exactly the "which of these are from the crash" question.
  • it is also the honest answer to "how much does victory-gpu: split the pipeline library into shards so a save writes only what changed (was 600 MB per save) #1653 save": on a healthy start, 926 MB of I/O with made=0; after a removal, the entire library is rebuilt, which is minutes rather than seconds.

If a "the library on disk is smaller than what the load reported" line is cheap (bytes at the load, bytes at the save), that would name this in one line instead of a 627 KB number noticed by accident - the crash path never reaches the save that would have written the full blob, so nothing in the log says so today.

@pes7

pes7 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Correction to what I wrote above, after reading library::save: the 612 KB is not a truncated write. write_atomically (temp file, then rename) is already there and correct. The small number is a legitimate library that is small: the pipelines in it are named after the shaders they were made from, so a translator change gives every pipeline a new name, the old 926 MB library holds nothing the run can load, and the save writes GetSerializedSize() of what this run made (612 KB) over the file. Atomic, and still a 926 MB / 7.5 minute cache destroyed.

The fix for that is #2706: the library lives in dir/<SOURCE_HASH>, two newest kept, so a run writes only its own directory and bisecting between two builds finds both libraries warm. Measured: run 1 saved bytes=3086 then 7488 with no load; run 2 loaded bytes=7488 ms=0, in pipelines/94ef170ae2f5e8da/.

So the shards in this PR are still right for the "926 MB per save" cost, and no longer need to carry the crash-durability argument - the crash durability is real (Added accumulations are lost with the process, not the file).

Marvisak pushed a commit that referenced this pull request Nov 25, 2026
A load still took the library's lock to clone the interface, and a save
holds that lock while it serialises the whole library (230-600 MB in the
world, #1683, #1653). Saves run every 20 s while pipelines are being
made, some on the pipeline workers themselves, so every load waited out
each save: the part of #1615 that #1000 left.

The library is kept a second time beside its lock, and loads use that
one without locking. A load adds nothing to the library, so the size a
save asks for still holds when it serialises; the library is
free-threaded but for loads of one name, which NAMES keeps apart. Stores
and saves keep the lock.

New test: a stored pipeline loads while the lock is held as a save holds
it. With the old locking it times out.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@pes7

pes7 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Status of this branch against today's main (f3578e3), and what porting it needs.

main reworked the two ends of this file since the branch was written: write_atomically (a save writes a temporary and renames), tag() + prebuilt-pipelines.txt (the pre-build's list holds the translator hash, so a list from another build is ignored), zeroed (a library of 1.7 GB on NVIDIA is not an infallible allocation), the USED/created counters, and library() now hands out a Shared { loads, state } so a pipeline load does not take the library's lock.

The shards themselves are the part that is not in main: nothing there writes only what changed, so every save still serialises the whole library and writes it whole (926 MB here, host pipeline library saved bytes=... every 20 s; on NVIDIA closer to 1.7 GB).

Porting is therefore not a rebase but a re-application of the shard structure onto Shared:

  • libraries() -> &'static [Shared; SHARDS], one shard file each (host-pipelines.NN.bin, prebuilt-pipelines.NN.txt), opened per shard;
  • shard_of(name) unchanged (a hash of the pipeline name, so a pipeline always lands in the same file);
  • pipeline, prebuilt, note_prebuilt pick the shard;
  • save serialises and writes only the shards that gained pipelines (USED_CHANGED/early list once, as now), through zeroed + write_atomically.

Two things to settle while porting: a StorePipeline into a shard that the host refuses (it stays dirty for the next save), and the early list, which is per library rather than per shard - it can stay one file.

Also worth saying: #2706 (a library per translation) removes part of the reason this was written - a translator edit no longer overwrites another build's library - so the remaining cost this PR addresses is the write volume of a run's own saves. If the maintainers would rather have the smaller change first, "save only after N new pipelines or on exit" is a few lines and gets most of the I/O, but it loses the crash-safety of a periodic save; the shard design keeps both.

I have not ported it here: 200 lines through a file whose save/load changed twice since, without a run to check the library still loads, is not something to push tonight. Happy to do it as the next piece of work if it is wanted.

…only what changed

The host serialises a whole pipeline library and nothing less, so every save
wrote all of it: 926 MB here, 1.7 GB on NVIDIA (`host pipeline library saved
bytes=970507898`), every 20 seconds, and a save holds the library's lock for
as long as it serialises.

The library is kept in 16 files now, `host-pipelines.NN.bin`, one per hash of
the pipeline's name (`shard_of`). The name is a hash already, so a pipeline
always lands in the same shard across runs, and a save serialises and writes
only the shards that gained pipelines since the last one (`Library::dirty`).
A shard is left dirty if its write fails, so the next save writes it. The
early list stays one file and is written when it changed (or on a shutdown
save, which passes `now`).

A cache made before the shards is not thrown away: `host-pipelines.bin` is
still read (never written), and a pipeline found there is stored into its
shard, so a warm run fills the shards and the old file can then be deleted.
The log says so once, and the load line now reports the shards and their
total bytes instead of one file.

Measured before this (retail title, `O:\tmp\victory-cache\pipelines`):

    host pipeline library loaded bytes=970507898 ms=57    (926 MB, 27 160 pipelines)

with a save every 20 s writing the same 926 MB back; a run that made three
pipelines paid it. After the split, the same save writes one shard of that
structure (a sixteenth of the names) plus the early list.

Rebased onto today's `main`: `write_atomically`, `zeroed` (a 1.7 GB
allocation is not infallible), `Shared { loads, state }` (loads take no lock)
and the pre-build's list tagged with the translator hash are all kept; the
shard structure is applied on top, and `pipeline`'s `load` is `Fn` rather
than `FnOnce` because a name may now be tried in its shard and in the
one-file library.

cargo test --release -p victory-gpu (229 tests, one new: names spread over
the shards and keep their shard); clippy clean; both `victory`
configurations build.
@pes7
pes7 force-pushed the perf/pipeline-library-shards branch from 013da61 to 3b1cda6 Compare November 25, 2026 22:39
@pes7

pes7 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Ported onto today's main (f3578e3) as a re-application rather than a rebase, since main reworked both ends of this file meanwhile (write_atomically, zeroed, Shared { loads, state } so loads take no lock, and the pre-build's list tagged with the translator hash). One commit, 3b1cda6.

What it does now:

  • the library is kept in SHARDS = 16 files, host-pipelines.NN.bin, chosen by shard_of(name) (a hash of the pipeline's name, so a pipeline keeps its shard across runs);
  • save serialises and writes only the shards that gained pipelines (Library::dirty), leaves a shard dirty if its write fails, and writes the early list (still one file) when it changed or on a shutdown save;
  • prebuilt/note_prebuilt/pipeline pick the shard;
  • a library made before the split is not thrown away: host-pipelines.bin is still read (never written) and a pipeline found there is stored into its shard, so a warm run fills the shards and the file can then be deleted (one log line says so);
  • pipeline's load is Fn rather than FnOnce, because a name may now be looked for in its shard and then in the one-file library.

Checked: cargo test --release -p victory-gpu 229 pass (one new: names spread over the shards and keep their shard), clippy clean, both victory configurations build.

Note for whoever merges: this and #2706 touch the same file but different parts (this one the library/save/load, #2706 the cache directory), so whichever goes second needs a trivial rebase.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

@pes7 I reran the shard-save regression on the current 3b1cda6. The fix and regression test are in 5236270 on codex2/pr1653-save-race, directly on that head.

The WARP test stores a second named compute PSO and its prebuilt key after the first save has taken its snapshot, just before the file write. It reopens the shard and reads the sidecar after each write.

Same fixture Pending after the first save Second save
3b1cda6 0 stores, prebuilt flag clear Skips the shard. The new entry is missing on disk.
5236270 1 store, prebuilt flag set Writes the shard. Both named PSOs load and both prebuilt keys are present.

The third save skips the clean shard. All 13 pipeline-library tests passed on Windows with WARP, and strict GPU Clippy passed with all features.

The patch restores acknowledgement of just the saved snapshot and keeps overlapping saves from acknowledging or replacing each other's snapshots. If you fold the fix or test into your commit, please retain Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com>.

AI-assisted.

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified 3b1cda6b locally on Windows 11 x64 (git merge-tree against 63bcca43 is conflict-free):

  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings (default and --all-features): clean (9.16s / 4.19s).
  • cargo test -p xtask --locked policy::tests::repository_passes_policy: 1 passed, 0 failed (0.35s).
  • cargo test -p victory-gpu --lib --locked pipeline::library:: -- --test-threads=1: 12 passed, 0 failed on both RTX 5090 (0.61s) and VICTORY_WARP=1 (0.52s).

Two save-concurrency bugs are present in 3b1cda6b:

  1. Lost dirty state during save() (library.rs:428-443): serialise(shared) releases the shard mutex before write_atomically, and on success save() unconditionally sets l.added = 0; l.prebuilt_changed = false;. Any pipeline stored or note_prebuilt key inserted during that I/O window is wiped from the dirty state and skipped by subsequent saves (including save(true)). Please acknowledge only the snapshot count/length (l.added -= added, l.prebuilt_changed = l.prebuilt.len() != prebuilt_count) and include a regression test for concurrent store/prebuilt updates during shard write.
  2. Out-of-order overlapping saves (library.rs:407-445): LAST only guards the throttle timestamp while WRITING inside write_atomically only serializes individual file renames, so a periodic save and save(true) can interleave and let an older snapshot overwrite a newer shard/sidecar. Please serialize the entire snapshot-and-publish transaction across save().

Two non-blocking notes as well: prebuilt/note_prebuilt shard by title key while pipeline() shards by host PSO name (so partial cross-shard failures can leave a prebuilt entry without its PSO on restart), and USED_CHANGED.swap(false) before writing EARLY_FILE drops retries on write failure.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

@fuzeofficlal 5236270, directly on 3b1cda6, addresses both races in your review. It acknowledges only the published snapshot, leaving concurrent stores and prebuilt keys pending, and holds the save lock through publication and acknowledgement so an older snapshot cannot overwrite a newer one. The WARP regression verifies that a PSO and key added during the first write are present after the second save.

Please retain the Git author on cherry-pick or rebase. If folded into another commit, please add Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants