Skip to content

DirectStorage: workers read a file through a handle each - #2904

Open
0xkn0wh3r3 wants to merge 1 commit into
Marvisak:mainfrom
0xkn0wh3r3:hle/dstorage-handle-per-worker
Open

0xkn0wh3r3 wants to merge 1 commit into
Marvisak:mainfrom
0xkn0wh3r3:hle/dstorage-handle-per-worker

Conversation

@0xkn0wh3r3

Copy link
Copy Markdown
Contributor

What

Each DirectStorage worker now reads a file through its own handle instead of the one handle the title's OpenFile made. A worker makes its handle with ReOpenFile the first time it reads that file. If the host refuses, it falls back to the title's handle, logs a warning, and carries on. The handles close with the file object. VICTORY_DSTORAGE_HANDLES=0 reads through the title's handle alone, as before.

Why

The title's handle is opened for synchronous reads, and Windows performs the reads on one such handle one at a time. So the scheduler's workers (one per host core) queued behind each other on every file, however many were free.

Measured with 48 KiB reads at random offsets from 24 threads:

one handle a handle each
title archive, not in the file cache (the fork's RX 7900 XTX machine) 185 MB/s 990 MB/s
2 GiB file in the file cache (this machine, read_bench) 4.2 GB/s 6.6 GB/s

Nothing here depends on the GPU vendor.

Where it comes from

From the avatarmanz/VIctory-7900XTX fork, commit fbc1830. This PR takes only the fork's default handles mode. Left out: its overlapped and unbuffered direct modes, and its xmem lock rework from the same commit.

#234 also targets this serialization, using overlapped reads on one handle, but it bundles that with xmem mapping changes. This PR is only the handle change.

Tested (Windows 11, RTX 5070 Ti)

  • cargo test -p victory-hle: 153 passed, 1 ignored (the benchmark). New test workers_read_a_file_through_handles_of_their_own: six threads read through three handles, and through the title's handle alone, at offsets across a 300,001-byte file and past its end. It checks the bytes and checks that each handle slot a thread used was made.
  • read_bench (ignored): VICTORY_BENCH_FILE=<file> cargo test --release -p victory-hle --lib read_bench -- --ignored --nocapture. The cached-file numbers above come from it.
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.

Not tested

  • Not run with the title on this machine. I couldn't measure the uncached rate here (the benchmark file sat in the file cache), so the 185 → 990 MB/s figure is the fork's.

Every worker read a file through the one handle the title's open made.
That handle is opened for synchronous reads, and the host does the reads
of such a handle one after the other, so the workers queued behind each
other on every file. Each worker now reads through a handle of its own,
made from the title's with ReOpenFile when it first reads that file (the
title's handle if the host makes none).

48 KiB reads at random, 24 threads: 185 MB/s through one handle and
990 MB/s through a handle each, from an archive not in the file cache
(the fork's measurement); from the cache on this machine, 4.2 GB/s and
6.6 GB/s (read_bench, ignored, on any file named by VICTORY_BENCH_FILE).
VICTORY_DSTORAGE_HANDLES=0 reads through the title's handle alone, as
before.

From avatarmanz/VIctory-7900XTX fbc1830 (its handles mode; the
overlapped and unbuffered modes and the xmem lock changes are left out).

Co-authored-by: local <local@localhost>

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Correct — a synchronous handle serializes reads on the host side, so N workers through one handle queue behind each other; a lazy per-worker handle (OnceLock indexed modulo worker count) removes that contention while keeping the title's handle semantics. The =0 escape hatch is there. LGTM.

@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 head 487d01e0 cleanly on origin/main (63bcca43) on Windows 11 x64.

Code Review

  • Using ReOpenFile(..., GENERIC_READ, FILE_SHARE_READ, 0) creates distinct kernel FILE_OBJECTs per worker (unlike DuplicateHandle), eliminating synchronous-I/O lock serialization across worker threads while keeping seek_read offsets independent.
  • Handles open lazily via OnceLock, fall back cleanly to first if ReOpenFile fails (or when VICTORY_DSTORAGE_HANDLES=0), and drop cleanly on Instance<File> release.

Local Verification

  • cargo test --locked -p victory-hle: 153/153 unit tests passed (1 ignored read_bench; includes workers_read_a_file_through_handles_of_their_own), plus fidelityfx (1/1) and no_dll_planting (1/1).
  • cargo clippy --locked -p victory-hle --tests -- -D warnings (default and --features diagnostics) & cargo xtask policy (191 files ok): clean.

Looks clean and ready to merge.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate (a recommendation to look, not an approval). In modules/dstorage.rs, each scheduler worker now reads a file through its own handle, made lazily with ReOpenFile from the title's handle (Files::read_at). If reopening fails it falls back to the title's handle, and VICTORY_DSTORAGE_HANDLES=0 restores the old behaviour. Reads are still positional (seek_read), so sharing a handle never depended on a file position; the change only removes the per-handle serialisation, and the PR measured 185 vs 990 MB/s. The test checks that the bytes and end-of-file results match with and without extra handles. CI check passes.


Generated by Claude Code

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
github-actions Bot pushed a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
…rmediate scratch buffers

Each worker thread in dstorage::work() processes requests sequentially,
but previously constructed a fresh ~10.5 KiB DecompressorOxide and
allocated/zero-filled new Vec<u8> buffers for file sources and
zlib+BCPack intermediate output on every request.

Keep a worker-local WorkerScratch in work() with a boxed
miniz_oxide::inflate::core::DecompressorOxide (reset via init() before
each stream) plus reusable source and intermediate Vec<u8> buffers
(trimmed back after >8 MiB outliers so idle worker RSS stays bounded).
Bit-identical inflate status and output verified across valid,
short-output, bad-header, bad-Adler32, truncated, and error-recovery
cases. Disjoint from Marvisak#2904 and Marvisak#2883; zero new dependencies.

Measured (--release bench_dstorage_worker_scratch, median of 3 runs):
- 256 KiB x400: 48.03 ms -> 27.61 ms (1.74x, -42.5%)
- 32 KiB x2000: 30.21 ms -> 28.79 ms (1.05x, -4.7%)
- 4 KiB x8000:  48.24 ms -> 44.41 ms (1.09x, -7.9%)

Assisted-by: T3 Code (Antigravity, gemini-4-neon)

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified against origin/main (63bcca4):

  • cargo test -p victory-hle --lib modules::dstorage:: -- --test-threads=1: 17/17 passed (1 ignored bench; including workers_read_a_file_through_handles_of_their_own).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Giving each DirectStorage worker its own file handle avoids contention on shared handles while keeping all existing queue/decompression invariants intact.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
@futurepro62

Copy link
Copy Markdown

NVIDIA full-suite run, to go with the reports above by Dukestar69, since none of them names a GPU run. Tested exact head 487d01e (on its base 842b0cb).

Tested

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

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