Skip to content

HLE/DirectStorage: reuse per-worker DecompressorOxide and source/intermediate scratch buffers - #2981

Open
RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/dstorage-worker-scratch
Open

RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/dstorage-worker-scratch

Conversation

@RVZO6

@RVZO6 RVZO6 commented Nov 26, 2026

Copy link
Copy Markdown

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

Keep a worker-local WorkerScratch in work() holding:

  • A boxed DecompressorOxide reset via init() before each zlib stream (matching decompress_slice_iter_to_slice(..., true, false) flags and status mapping bit-for-bit without re-zeroing Huffman tables).
  • Reusable source and intermediate Vec<u8> buffers, trimmed back after any outlier request exceeding 8 MiB (SCRATCH_RETAIN_CAP) so idle worker RSS stays bounded.

Leaves open_file, FACTORY_VTABLE, scheduler, read_into, and into_destination untouched (zero overlap with #2904 and #2883; zero new dependencies).

Measured (--release bench_dstorage_worker_scratch, median of 3 runs)

  • 256 KiB × 400: 48.03 ms → 27.61 ms (1.74×, -42.5%)
  • 32 KiB × 2,000: 30.21 ms → 28.79 ms (1.05×, -4.7%)
  • 4 KiB × 8,000: 48.24 ms → 44.41 ms (1.09×, -7.9%)

Verification

  • Official Windows MSVC + WARP SM 6.6 CI (Rust 1.98.1, default + --tests + --features diagnostics clippy, xtask policy, full workspace tests): RVZO6/VIctory CI run #45738573777 (green, combined Wave 8 check on 63bcca4).
  • Added reused_decompressor_matches_fresh_on_valid_and_error_streams (exact parity vs decompress_slice_iter_to_slice across valid, short-output, empty, bad-header, bad-Adler32, truncated, output-too-small, and post-error recovery cases) and worker_scratch_reuses_capacity_and_trims_outliers.
  • Not run: the title; any hardware GPU.

AI-assisted: T3 Code (Antigravity, gemini-4-neon).

…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)

@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.

Verified: per-worker scratch is sound since each worker runs one request at a time. init() yields fresh decompressor state before each stream (tables included), flags match the old decompress_slice_iter_to_slice semantics (zlib header + non-wrapping output), and non-Done statuses still map to INVALID_DATA. The 8 MiB retain cap prevents one oversized request from pinning worker memory, and buffer reuse keeps the source/intermediate borrows disjoint.

@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.

Reviewed fbfe80ed against main (63bcca43) — worker-local scratch and DecompressorOxide reuse looks sound:

  • Decoder and source/intermediate buffers stay strictly worker-local outside the scheduler lock (dstorage.rs:450-469), init() cleanly resets miniz_oxide's state machine (preserving zlib header checks, Adler-32 validation, and error mapping), and the new test covers reuse after malformed, truncated, and output-limited inputs.
  • Two notes to keep in mind:
    1. With 8 MiB per retained buffer (16 MiB per worker), idle workers can retain up to 16 MiB × worker_count (e.g., ~1 GiB at 64 workers), and scheduler staging accounting (514-530, 657-667) covers buffered file input without the zlib-to-BCPack intermediate buffer.
    2. Minor merge overlap with #2987 around dstorage.rs:461-465 (worker loop calling execute / count / finish), straightforward to combine by trimming scratch before reacquiring the lock and dropping #2987's QueuedRequest before publishing completion.

@pickles976

Copy link
Copy Markdown

16 GiB Linux/Wine laptop verification, exact PR head fbfe80ed28 using the Windows target under Wine 11.0: four focused DirectStorage tests passed 1/1 each — decompressor fresh-vs-reused success/error parity, scratch reuse and release above 8 MiB, queued file raw+zlib reads, and zlib+BCPack combined requests. Local logs: .compat-runtime/logs/verify-pr2981-wine-{decompressor-parity,scratch-trim,queued-file-zlib,zlib-bcpack}.log (not attached). No GPU host device, title run, process RSS or global available RAM was measured; these fixtures prove bounded scratch lifecycle and correctness, not relief from our VIctory world-load guard stop below 2 GiB free system RAM.

