Skip to content

DirectStorage: keep unpacked file requests on disk (XCL_DSTORAGE_CACHE) - #2864

Draft
ozimchuk wants to merge 1 commit into
xcl-dev:mainfrom
ozimchuk:dstorage-unpack-cache
Draft

ozimchuk wants to merge 1 commit into
xcl-dev:mainfrom
ozimchuk:dstorage-unpack-cache

Conversation

@ozimchuk

@ozimchuk ozimchuk commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

A title streams the same compressed data again and again, within one run and in every later one, and XCL decodes it on the CPU each time (zlib, then BCPack). With XCL_DSTORAGE_CACHE=<folder>, XCL keeps the unpacked bytes of each decoded file request in that folder and reads them back the next time the same request comes. Off by default. Part of #2863.

A request counts as the same when it names the same file (path, size, modification time), the same offset and source size, and the same decoding (zlib and BCPack bits, intermediate and destination sizes). Requests from memory sources and plain copies are not kept. A request on a closed file fails with E_FILE_HANDLE as before, kept bytes or not.

What is in it

  • crates/xcl/src/hle/dstorage/unpack_cache.rs (new):
    • One store per source file: an append-only data file and an index of fixed 40-byte records, each with the entry's Adler-32.
    • On open, a record without all its bytes, or a record cut short, is dropped. On read, bytes that do not match their checksum are decoded again (counted as bad_entries).
    • A writer thread does the writes (at most 256 MiB queued), so a request never waits for the disk.
    • No eviction: entries stop at XCL_DSTORAGE_CACHE_MB (default 32,768).
    • Unit tests: found again in a later run and only for the same request; a changed source file and damaged bytes are not used; cut index and lost data are ignored; full cache; puts from 8 threads; Adler-32.
  • crates/xcl/src/hle/dstorage.rs: execute asks the cache before reading and decoding a file request, and gives it the result after. FileInner keeps the file's store. A closed file gets no cache entry (test: a_closed_file_is_not_answered_from_the_unpack_cache). report.json → dstorage.unpack_cache counts hits, stored entries, entries not stored (full or busy) and bad entries.
  • README.md: the three variables.

598 changed lines, about 225 of them unit tests. The cache cannot be split from its use in execute and still be testable in a run.

