Skip to content

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

Closed
Lewandox wants to merge 1 commit into
LooperSalty:mainfrom
Lewandox:fix/dstorage-bounded-tracking-and-scratch-reuse
Closed

Lewandox wants to merge 1 commit into
LooperSalty:mainfrom
Lewandox:fix/dstorage-bounded-tracking-and-scratch-reuse

Conversation

@Lewandox

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Collaborator Author

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 Lewandox closed this Nov 26, 2026
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.

1 participant