dstorage: persistent block cache for game files on slow mounts (VICTORY_READ_CACHE) #101

Manually merged
github merged 3 commits from github-bot/gh-101 into main 2026-11-27 10:48:36 +00:00 AGit
Owner

@⁠daidalos4 on GitHub · 2026-11-26 14:51 UTC · original
daidalos4:read-cache → main · +170 −5 · 4 files · 3 commits · merged 2026-11-27 10:46 UTC

What this changes

Adds VICTORY_READ_CACHE=<dir>:<prefix>, an opt-in persistent block cache under the DirectStorage file reads, for game files on a slow network share.

When the game folder lives on a NAS or a remote mount, the first boot can spend many minutes on reads (here, about 1 MB/s), and the run never gets past a black screen. With the cache, every 64 KiB block that DirectStorage reads from a file under <prefix> (checked after resolving links) is kept in <dir> as <name>-<length>.data (sparse) plus a .map with one byte per block. The next run reads those blocks from local disk, so each boot gets further than the last.

  • Off unless the variable is set; only files under <prefix> are cached.
  • A missing block is fetched with the missing blocks after it that the same read covers (up to 64), in one read.
  • A run adds at most 12 GB; past that, reads go straight to the source. Nothing is evicted: delete <dir> to reclaim the space.
  • macOS x86_64 only (#[cfg]), like the rest of the macOS DirectStorage path. Documented in docs/native-host.md.

How it was tested

  • cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory: victory-hle 227 passed, including the new read_cache::tests::reads_match_the_source_cold_and_warm (cold and warm passes against the source bytes). The probe-based loader tests fail on this machine before and after the change (lld: unable to find library -lkernel32, the cross toolchain isn't installed here).
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings: not run (no Windows target installed). The new code is macOS-only; clippy on x86_64-apple-darwin for victory-hle reports nothing in the changed files.
  • Ran the game with the folder on a slow share: boots that stopped at a black screen now reach the start screen and the setup menus; the second boot is much faster.

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none: this is host-side file I/O)
  • New diagnostics are behind a VICTORY_* variable and documented
**@⁠daidalos4** on GitHub · 2026-11-26 14:51 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/101) `daidalos4:read-cache` → `main` · +170 −5 · 4 files · 3 commits · merged 2026-11-27 10:46 UTC ## What this changes Adds `VICTORY_READ_CACHE=<dir>:<prefix>`, an opt-in persistent block cache under the DirectStorage file reads, for game files on a slow network share. When the game folder lives on a NAS or a remote mount, the first boot can spend many minutes on reads (here, about 1 MB/s), and the run never gets past a black screen. With the cache, every 64 KiB block that DirectStorage reads from a file under `<prefix>` (checked after resolving links) is kept in `<dir>` as `<name>-<length>.data` (sparse) plus a `.map` with one byte per block. The next run reads those blocks from local disk, so each boot gets further than the last. - Off unless the variable is set; only files under `<prefix>` are cached. - A missing block is fetched with the missing blocks after it that the same read covers (up to 64), in one read. - A run adds at most 12 GB; past that, reads go straight to the source. Nothing is evicted: delete `<dir>` to reclaim the space. - macOS x86_64 only (`#[cfg]`), like the rest of the macOS DirectStorage path. Documented in docs/native-host.md. ## How it was tested - [x] `cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory`: victory-hle 227 passed, including the new `read_cache::tests::reads_match_the_source_cold_and_warm` (cold and warm passes against the source bytes). The probe-based loader tests fail on this machine before and after the change (`lld: unable to find library -lkernel32`, the cross toolchain isn't installed here). - [ ] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings`: not run (no Windows target installed). The new code is macOS-only; clippy on `x86_64-apple-darwin` for victory-hle reports nothing in the changed files. - [x] Ran the game with the folder on a slow share: boots that stopped at a black screen now reach the start screen and the setup menus; the second boot is much faster. ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none: this is host-side file I/O) - [x] New diagnostics are behind a `VICTORY_*` variable and documented
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:54:59 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 15:55 UTC · original · requested changes

Opt-in and local, so the idea is fine, but before merging:

  • The cache key is -. A patched or replaced game file of the same size would silently serve stale blocks. Include the source mtime in the key or in a header checked on open.
  • The data write and the .map write are not ordered or synced: after a crash or power loss a block can be marked present with no data behind it. Write the map byte after data sync, or accept that and say so in the docs.
  • No CI has run and the branch is behind main; please merge main.
    This is tangential to the story-load crash work, so it is low priority on our side.
**@⁠LakshanSivakumar** on GitHub · 2026-11-26 15:55 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/101#pullrequestreview-6077114580) · **requested changes** Opt-in and local, so the idea is fine, but before merging: - The cache key is <name>-<length>. A patched or replaced game file of the same size would silently serve stale blocks. Include the source mtime in the key or in a header checked on open. - The data write and the .map write are not ordered or synced: after a crash or power loss a block can be marked present with no data behind it. Write the map byte after data sync, or accept that and say so in the docs. - No CI has run and the branch is behind main; please merge main. This is tangential to the story-load crash work, so it is low priority on our side.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 05:41 UTC · original

This needs a rebase on main (dstorage.rs conflicts) and the two review points: the cache key should include the file's mtime so a patched file of the same size is not served stale, and the data and map writes need an order (data, sync, then map) so a crash cannot leave a block marked present with nothing behind it. Low priority, but welcome once those are in.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 05:41 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/101#issuecomment-7210851587) This needs a rebase on main (dstorage.rs conflicts) and the two review points: the cache key should include the file's mtime so a patched file of the same size is not served stale, and the data and map writes need an order (data, sync, then map) so a crash cannot leave a block marked present with nothing behind it. Low priority, but welcome once those are in.
Author
Owner

@⁠daidalos4 on GitHub · 2026-11-27 10:12 UTC · original

Merged main and addressed both points: the cache key is now <name>-<length>-<mtime>, so a patched file of the same size gets a fresh entry, and each fetched run of blocks is sync_data'd before its map bytes are written, so a crash can lose blocks but never mark one present with nothing behind it. Docs updated. read_cache::tests passes on x86_64-apple-darwin.

**@⁠daidalos4** on GitHub · 2026-11-27 10:12 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/101#issuecomment-7215604834) Merged main and addressed both points: the cache key is now `<name>-<length>-<mtime>`, so a patched file of the same size gets a fresh entry, and each fetched run of blocks is `sync_data`'d before its map bytes are written, so a crash can lose blocks but never mark one present with nothing behind it. Docs updated. `read_cache::tests` passes on x86_64-apple-darwin.
github manually merged commit abb893120e into main 2026-11-27 10:48:36 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 10:46 UTC · original

Thank you. Both points are in: the key has the mtime, and the data is synced before the map byte is written. CI is green on the merge. I have not run it; I rely on your run on the slow share.

Two small notes, neither blocks it: every missing run now ends in a sync, so please say if the first boot felt slower on a fast disk; and clippy for the Windows target was not run on your side, but CI did it. I will review it again and merge.

Merged.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 10:46 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/101#issuecomment-7216283618) Thank you. Both points are in: the key has the mtime, and the data is synced before the map byte is written. CI is green on the merge. I have not run it; I rely on your run on the slow share. Two small notes, neither blocks it: every missing run now ends in a sync, so please say if the first boot felt slower on a fast disk; and clippy for the Windows target was not run on your side, but CI did it. I will review it again and merge. Merged.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 10:46 UTC · original

I ran these five together on my Mac before merging (#101, #267, #288, #289, #291 on main, M5 Pro 48 GB, Series S, warm cache, three runs): two runs stayed in the world until the time limit (139 s and 202 s there, about 2 fps, no failed command buffer, no fault); one ended at the start of the world with a title fault I had not seen before (first chance at image+0x4fafef8, a read of 0x30000065c), with no GPU error before it. I could not tie that one to any of the five. If you see it, please say so.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 10:46 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/101#issuecomment-7216287534) I ran these five together on my Mac before merging (#101, #267, #288, #289, #291 on main, M5 Pro 48 GB, Series S, warm cache, three runs): two runs stayed in the world until the time limit (139 s and 202 s there, about 2 fps, no failed command buffer, no fault); one ended at the start of the world with a title fault I had not seen before (first chance at image+0x4fafef8, a read of 0x30000065c), with no GPU error before it. I could not tie that one to any of the five. If you see it, please say so.
This discussion has been locked. Commenting is limited to contributors.
No description provided.