Tested

  • NVIDIA (32-lane waves), driver 595.99.02, Ryzen 5 9600X. Linux (Ubuntu 24.04) with Wine 11.21 + vkd3d-proton (dxil-spirv 694cc67), not Windows. Release build for x86_64-pc-windows-gnu, Rust 1.100.0.

  • cargo test -p xcl --lib --release -- --test-threads=4 (the test binary under Wine, with the relaxed vkd3d-proton from Linux (Wine + vkd3d-proton): a retail game reaches what main reaches on Windows; the obstacle is replaced vtables — proposal: proxies #175) on aea330e1, this PR's first head (before the Close fix): 1,312 passed, 12 failed, 27 ignored. 25 SKIPPED, mostly no wave64.

    • Ten of the failures also fail on main 8b957bec here: 8 test-data and validator cases in gpu::rdna2, gpu::dxil, gpu::recomp, plus bcpack::tests::fixtures_decode_to_their_lanes and device_x::tests::gpu_promoted_uav_clear_values_pass_create_committed_resource.
    • The other two, list_x::tests::mid_list_write64_wait64_split_avoids_cycle_and_reuses_spares and vram::tests::d32s8_targets_take_their_own_size_of_video_memory, pass when run alone.
    • main's own run hung twice in vram GPU tests after a GPU fault (1,305 passed and the same 10 failed before that). So I have no complete main count to compare with.
    • On this head (723f5a19): hle::dstorage:: 38 passed, 0 failed (unpack_cache 6 of them). The Close test fails without its fix.
  • A retail title, run on main b49be18 plus this commit, because 8b957bec's startup shader preparation (xcl run: prepare shaders in the game window before title execution #2555) takes longer than a run here. The DirectStorage code is identical between the two. A local counter timed the decode stages. Each run ends at ~95 s in main's own crash (XG CopyFromSubresource: retail title passes one argument more → memcpy AV in copy_from; then null write at game+0x23cf683 at end of loading #662; the same with the cache off).

    at 80 s cache off empty cache (first run) kept from an earlier run same, XCL_DSTORAGE_CACHE_VERIFY=0
    Series S: zlib + BCPack decode time 10.4 s (3.5 GB out) 9.3 s 0.1 s 0.1 s
    Series S: all DirectStorage request time 20.2 s 20.5 s 9.3 s 8.1 s
    Series S: answered from the cache – 4,606 (0.3 GB) 63,005 (3.8 GB) 64,786 (3.8 GB)
    Series X: zlib + BCPack decode time 13.7 s (4.4 GB out) 8.0 s (at 74 s) 0.3 s 0.3 s
    Series X: all DirectStorage request time 28.8 s 29.4 s (at 84 s) 15.9 s 13.3 s
    Series X: answered from the cache – 2,298 (0.2 GB, at 74 s) 84,294 (4.9 GB) 83,202 (4.8 GB)

    One run per cell. "Request time" sums the time requests spent on the queue's worker thread (read, decode, cache, copy). The first run costs about the same as no cache, since the writes go to a thread of their own. The read check costs 1.2–2.6 s per 3.8–4.9 GB read back. No entry failed it.
    Frames presented were about the same in all runs (title screens at 30 fps). No run had a device removal before the crash. Cache folder after one run: about 4–5 GB per console mode.

Regression run

  • Pending: blocks from main 8b957bec and this head will follow (a Windows run was asked for in the comments; on Linux, the shader preparation in 8b957bec has to finish first).
  • On Linux/Wine, the same script on main b49be18, with and without this commit and with the cache empty and full: four clean 60 s runs (1,573–1,612 frames, 60 presents per 2 s, no removals or crashes; Wine reports no commit). The full cache answered all 6,507 requests.

Not tested

  • Windows, AMD, and a run past the world load (main crashes before it here).
  • XCL_DSTORAGE_PARALLEL_DECOMPRESS together with the cache: its requests do not use the cache.
  • A cache folder on a slow disk, and the cache when full over a long session.

AI use

  • Claude Code, Claude Opus 5.5 wrote the code, the tests and this description, and ran the tests and the runs.

🤖 Generated with Claude Code

https://claude.ai/code/session_01H6JBZXaFw99FcnmUV1mPWj

AI-assisted.

@razormontana

Copy link
Copy Markdown
Contributor

Native Windows SATA validation for #2864

Clean PR head aea330e1b892a8a737fd247af2e3606406cfa64e on 8b957bec6f82495aec940944c6bf47faa02db590. The six cache fixtures passed, the opt-in file-request fallback wrote the expected synthetic payload on SATA, and both GPU cases reported in the Wine run passed together on this machine.

Tested

  • Windows 11 Pro 23H2 (22631.5335), Core i7-10700K, GeForce RTX 3070 8 GB, NVIDIA 617.42 (Windows driver 32.0.16.1742), Rust 1.100.0 x86_64-pc-windows-msvc and debug test binaries. Test files and the opt-in cache were on D: (SATA). XCL_ADAPTER=3070, XCL_NETWORK=0, and external sample variables unset.
  • cargo test --offline --locked -j 1 -p xcl --lib --no-run built successfully.
  • cargo test --offline --locked -j 1 -p xcl --lib hle::dstorage:: -- --test-threads=1 --nocapture reported 37 passed, 0 failed, 0 ignored, and 0 explicit skips with the cache unset and again with XCL_DSTORAGE_CACHE pointing to a fresh D: folder, XCL_DSTORAGE_CACHE_MB=64, and XCL_DSTORAGE_CACHE_VERIFY=1. The opt-in run's existing budget-fallback fixture delivered its 48 KiB synthetic zlib output. The resulting data file matched the expected payload hash, and the index held an 8-byte header and one 40-byte record.
  • cargo test --offline --locked -j 1 -p xcl --lib hle::dstorage::unpack_cache::tests:: -- --test-threads=4 reported 6 passed, 0 failed, 0 ignored, and 0 explicit skips in each of two focused runs. This covers reopening a store, source-size and request-key checks, checksum failures, truncated index and data recovery, a full cache, and all 400 writes from eight producers.
  • The two GPU fixtures ran together three times using the command below. Each run reported 2 passed, 0 failed, 0 ignored, and 0 explicit skips. The D32S8 fixture logged all 14 allocations and the command-list fixture logged both splits.
cargo test --offline --locked -j 1 -p xcl --lib -- --exact hle::d3d12x::list_x::tests::mid_list_write64_wait64_split_avoids_cycle_and_reuses_spares hle::d3d12x::vram::tests::d32s8_targets_take_their_own_size_of_video_memory --test-threads=4 --nocapture

Review observation

By inspection, file_close drops the source handle. A request with a memoized store and a matching cache hit could skip the live-handle check in execute. The file-read path returns E_FILE_HANDLE once the handle is closed. The committed fixtures leave this difference in Close behavior unverified. Close, cache lookup and source read

Not tested

Retail playback or smoke, sustained SATA throughput, warm cache hits through the file-request queue, Windows release builds, and the full-workspace or full-library scheduling conditions from the Wine report.

AI use

Codex GPT-6.2 inspected the source and fixtures, ran the tests, checked the stored synthetic output, and drafted this report.

A title streams the same compressed data again and again, in one run and in every
later one, and XCL decodes it on the CPU each time (zlib, then BCPack). With
XCL_DSTORAGE_CACHE=<folder> the unpacked bytes of a decoded file request are kept in
the folder and read back the next time the same request comes: same file (path, size,
modification time), offset, source size and decoding. One store per source file (an
append-only data file and an index of fixed records, each with the entry's Adler-32);
a record without all its bytes, a cut record or bytes that do not match their checksum
are not used. New entries are written by a thread of their own (at most 256 MiB
queued), so a request never waits for the disk. No eviction: entries stop once the
folder holds XCL_DSTORAGE_CACHE_MB (default 32,768). report.json counts hits and
stored entries. XCL_DSTORAGE_CACHE_VERIFY=0 skips the check on reads.

Assisted-by: Claude Code (Claude Opus 5.5)
@ozimchuk
ozimchuk force-pushed the dstorage-unpack-cache branch from aea330e to 723f5a1 Compare November 26, 2026 20:27
@ozimchuk

Copy link
Copy Markdown
Contributor Author

@razormontana thanks for the Windows run and the review.

You're right about Close. Once the store was set up, a request on a closed file could still be answered from the cache, where without it the request fails with E_FILE_HANDLE. In 723f5a19, unpack_cache_entry returns nothing for a closed file, so the request goes the usual way and fails as before. New test: a_closed_file_is_not_answered_from_the_unpack_cache. It fails without the fix and passes with it. All 38 hle::dstorage:: tests pass here (Linux, Wine, release).

One request, if you have the time: the PR can't be merged without scripts/smoke.ps1 blocks from Windows, and I only have Linux. Could you run it, with any title you have:

.\scripts\smoke.ps1 -Game <title folder> -StartupTimeoutSeconds <enough for the shader preparation>
  • once on main 8b957bec;
  • once on this PR's head 723f5a19, with the cache off (the default).

The same title and time for both. It would also be useful to run the PR's head twice more with $env:XCL_DSTORAGE_CACHE = 'D:\xcl-dscache', starting from an empty folder. The first run fills it and the second reads from it; report.json → dstorage.unpack_cache shows the hits. That part is optional.

On Linux/Wine, the same script on main b49be18, with and without this commit and with the cache empty and full, gave four clean 60 s runs (1,573–1,612 frames, 60 presents per 2 s, no removals or crashes; Wine reports no commit). The full cache answered all 6,507 requests.

AI-assisted. Claude Code, Claude Opus 5.5 made the fix and the test and wrote this reply.

@razormontana

Copy link
Copy Markdown
Contributor

@ozimchuk, I confirmed the Close fix at 723f5a19cf3c371de71da06888302f43ab1dfb72. The live-handle check now precedes the per-file cache lookup. An already closed file falls through to the usual E_FILE_HANDLE path. Your update · Handle check · File-read path

Focused Windows tests

Windows 11 Pro 23H2 (22631.5335), i7-10700K, RTX 3070 8 GB on driver 617.42, Rust 1.100.0 x86_64-pc-windows-msvc, debug tests and D: SATA. Here is the command I ran.

cargo test --offline --locked -j 1 -p xcl --lib hle::dstorage:: -- --test-threads=1 --nocapture

Each run passed all 38 tests, including a_closed_file_is_not_answered_from_the_unpack_cache, with 0 failures, 0 ignored and 0 reported skips. I ran once with XCL_DSTORAGE_CACHE unset and once with a fresh D: cache (64 MiB, verification enabled). The opt-in run's stored 48 KiB matched the synthetic fixture payload.

Windows retail smoke

Clean release builds succeeded on 8b957bec6f82495aec940944c6bf47faa02db590 and 723f5a19cf3c371de71da06888302f43ab1dfb72. Both guarded cache-off scripts/smoke.ps1 attempts on main reached the 300 s shader preparation limit before the guest started. The second attempt was seeded with the first attempt's saved shader cache.

A third guarded main run is active with no local preparation deadline and the stock script's 24-hour startup allowance. Its preparation window showed 2,764 of 26,503 records processed at about 18 minutes. The paired cache-off xcl-smoke blocks and optional retail cold/warm measurements are pending.

AI use

Codex GPT-6.2 inspected the fix, ran the Windows tests and drafted this reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants