fix(victory-hle): eliminate dstorage ENQUEUED leak/desync, bound page history, and reuse capped scratch buffers #401

Closed
github-bot wants to merge 1 commit from github-bot/gh-401 into main AGit
Owner

@⁠Lewandox on GitHub · 2026-11-26 17:19 UTC · original
Lewandox:fix/dstorage-bounded-tracking-and-scratch-reuse → main · +346 −170 · 1 files · 1 commits · closed without merging

Summary

Fixes unbounded memory retention / FIFO desync and reduces per-request heap churn in DirectStorage (third_party/victory/crates/victory-hle/src/modules/dstorage.rs):

  1. Eliminate ENQUEUED leak, FIFO desync, and global mutex contention:
    • Previously, as_enqueued pushed each PAGE_DESTINATION request's page-array snapshot into a global ENQUEUED Mutex<Option<HashMap<usize, VecDeque<Vec<usize>>>>>, and filled() popped from it when the worker ran.
    • If a queue closed (close_lane) while requests were unsubmitted/queued, or if execute() failed before into_destination() (e.g. invalid args, read failure, zlib error), those snapshots leaked forever and permanently shifted the FIFO queue for any future request reusing that page-array address (and parallel workers could even pop each other's snapshots out of order).
    • Attaching enqueued_pages: Option<Vec<usize>> directly to each Command::Read / Lane::reads entry / Job removes the global ENQUEUED mutex completely, guarantees zero leaks on close or early failure, and pairs every worker execution with its exact enqueue-time snapshot.
  2. Bound LAST_RUN / SAME_RUNS tracking memory:
    • Merges LAST_RUN and SAME_RUNS into a single PAGE_HISTORY map (one lock + one lookup instead of two) and caps it at MAX_PAGE_HISTORY (4,096) entries so long sessions with churning page-array addresses cannot grow unboundedly.
  3. Reuse capped worker thread-local scratch buffers:
    • Uses a small per-worker thread-local scratch pool (with_scratch, max 2 buffers, each capped at 4 MiB on return) for file-read bounce buffers, compressed source reads, zlib+BCPack intermediate buffers, and non-consecutive physical-page staging buffers.
    • Eliminates repeated multi-MB heap allocations, zero-fills, and VirtualAlloc/VirtualFree TLB shootdowns during world streaming while dropping any oversized (>4 MiB) buffer immediately so idle workers never retain large RAM.
  4. Unit tests:
    • Verifies closing a queue or failing a PAGE_DESTINATION request early drops its enqueue snapshot cleanly without corrupting subsequent requests on the same page-array address.
    • Verifies PAGE_HISTORY stays strictly bounded at MAX_PAGE_HISTORY even under heavy address churn.
    • Verifies nested with_scratch calls preserve independent buffers, reuse capacity up to the cap, and drop oversized buffers on return.

Verification

  • cargo test -p victory-hle --lib modules::dstorage::tests
  • cargo xtask policy inside third_party/victory
**@⁠Lewandox** on GitHub · 2026-11-26 17:19 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/401) `Lewandox:fix/dstorage-bounded-tracking-and-scratch-reuse` → `main` · +346 −170 · 1 files · 1 commits · closed without merging ## Summary Fixes unbounded memory retention / FIFO desync and reduces per-request heap churn in DirectStorage (`third_party/victory/crates/victory-hle/src/modules/dstorage.rs`): 1. **Eliminate `ENQUEUED` leak, FIFO desync, and global mutex contention**: - Previously, `as_enqueued` pushed each `PAGE_DESTINATION` request's page-array snapshot into a global `ENQUEUED` `Mutex<Option<HashMap<usize, VecDeque<Vec<usize>>>>>`, and `filled()` popped from it when the worker ran. - If a queue closed (`close_lane`) while requests were unsubmitted/queued, or if `execute()` failed before `into_destination()` (e.g. invalid args, read failure, zlib error), those snapshots leaked forever and permanently shifted the FIFO queue for any future request reusing that page-array address (and parallel workers could even pop each other's snapshots out of order). - Attaching `enqueued_pages: Option<Vec<usize>>` directly to each `Command::Read` / `Lane::reads` entry / `Job` removes the global `ENQUEUED` mutex completely, guarantees zero leaks on close or early failure, and pairs every worker execution with its exact enqueue-time snapshot. 2. **Bound `LAST_RUN` / `SAME_RUNS` tracking memory**: - Merges `LAST_RUN` and `SAME_RUNS` into a single `PAGE_HISTORY` map (one lock + one lookup instead of two) and caps it at `MAX_PAGE_HISTORY` (4,096) entries so long sessions with churning page-array addresses cannot grow unboundedly. 3. **Reuse capped worker thread-local scratch buffers**: - Uses a small per-worker thread-local scratch pool (`with_scratch`, max 2 buffers, each capped at `4 MiB` on return) for file-read bounce buffers, compressed source reads, zlib+BCPack intermediate buffers, and non-consecutive physical-page staging buffers. - Eliminates repeated multi-MB heap allocations, zero-fills, and `VirtualAlloc`/`VirtualFree` TLB shootdowns during world streaming while dropping any oversized (>4 MiB) buffer immediately so idle workers never retain large RAM. 4. **Unit tests**: - Verifies closing a queue or failing a `PAGE_DESTINATION` request early drops its enqueue snapshot cleanly without corrupting subsequent requests on the same page-array address. - Verifies `PAGE_HISTORY` stays strictly bounded at `MAX_PAGE_HISTORY` even under heavy address churn. - Verifies nested `with_scratch` calls preserve independent buffers, reuse capacity up to the cap, and drop oversized buffers on return. ## Verification - `cargo test -p victory-hle --lib modules::dstorage::tests` - `cargo xtask policy` inside `third_party/victory`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 17:25:29 +00:00
github-bot closed this pull request 2026-11-26 19:34:44 +00:00
Author
Owner

@⁠Lewandox on GitHub · 2026-11-26 19:15 UTC · original

Closing as superseded after #409 (\dstorage.rs\ was rewritten around \Lanes/\Scheduler/\Traced, removing the old \ENQUEUED\ map and unbounded page history). The remaining \WALLS_FILES\ leak fix stays tracked in #421.

**@⁠Lewandox** on GitHub · 2026-11-26 19:15 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/401#issuecomment-7201385575) Closing as superseded after #409 (\dstorage.rs\ was rewritten around \Lanes\/\Scheduler\/\Traced\, removing the old \ENQUEUED\ map and unbounded page history). The remaining \WALLS_FILES\ leak fix stays tracked in #421.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.