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

Merged
daidalos4 merged 3 commits from upstream-pr/101 into main 2026-11-27 10:46:04 +00:00
daidalos4 commented 2026-11-26 14:51:49 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by daidalos4 on 2026-11-26T14:51:49Z.


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
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5512218081 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/101). Originally posted by [daidalos4](https://github.com/daidalos4) on 2026-11-26T14:51:49Z. --- ## 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
LakshanSivakumar commented 2026-11-26 15:55:43 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T15:55:43Z.

Review: CHANGES_REQUESTED


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.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6077114580 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/101#pullrequestreview-6077114580). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T15:55:43Z. **Review:** CHANGES_REQUESTED --- 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.
LakshanSivakumar commented 2026-11-27 05:41:15 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T05:41:15Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7210851587 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/101#issuecomment-7210851587). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T05:41:15Z. --- 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.
daidalos4 commented 2026-11-27 10:12:30 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by daidalos4 on 2026-11-27T10:12:30Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7215604834 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/101#issuecomment-7215604834). Originally posted by [daidalos4](https://github.com/daidalos4) on 2026-11-27T10:12:30Z. --- 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.
LakshanSivakumar commented 2026-11-27 10:46:13 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T10:46:13Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7216283618 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/101#issuecomment-7216283618). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T10:46:13Z. --- 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 commented 2026-11-27 10:46:25 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T10:46:25Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7216287534 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/101#issuecomment-7216287534). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T10:46:25Z. --- 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.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
LakshanSivakumar/Native-Victory-macOS!101
No description provided.