[pr-fix][startup][shaders] Checkpoint early pipeline keys independently #235

Open
andrewpozdnakov7-jpg wants to merge 2 commits from andrewpozdnakov7-jpg/Darner:port/1989-checkpoint-early-pipeline-history into main
First-time contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

The early list can stay unsaved when a run first uses keys and the native pipeline library never saves. Record new first-use keys independently. Coalesce updates on a background thread, merge the existing snapshot under a cache-local lease, and keep failed checkpoints dirty for retry.

Restores locally saved VIctory-NVIDIA work from December 1, 2026. Previous review: CodeFloe #390. Adapted by andrewpozdnakov7-jpg (GitHub profile).

How it was tested

  • Tested revision: 17f43b3 on f3840d7.
  • Commands and results: A Windows Rust fixture exercised the current observation, checkpoint, and atomic write paths. Main left the list unchanged without a native save. The patched worker kept the existing file while a competing lease was held, then retained all four keys after retry, including the other writer entry. cargo check --locked -p darner-gpu --lib --tests, xtask policy, and git diff --check passed. git merge-tree verified a clean merge with #232.
  • Changes a default: no.
  • Follow-up 2668ae5 keeps library-coupled checkpointing by default and enables independent saves with DARNER_EARLY_CHECKPOINT=1. With the new mode on, the exit checkpoint retries a busy cache lease for up to 200 ms. A real Windows sharing-mode fixture verified lease release and merged keys, and precedence cases verified that only 1 enables the mode. The focused GPU crate type check and asset policy passed.
  • Gates run on that revision: none of the three workspace-wide gates.
  • Coverage gaps: Synthetic shared-cache paths on Windows. No gameplay. superhero-520 tested mixed versions. Two patched writers retained both keys. The older writer can overwrite a newer key when both versions write into the same directory. A later new observation on the patched process restored the key in the fixture.

Checklist

  • I ran this change in the game.
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #232 also changes pipeline/library.rs and can merge independently. #147 periodically saves the native library during preload; this handles first use during a run.
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: <!-- optional --> ## What it changes The early list can stay unsaved when a run first uses keys and the native pipeline library never saves. Record new first-use keys independently. Coalesce updates on a background thread, merge the existing snapshot under a cache-local lease, and keep failed checkpoints dirty for retry. Restores locally saved VIctory-NVIDIA work from December 1, 2026. Previous review: [CodeFloe #390](https://codefloe.com/TrustNo1/Darner/pulls/390). Adapted by [andrewpozdnakov7-jpg](https://git.gtahub.st/andrewpozdnakov7-jpg) ([GitHub profile](https://github.com/andrewpozdnakov7-jpg)). ## How it was tested - Tested revision: 17f43b3 on f3840d7. - Commands and results: A Windows Rust fixture exercised the current observation, checkpoint, and atomic write paths. Main left the list unchanged without a native save. The patched worker kept the existing file while a competing lease was held, then retained all four keys after retry, including the other writer entry. cargo check --locked -p darner-gpu --lib --tests, xtask policy, and git diff --check passed. git merge-tree verified a clean merge with #232. - Changes a default: no. - Follow-up 2668ae5 keeps library-coupled checkpointing by default and enables independent saves with `DARNER_EARLY_CHECKPOINT=1`. With the new mode on, the exit checkpoint retries a busy cache lease for up to 200 ms. A real Windows sharing-mode fixture verified lease release and merged keys, and precedence cases verified that only `1` enables the mode. The focused GPU crate type check and asset policy passed. - Gates run on that revision: none of the three workspace-wide gates. - Coverage gaps: Synthetic shared-cache paths on Windows. No gameplay. [superhero-520 tested mixed versions](https://git.gtahub.st/TrustNo1/Darner/pulls/235#issuecomment-861481). Two patched writers retained both keys. The older writer can overwrite a newer key when both versions write into the same directory. A later new observation on the patched process restored the key in the fixture. ## Checklist - [ ] I ran this change in the game. - [x] This PR contains one change based on main. - [x] The diff follows docs/asset-policy.md. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #232 also changes pipeline/library.rs and can merge independently. #147 periodically saves the native library during preload; this handles first use during a run. - Replaces or overlaps: none found
Adapt locally saved VIctory-NVIDIA work from December 1, 2026 (7dc512d). Save first-use pipeline keys on a coalesced checkpoint and merge them under a cache-local file lock before atomic publication.

https://git.gtahub.st/andrewpozdnakov7-jpg
https://github.com/andrewpozdnakov7-jpg

Co-authored-by: NotADonky43 <[email protected]>
First-time contributor

I tested the mixed-version writer case on Windows at 17f43b3de30d9800a3f30d167b1c0c13336c3a45 and its parent f3840d73d3504c30218debfbc8795b9225447317. Two processes loaded the same seed P before either saved. The harness compiled each revision’s observation, save, atomic-write and reader functions, used synthetic keys and supplied no GPU library.

  • Candidate saves C, then parent saves M: fresh readers see P,M. The parent overwrites C.
  • Parent saves M, then candidate saves C: fresh readers see P,M,C.
  • Two candidate writers: both keys survive in either reader.

After the overwrite, a checkpoint in the still-running candidate with no new keys left the file at P,M. Recording D and checkpointing restored C and preserved M, producing P,M,C,D. This matters when two versions write to the same cache directory. Normal play guards against a second run from the same home. No gameplay or performance measurement was involved.

I tested the mixed-version writer case on Windows at `17f43b3de30d9800a3f30d167b1c0c13336c3a45` and its parent `f3840d73d3504c30218debfbc8795b9225447317`. Two processes loaded the same seed `P` before either saved. The harness compiled each revision’s observation, save, atomic-write and reader functions, used synthetic keys and supplied no GPU library. - Candidate saves `C`, then parent saves `M`: fresh readers see `P,M`. The parent overwrites `C`. - Parent saves `M`, then candidate saves `C`: fresh readers see `P,M,C`. - Two candidate writers: both keys survive in either reader. After the overwrite, a checkpoint in the still-running candidate with no new keys left the file at `P,M`. Recording `D` and checkpointing restored `C` and preserved `M`, producing `P,M,C,D`. This matters when two versions write to the same cache directory. Normal `play` guards against a second run from the same home. No gameplay or performance measurement was involved.
Collaborator

Code-read review of 17f43b3. I did not build or run it, and I looked only at this diff, not at other open PRs.

The problem is real. On main, early-pipelines.txt is written only inside save_inner, after a library save and only if USED_CHANGED is set or bytes were written. A run that loads every pipeline from the library loses its first-use keys if it crashes. The handshake between changed() and the worker (dirty is set before RUNNING is taken, and the worker re-checks dirty under RUNNING) looks right to me. Three things I would look at before merging:

1. Title threads wait on a full clone of the USED set, about once a second during the opening.
checkpoint() calls HISTORY.flush(dir, || super::used().iter().flatten().cloned().collect()) (early_history.rs). The USED guard is held while up to EARLY_MAX (100,000) strings are cloned. Title threads take the same mutex in note_used, wanted_early and listed (make.rs:733, 765, 928, 1098, 1114). While new keys keep arriving, the worker repeats this every second, and each round also re-reads, sorts and rewrites the whole file. Main did the clone once per library save. The test comment on this PR says no gameplay or performance measurement was involved, so nothing shows this is cheap. Suggestion: keep a small "new since the last checkpoint" Vec<String> (pushed in note_used, drained by the worker) and merge only that into the file. Otherwise a frame-time comparison on a heavy scene would settle it.

2. The exit checkpoint can lose keys that main would have written.
save_inner(true) now calls early_history::checkpoint() (library.rs:1348). The lock file is opened with share_mode(0), so if another process holds early-pipelines.lock at that moment the open fails at once. checkpoint() logs at debug level and returns false, and the process ends with the keys unsaved. Main wrote the file at exit whenever a new key had been added. The window is small, but this is the one write that must not be skipped: please retry for a short time (a few tries over a second or so) in the final call, as writer(dir, wait) does for the library.

3. New behaviour, no switch.
It adds a thread, a new persistent file (early-pipelines.lock) and union-merge semantics for the early list. Upstream's habit for new behaviour is a default-off switch, or the maintainer's explicit OK. Please say which in the body. superhero-520's mixed-version test (an older build overwriting a newer build's keys) is worth stating in the body.

Smaller points: the early count in the "host pipeline library saved" log is now the in-memory count, not what was written; leftover early-pipelines.txt.tmp-<pid>-<n> files after a kill are not swept (remove_leftovers only handles host-pipelines.tmp*), and writes are now far more frequent; early_history.rs is not a single-word file name and its //! line does not give the why with evidence (AGENTS.md conventions).

The unit test covers the merge and the held-lease failure well. Nothing covers the worker thread or the cost.

This comment was written with AI assistance (Claude Code).

Code-read review of 17f43b3. I did not build or run it, and I looked only at this diff, not at other open PRs. The problem is real. On main, `early-pipelines.txt` is written only inside `save_inner`, after a library save and only if `USED_CHANGED` is set or bytes were written. A run that loads every pipeline from the library loses its first-use keys if it crashes. The handshake between `changed()` and the worker (dirty is set before `RUNNING` is taken, and the worker re-checks dirty under `RUNNING`) looks right to me. Three things I would look at before merging: **1. Title threads wait on a full clone of the `USED` set, about once a second during the opening.** `checkpoint()` calls `HISTORY.flush(dir, || super::used().iter().flatten().cloned().collect())` (`early_history.rs`). The `USED` guard is held while up to `EARLY_MAX` (100,000) strings are cloned. Title threads take the same mutex in `note_used`, `wanted_early` and `listed` (`make.rs:733`, `765`, `928`, `1098`, `1114`). While new keys keep arriving, the worker repeats this every second, and each round also re-reads, sorts and rewrites the whole file. Main did the clone once per library save. The test comment on this PR says no gameplay or performance measurement was involved, so nothing shows this is cheap. Suggestion: keep a small "new since the last checkpoint" `Vec<String>` (pushed in `note_used`, drained by the worker) and merge only that into the file. Otherwise a frame-time comparison on a heavy scene would settle it. **2. The exit checkpoint can lose keys that main would have written.** `save_inner(true)` now calls `early_history::checkpoint()` (`library.rs:1348`). The lock file is opened with `share_mode(0)`, so if another process holds `early-pipelines.lock` at that moment the open fails at once. `checkpoint()` logs at debug level and returns false, and the process ends with the keys unsaved. Main wrote the file at exit whenever a new key had been added. The window is small, but this is the one write that must not be skipped: please retry for a short time (a few tries over a second or so) in the final call, as `writer(dir, wait)` does for the library. **3. New behaviour, no switch.** It adds a thread, a new persistent file (`early-pipelines.lock`) and union-merge semantics for the early list. Upstream's habit for new behaviour is a default-off switch, or the maintainer's explicit OK. Please say which in the body. superhero-520's mixed-version test (an older build overwriting a newer build's keys) is worth stating in the body. Smaller points: the `early` count in the "host pipeline library saved" log is now the in-memory count, not what was written; leftover `early-pipelines.txt.tmp-<pid>-<n>` files after a kill are not swept (`remove_leftovers` only handles `host-pipelines.tmp*`), and writes are now far more frequent; `early_history.rs` is not a single-word file name and its `//!` line does not give the why with evidence (AGENTS.md conventions). The unit test covers the merge and the held-lease failure well. Nothing covers the worker thread or the cost. This comment was written with AI assistance (Claude Code).
First-time contributor

I measured the USED lock interval discussed above at 17f43b3de30d9800a3f30d167b1c0c13336c3a45. On a Ryzen 7 5800H with Windows and optimized Rust 1.98.1, a standalone probe used the PR's Mutex<Option<HashSet<String>>> and its clone into Vec<String>. Timing started after lock acquisition and ended immediately after dropping the guard. Clone destruction, file I/O, and the subsequent BTreeSet merge were outside the timed interval.

Synthetic 32-byte keys Median hold p95 hold
1,019 0.10 ms 0.18 ms
4,167 0.40 ms 0.61 ms
100,000 13.48 ms 15.47 ms

The smaller counts match the lengths of two local early-pipeline indexes. The largest is the code's cap. Each row pools 180 observations across three processes after warm-up, with sizes interleaved. A separate workspace test was running on the host. These figures measure the copy while the lock is held. Gameplay frame times and contention experienced by a drawing thread remain unmeasured.

I measured the `USED` lock interval discussed above at `17f43b3de30d9800a3f30d167b1c0c13336c3a45`. On a Ryzen 7 5800H with Windows and optimized Rust 1.98.1, a standalone probe used the PR's `Mutex<Option<HashSet<String>>>` and its clone into `Vec<String>`. Timing started after lock acquisition and ended immediately after dropping the guard. Clone destruction, file I/O, and the subsequent `BTreeSet` merge were outside the timed interval. | Synthetic 32-byte keys | Median hold | p95 hold | |---:|---:|---:| | 1,019 | 0.10 ms | 0.18 ms | | 4,167 | 0.40 ms | 0.61 ms | | 100,000 | 13.48 ms | 15.47 ms | The smaller counts match the lengths of two local early-pipeline indexes. The largest is the code's cap. Each row pools 180 observations across three processes after warm-up, with sizes interleaved. A separate workspace test was running on the host. These figures measure the copy while the lock is held. Gameplay frame times and contention experienced by a drawing thread remain unmeasured.
Author
First-time contributor

Thanks for the review and the lock-hold measurements. The follow-up in 2668ae5 keeps the current library-coupled save by default. Independent checkpoints now require DARNER_EARLY_CHECKPOINT=1, so the background worker and its set clone are limited to that mode.

With that mode enabled, the exit checkpoint waits up to 200 ms on a sharing violation and warns if the lease stays busy. A Windows fixture held and released the real lease, then checked that both the existing and newly observed keys survived. The focused GPU crate type check and asset policy passed.

The synthetic timings are useful at the largest key counts. I am keeping them as lock-hold measurements in this review, since gameplay impact was outside the probe.

If folding the follow-up into another commit, please retain the Git author or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.

Thanks for the review and the lock-hold measurements. The follow-up in `2668ae5` keeps the current library-coupled save by default. Independent checkpoints now require `DARNER_EARLY_CHECKPOINT=1`, so the background worker and its set clone are limited to that mode. With that mode enabled, the exit checkpoint waits up to 200 ms on a sharing violation and warns if the lease stays busy. A Windows fixture held and released the real lease, then checked that both the existing and newly observed keys survived. The focused GPU crate type check and asset policy passed. The synthetic timings are useful at the largest key counts. I am keeping them as lock-hold measurements in this review, since gameplay impact was outside the probe. If folding the follow-up into another commit, please retain the Git author or add `Co-authored-by: andrewpozdnakov7-jpg <296776509+[email protected]>`.
This pull request has changes conflicting with the target branch.
  • README.md
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'port/1989-checkpoint-early-pipeline-history:andrewpozdnakov7-jpg-port/1989-checkpoint-early-pipeline-history'
git switch 'andrewpozdnakov7-jpg-port/1989-checkpoint-early-pipeline-history'

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 'andrewpozdnakov7-jpg-port/1989-checkpoint-early-pipeline-history'
git switch 'andrewpozdnakov7-jpg-port/1989-checkpoint-early-pipeline-history'
git rebase 'main'
git switch 'main'
git merge --ff-only 'andrewpozdnakov7-jpg-port/1989-checkpoint-early-pipeline-history'
git switch 'andrewpozdnakov7-jpg-port/1989-checkpoint-early-pipeline-history'
git rebase 'main'
git switch 'main'
git merge --no-ff 'andrewpozdnakov7-jpg-port/1989-checkpoint-early-pipeline-history'
git switch 'main'
git merge --squash 'andrewpozdnakov7-jpg-port/1989-checkpoint-early-pipeline-history'
git switch 'main'
git merge --ff-only 'andrewpozdnakov7-jpg-port/1989-checkpoint-early-pipeline-history'
git switch 'main'
git merge 'andrewpozdnakov7-jpg-port/1989-checkpoint-early-pipeline-history'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
3 participants
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
TrustNo1/Darner!235
No description provided.