This is a useful component of the larger host-RAM work, but the opt-in application-wide admission/reclaim budget is tracked separately in #3012. Please keep its RAM benefit claims scoped to measured scratch and add a guarded matched title/RAM sample before describing the world-load impact on a 16 GiB machine.

@futurepro62

Copy link
Copy Markdown

NVIDIA full-suite run, to go with the reports above by pickles976, since none of them names a GPU run. Tested exact head fbfe80e (on its base 63bcca4).

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.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. Each dstorage::work thread now keeps one WorkerScratch: a boxed DecompressorOxide, reset with init() per stream, plus reusable source and intermediate buffers, trimmed back above 8 MiB. This saves the per-request allocations and zeroing.

What I checked:

  • The new inflate calls decompress with PARSE_ZLIB_HEADER | USING_NON_WRAPPING_OUTPUT_BUF and no HAS_MORE_INPUT. That is what decompress_slice_iter_to_slice(.., true, false) does for a single final slice, and a stream that runs out of input is still an error. reused_decompressor_matches_fresh_on_valid_and_error_streams checks this against the old call, including recovery after errors.
  • Stale bytes can't leak from the reused buffers. read_into fills all size bytes or fails (END_OF_FILE), and only intermediate[..n] (the inflated length) reaches BCPack, as with the old truncate(n).

Minor and optional: the #[ignore] benchmark bench_dstorage_worker_scratch could be left out of the tests. The figures in the description are enough.

CI: passing. A tester also ran the full suite on an RTX 5070 Ti with no new failures.

Recommending a look, not approving.


Generated by Claude Code

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
…Oxide and source/intermediate scratch buffers

@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.

Tested locally merged onto origin/main (63bcca4) on Windows 11 x64:

  • cargo test -p victory-hle dstorage: all tests passed.
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

@Desamodeth Desamodeth 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.

fbfe80ed has a repeatable allocation cliff on the file-source path. Alternate 5 MiB and 6 MiB requests on one worker: Vec::resize grows the 5 MiB buffer to 10 MiB, then trim() drops it. Both requests are below the 8 MiB cap. The intermediate buffer uses the same growth policy.

Windows x64, Ryzen 7 9800X3D, Rust 1.98.1 release. I used the extracted read_source/read_into functions on the same warm local file. Medians of 12 interleaved batches of 32 reads:

File read sizes main 63bcca43 PR capped growth
5 MiB throughout 23.04 ms 10.22 ms 10.15 ms
5 / 6 MiB alternating 25.08 ms 29.95 ms 11.89 ms
5 MiB / (8 MiB + 4 KiB) 29.44 ms 34.85 ms 34.79 ms

On the 5/6 trace, growth events fell from 32 to 2 and peak source Vec capacity from 10 to 8 MiB. The revised worker retains 8 MiB at the end of that trace; the current PR retains zero. A second run in reversed order gave 29.60 → 11.48 ms. The above-cap control still released the buffer. Matched file bytes checked separately. These are file-read and scratch measurements only; no decompression, GPU, title run or process RSS measurement.

Tested change for both resize sites
fn grow_scratch(bytes: &mut Vec<u8>, len: usize) {
    if len <= bytes.len() {
        return;
    }
    if len > bytes.capacity() && bytes.capacity() > SCRATCH_RETAIN_CAP / 2 {
        bytes.reserve_exact(len.max(SCRATCH_RETAIN_CAP) - bytes.len());
    }
    bytes.resize(len, 0);
}

Call grow_scratch(scratch, size) in read_source and grow_scratch(intermediate, inter_size) in execute, keeping trim() as written. Regression case: grow both buffers to 5 MiB, then 6 MiB, then 8 MiB, trimming and checking both still hold the requested length after each step. Grow one to 8 MiB + 1 and check trimming releases only that buffer. Isolated checks passed; I did not run the workspace suite.

AI-assisted with Codex.

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.

8 participants