loader: boot I/O — digest sidecar for the image cache, batched IAT VirtualProtect #3354

Open
github-bot wants to merge 3 commits from github-bot/gh-3354 into main AGit
Owner

@⁠borosf on GitHub · 2026-11-26 20:07 UTC · original
borosf:vibe/loader-boot-io-842ca5 → main · +403 −27 · 3 files · 3 commits · open

What

Two boot-time I/O costs in victory-loader, both measured synthetic-only (no game data):

  1. publish::write_if_changed re-read the whole cached image (~114 MB per exe/DLL, per boot) just to compare bytes. It now answers "already there" from a 32-byte blake3 sidecar written at publish time, plus a metadata length check (so a deleted image still republishes). The full read+compare remains only as the legacy fallback for caches from before sidecars, and it publishes the sidecar for next time.
  2. windows.rs write_slots did two VirtualProtect per IAT slot. It now groups slots by page, asks each page's protection once (VirtualQuery), merges consecutive same-protection pages into runs, and does one unprotect + one restore per run — splitting where protection changes (delay-load tables in .didat), so per-section protections are preserved exactly.

Claimed on #1633 (comment 7202184435).

Measurement (Windows 11 x64, Intel Arc B580 host, synthetic tempfile/VirtualAlloc only)

New tests print the numbers (cargo test -p victory-loader --locked -- --test-threads=1 --nocapture slots_tests publish):

64 MB cache hit: sidecar path 14.07ms, old read+compare 19.58ms   (warm page cache; cold cache pays the NVMe read on the old path, per DLL per boot)
5000 IAT slots: batched 710.3µs, per-slot 8.83ms                    (12.4x)

Correctness tests: sidecar hit does not rewrite (mtime unchanged); deleted image under a matching sidecar republishes; changed bytes republish and move the sidecar; legacy cache without sidecar falls back to read+compare and publishes the sidecar; 500 slots across 3 pages with mixed protections are all written and each page's original protection restored (incl. a PAGE_READONLY middle page forcing a run split).

Verification

  • cargo test -p victory-loader --locked --no-fail-fast — all targets ok (37+1+2+1 passed, 0 failed)
  • cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features — clean
  • fmt gate: rustfmt --config-path rustfmt.toml --edition 2024 --check — windows.rs 0 hunks (baseline 0), publish.rs 1 hunk (baseline 2; only pre-existing hunks remain)
  • cargo xtask policy — 193 files ok

No game data used anywhere; synthetic tests only.

**@⁠borosf** on GitHub · 2026-11-26 20:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/3354) `borosf:vibe/loader-boot-io-842ca5` → `main` · +403 −27 · 3 files · 3 commits · open ## What Two boot-time I/O costs in `victory-loader`, both measured synthetic-only (no game data): 1. **`publish::write_if_changed`** re-read the whole cached image (~114 MB per exe/DLL, per boot) just to compare bytes. It now answers "already there" from a 32-byte blake3 **sidecar** written at publish time, plus a metadata length check (so a deleted image still republishes). The full read+compare remains only as the legacy fallback for caches from before sidecars, and it publishes the sidecar for next time. 2. **`windows.rs write_slots`** did **two `VirtualProtect` per IAT slot**. It now groups slots by page, asks each page's protection once (`VirtualQuery`), merges consecutive same-protection pages into runs, and does one unprotect + one restore per run — splitting where protection changes (delay-load tables in `.didat`), so per-section protections are preserved exactly. Claimed on #1633 (comment 7202184435). ## Measurement (Windows 11 x64, Intel Arc B580 host, synthetic tempfile/VirtualAlloc only) New tests print the numbers (`cargo test -p victory-loader --locked -- --test-threads=1 --nocapture slots_tests publish`): ``` 64 MB cache hit: sidecar path 14.07ms, old read+compare 19.58ms (warm page cache; cold cache pays the NVMe read on the old path, per DLL per boot) 5000 IAT slots: batched 710.3µs, per-slot 8.83ms (12.4x) ``` Correctness tests: sidecar hit does not rewrite (mtime unchanged); deleted image under a matching sidecar republishes; changed bytes republish and move the sidecar; legacy cache without sidecar falls back to read+compare and publishes the sidecar; 500 slots across 3 pages with mixed protections are all written and each page's original protection restored (incl. a `PAGE_READONLY` middle page forcing a run split). ## Verification - `cargo test -p victory-loader --locked --no-fail-fast` — all targets ok (37+1+2+1 passed, 0 failed) - `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features` — clean - fmt gate: `rustfmt --config-path rustfmt.toml --edition 2024 --check` — windows.rs 0 hunks (baseline 0), publish.rs 1 hunk (baseline 2; only pre-existing hunks remain) - `cargo xtask policy` — 193 files ok No game data used anywhere; synthetic tests only.
write_if_changed re-read the whole cached image (~114 MB per exe/DLL per
boot) just to compare bytes; it now answers from a 32-byte blake3 sidecar
written at publish time (full read+compare only as legacy fallback, plus a
length check so a deleted image still republishes). write_slots did two
VirtualProtect per IAT slot; it now unprotects/restores once per run of
pages with uniform protection (per-page VirtualQuery, cached), splitting
runs where the protection changes (delay-load .didat).

Measured (Windows 11, B580 host, synthetic only): 64 MB cache hit 14.1 ms
sidecar vs 19.6 ms read+compare warm (cold-cache gap is the NVMe read);
5,000 IAT slots 710 us batched vs 8.83 ms per-slot (12.4x).

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 20:09:12 +00:00
Author
Owner

@⁠AurumCarlos on GitHub · 2026-11-26 20:18 UTC · original · reviewed

The IAT batching looks right. The sidecar can end up describing a different image than the one on disk, though, and the fast path then trusts it:

  • The image is published by an atomic rename, but the sidecar is written afterwards as a separate, non-atomic step. The module explicitly supports two publishers of the same path at once. If P1 renames A, P2 renames B, and P1's write(sidecar, digest(A)) lands last, the cache holds image B with sidecar A. A later boot whose bytes are A then gets "already there" (digest matches, and length matches if A and B are the same size, which is common for re-prepared images) and runs with B. A crash between the rename and the sidecar write leaves the same mismatch.
  • A cheap fix: remove the old sidecar before renaming, and only trust a sidecar written after the rename, e.g. by also recording the image's length and mtime in it, or by naming the sidecar after the digest so it can't describe a different file.
**@⁠AurumCarlos** on GitHub · 2026-11-26 20:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/3354#pullrequestreview-6080052567) · **reviewed** The IAT batching looks right. The sidecar can end up describing a different image than the one on disk, though, and the fast path then trusts it: - The image is published by an atomic rename, but the sidecar is written afterwards as a separate, non-atomic step. The module explicitly supports two publishers of the same path at once. If P1 renames A, P2 renames B, and P1's `write(sidecar, digest(A))` lands last, the cache holds image B with sidecar A. A later boot whose bytes are A then gets "already there" (digest matches, and length matches if A and B are the same size, which is common for re-prepared images) and runs with B. A crash between the rename and the sidecar write leaves the same mismatch. - A cheap fix: remove the old sidecar before renaming, and only trust a sidecar written after the rename, e.g. by also recording the image's length and mtime in it, or by naming the sidecar after the digest so it can't describe a different file.
The sidecar was written after the atomic rename as a separate step, so two
publishers could leave sidecar(A) beside image(B), or a crash between the
two left the same mismatch — and the fast path would then trust it. Now a
per-image lock file (LockFileEx, released on process death) serializes
publishers, the sidecar is removed BEFORE the rename and written after it,
so a sidecar on disk always describes the file on disk; a crash in between
leaves no sidecar and the next boot falls back to reading the image.
Regression test: 4 threads x 50 alternating publishes, asserting the
sidecar matches the file whenever the file did not change mid-check.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
Author
Owner

@⁠borosf on GitHub · 2026-11-26 20:34 UTC · original

Fixed at 9f8174f — exactly the race you described. Publishers of one path are now serialized with a per-image lock file (LockFileEx on .{name}.lock, released when the handle closes, so a crashed publisher doesn't keep the cache locked), and the sidecar is removed before the atomic rename and written after it. Invariant: a sidecar on disk always describes the file on disk; a crash between rename and sidecar write leaves no sidecar, and the next boot takes the full read+compare fallback. The fast path itself is unchanged (sidecar digest + length match).

Regression test concurrent_publishers_keep_the_sidecar_matching_the_file: 4 threads × 50 alternating publishes; after each, if the file is unchanged across the check, a present sidecar must equal the file's digest. (The first version of that assert raced with publishers mid-rename; it now only asserts when the file is stable across both reads.)

Verification at 9f8174f (Windows 11, B580): cargo test -p victory-loader --locked --no-fail-fast all ok (38 lib tests incl. the race test); workspace clippy clean with -D warnings default and --all-features; cargo xtask policy ok; fmt gate 0 hunks on publish.rs; Cargo.lock unchanged (feature-only addition of windows-sys Win32_Storage_FileSystem/Win32_System_IO). Synthetic tests only; no game data.

**@⁠borosf** on GitHub · 2026-11-26 20:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/3354#issuecomment-7202841565) Fixed at `9f8174f` — exactly the race you described. Publishers of one path are now serialized with a per-image lock file (`LockFileEx` on `.{name}.lock`, released when the handle closes, so a crashed publisher doesn't keep the cache locked), and the sidecar is **removed before the atomic rename and written after it**. Invariant: a sidecar on disk always describes the file on disk; a crash between rename and sidecar write leaves no sidecar, and the next boot takes the full read+compare fallback. The fast path itself is unchanged (sidecar digest + length match). Regression test `concurrent_publishers_keep_the_sidecar_matching_the_file`: 4 threads × 50 alternating publishes; after each, if the file is unchanged across the check, a present sidecar must equal the file's digest. (The first version of that assert raced with publishers mid-rename; it now only asserts when the file is stable across both reads.) Verification at `9f8174f` (Windows 11, B580): `cargo test -p victory-loader --locked --no-fail-fast` all ok (38 lib tests incl. the race test); workspace clippy clean with `-D warnings` default and `--all-features`; `cargo xtask policy` ok; fmt gate 0 hunks on publish.rs; `Cargo.lock` unchanged (feature-only addition of windows-sys `Win32_Storage_FileSystem`/`Win32_System_IO`). Synthetic tests only; no game data.
CI caught a torn sidecar: fs::write truncates then writes, so a concurrent
reader saw an empty digest. The sidecar now goes through its own staging
file + atomic rename. The race test only asserts when the file reads
succeed (a concurrent rename can fail them transiently with AccessDenied).

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
Author
Owner

@⁠borosf on GitHub · 2026-11-26 20:56 UTC · original

CI caught a real one at 9f8174f: the sidecar was written with plain fs::write (truncate-then-write), so a concurrent reader saw a torn/empty digest (left: []), and the race test itself could fail its reads with AccessDenied while another publisher renamed over the file. Fixed at ad0fb99:

  • the sidecar now goes through its own staging file + atomic rename (write_sidecar), so readers only ever see a complete old or new digest;
  • the race test only asserts when all three reads succeed (a concurrent rename can fail them transiently).

Re-verified at ad0fb99 (Windows 11, B580): the race test passes 5/5 runs at --test-threads=8 (the CI condition), full loader suite ok, workspace clippy clean -D warnings default + --all-features, fmt gate at baseline (2 hunks), cargo xtask policy ok. Synthetic tests only; no game data.

**@⁠borosf** on GitHub · 2026-11-26 20:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/3354#issuecomment-7203227223) CI caught a real one at `9f8174f`: the sidecar was written with plain `fs::write` (truncate-then-write), so a concurrent reader saw a torn/empty digest (`left: []`), and the race test itself could fail its reads with AccessDenied while another publisher renamed over the file. Fixed at `ad0fb99`: - the sidecar now goes through its own staging file + atomic rename (`write_sidecar`), so readers only ever see a complete old or new digest; - the race test only asserts when all three reads succeed (a concurrent rename can fail them transiently). Re-verified at `ad0fb99` (Windows 11, B580): the race test passes 5/5 runs at `--test-threads=8` (the CI condition), full loader suite ok, workspace clippy clean `-D warnings` default + `--all-features`, fmt gate at baseline (2 hunks), `cargo xtask policy` ok. Synthetic tests only; no game data.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3354/head:github-bot/gh-3354'
git switch 'github-bot/gh-3354'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3354'
git switch 'github-bot/gh-3354'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3354'
git switch 'github-bot/gh-3354'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3354'
git switch 'main'
git merge --squash 'github-bot/gh-3354'
git switch 'main'
git merge --ff-only 'github-bot/gh-3354'
git switch 'main'
git merge 'github-bot/gh-3354'
git push origin 'main'
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!3354
No description provided.