HLE: keep one view of a physical-page section instead of a view per write (VICTORY_XMEM_PERSISTENT_VIEW=1) #231
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/fxceless--victory-fork!231
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-231"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@HailLucifer666 on GitHub · 2026-11-24 22:51 UTC · original
HailLucifer666:perf/xmem-persistent-section-views→main· +625 −35 · 1 files · 4 commits · openProblem
write_physicalandpage_windowreach the physical pages throughView::map, which maps a view of the section, commits it withVirtualAlloc(MEM_COMMIT), writes, then unmaps it inDrop for View. Every write pays aMapViewOfFile, a commit and anUnmapViewOfFile, once per run of consecutive pages, however few bytes it touches.page_windowandwrite_physicalhave exactly one production caller: the DirectStorage destination path indstorage.rs, which triespage_windowfirst and falls back towrite_physical. That is the streaming path, called once per in-flight request on every worker, for the whole world load and for every texture streamed while playing. An unmap invalidates that mapping's translations on every processor, so the cost is charged to all cores rather than to the writer alone.Fix
Sectionkeeps one read-write view of its whole range, mapped on first use and unmapped when the section goes (MappedBase, dropped with theSection), plus one bit per 64 KiB page recording which pages are already committed through it.Viewbecomes an enum:Temporaryis the old map/commit/unmap path,Persistentpoints into the section's view and unmaps nothing when it drops.Section::ensure_committedcommits a run in a single call the first time and makes no calls after, reusingcommit()so a host at its commit limit still produces the same crash report.View::temporary, so the worst case is today's behaviour.Mappingis the seam that lets tests force either path, since the switch is read once per process.VICTORY_XMEM_PERSISTENT_VIEW=1, matching the other rollout switches here. It holds a whole-section mapping for the section's life, which is 6.1 GiB of address space for the title's largest allocation, so it should be proven by a live run before it becomes the default. No frame-rate claim is made here — this tree cannot measure that.Verification
cargo test -p victory-hle xmem:: --locked— 16 passed (5 new, all executing:parse_persistent_views_accepts_only_an_explicit_yes,persistent_views_write_and_commit_once,persistent_page_windows_share_one_mapping,temporary_views_never_map_the_section_whole,ensure_committed_refuses_ranges_outside_the_section).cargo clippy --workspace --all-targets --locked -- -D warnings— cleancargo clippy --workspace --all-targets --all-features --locked -- -D warnings— cleancargo test -p xtask --locked— 4/4 passedmark_committedmade a no-op fails the twocommitted_allassertions and nothing else; removing thecommitcall makes the persistent write fault withSTATUS_ACCESS_VIOLATION, which proves the commit is load-bearing; forcing thecommitted_allfast path off leaves 16/16 green, so it is documented as the one surviving mutation rather than given a test that could not fail.View::temporaryis the oldView::mapbody), so every pre-existing test is the negative control for the persistent one.Notes for review
XMemMapPhysicalPages, its placeholders, and theunmap()bookkeeping — the persistent view is not inphysical.views, so remapping and unmapping behave exactly as before.write_physicaland thePageWindowfields; this touchesPageWindowonly by changing the type of itsviewfield. Whichever lands second rebases cheaply.Review of the commit before this one found five things worth fixing. All of them are about the evidence the change rests on, not about the mapping. THE BUG. Three of them are claims the earlier commit message or comments got wrong. The comment on `View::bytes` said two writers through the section's one mapping write "exactly as they did through separate temporary views". They do not. Through separate virtual views two writes to the same bytes were a data race; through one shared base two live `&mut [u8]` over overlapping bytes is undefined behaviour, and the compiler may assume they do not alias. The precondition -- no two writes in flight cover the same page -- is real, is stated in `dstorage` for the buffer branch only, and is not stated at all for the page branch that calls these helpers. The comment now names it as a precondition rather than calling it unchanged behaviour. The comment on `ensure_committed` promised that a host at its commit limit gets "the same crash report". It gets two: the persistent commit fails and reports, then the caller retries on the temporary path and that commit fails and reports again. The comment says so now instead of claiming otherwise. The `end <= self.size` comparison was described as unobserved and was declared alongside the one surviving mutation, which understated it: it is the only thing keeping `committed_all`'s page arithmetic inside `committed`. It was also invisible to all 16 tests, because both "past the section" rows are refused by the host's own `VirtualAlloc` first. A zero-length range past the end is now asserted refused; that row the host cannot supply, so deleting the comparison turns the test red. WHY NOT HYPOTHETICAL. `persistent_page_windows_share_one_mapping` passed with every write on the temporary path. Its only evidence was `persistent_ptr()` returning the base -- but the test itself calls that, and `View::persistent` calls it before the commit can fail, so the `OnceLock` was populated whether or not the persistent path ever ran. Every byte assertion still held, because the temporary view writes the same pages. A test that cannot see the thing it is named for is worse than no test, and this one would have been quoted as proof that the path works. And with the switch on, nothing said whether it was actually in effect: the map half warns per section, the commit half said nothing, and no run could report "these sections are persistent". That is the exact evidence the switch becoming the default is supposed to rest on. THE FIX. `page_bytes`, the helper the pre-existing tests read bytes through, now asks for `Mapping::Temporary` explicitly, so those tests are the persistent path's negative control whatever the environment says. `persistent_page_windows_share_one_mapping` asserts the pages were committed through the section's mapping after the first window, which only the persistent path does. Confirmed by probe: with `View::persistent` made to return `None`, it now fails with "the window fell back to a temporary view" where it previously passed. `ensure_committed_refuses_ranges_outside_the_section` gains the zero-length row, and the surviving-mutation note now names one mutation because only one remains. A `fallback_noted` on `Section` warns once per section when a commit through the persistent view fails, and `View::persistent` says once, at info, that pages are being written through the persistent view. A run with the switch on can now show which it did. SCOPE. xmem.rs only. The whole-section-vs-chunked decision, the fallback behaviour and the default path are unchanged. Two claims in the previous message are corrected here rather than by rewriting it: "the default path is byte-identical to before" is true of writes, not of allocation -- every section now allocates its page bitset either way, a few KiB per GiB; and "the same crash report" is two reports, as above. Verified: cargo test -p victory-hle xmem:: --locked 16 passed; the two clippy commands clean with and without --all-features; cargo test -p xtask 4 passed; cargo test --workspace --locked 500 passed, 0 failed, 7 ignored. Mutation runs 2026-11-25, all restored: `View::persistent` returning `None` outright fails both persistent tests ("fell back to a temporary view", "not mapped whole"); the `end <= self.size` comparison deleted fails the new zero-length row; the earlier probes (bitset not marked, commit removed) still behave as recorded. Co-Authored-By: DeepSeek V4.1 Flash <[email protected]>@HailLucifer666 on GitHub · 2026-11-25 00:19 UTC · original
Thanks — this was right, and the second half of it was the more important half.
View::bytes()formed its slice over the whole run, and onlywrite_physical/PageWindow::bytesnarrowed it afterwards. Through the temporary path that was harmless, because every view has an address of its own. Through the persistent path the section has one address, so narrowing after the fact was too late: two requests writing different bytes of one page each held a slice over the whole page.Both halves are now done, in
8ad1bd62:View::bytes_at(from, len)takes the offset and length and returns exactly those bytes, on either path. Nothing in the module forms a slice over a whole run any more, so writers whose bytes do not touch hold disjoint slices even inside one page.The exclusion rule is enforced, not assumed.
Sectionrecords the byte ranges its live persistent views hold;holdrefuses a claim that touches a live one, andView::persistentreturnsNonewhen refused — so the caller lands on a temporary view, which has an address of its own. Writers whose bytes really do overlap therefore go through two addresses and never through two slices at one base. A view gives its range back when it drops.You were also right that I had nothing to point at. I had written that the title "writes each destination alone" and cited
dstorage, but the page-destination branch states only a size fact — andzlib_into_pages_in_place_and_across_allocationsenqueues two requests with the same destination pages from the same offset, one through the in-place path and one through the buffered path. That is the case the claim rule now handles.What this does not do: it does not make overlapping concurrent writes correct. It makes them fall back to the path that was correct before, so a writer overlapping another still sees whichever write lands last, exactly as it did previously. Making that correct would need an ordering guarantee on the title's side that I cannot verify from here.
Verification (all local; the fork's CI runs sit at
action_requireduntil a maintainer approves them):cargo test -p victory-hle xmem:: --locked— 18 passed, including two new tests:a_view_slice_covers_only_the_bytes_asked_foranda_second_overlapping_persistent_view_falls_back_to_a_temporary_one.-D warningsclean, with and without--all-features;cargo test -p xtask4 passed;cargo test --workspace --lockedgreen.bytes_atreturning the whole run fails withleft: 65536, right: 3; its end check removed makes the past-the-end case read on instead of refusing; skipping the claim makes the second overlapping viewPersistent; never releasing it makes the re-claim fail.One residual I should name rather than leave implied: a leaked
PageWindowwould hold its range for good, so later writes to those bytes would keep taking the temporary path — slower and correct, degrading toward the old behaviour rather than toward unsoundness.@HailLucifer666 on GitHub · 2026-11-25 13:50 UTC · original
Your P2 is fixed on
ec89292d, in both halves you asked for.The range.
View::bytes_at(from, len)builds the mutable slice over[from, from + len)and nothing else — a checkedfrom.checked_add(len) <= wholefirst, so a range that does not lie inside the view is a panic with the numbers in it rather than a slice over someone else's pages. Two independent DirectStorage requests writing disjoint ranges of the same page now get disjoint slices even though they lie in one page, which the old whole-run slice could not give them.The exclusion. You were right that disjoint bytes are not enough on their own — the same virtual address shared by two lifetimes still needs a rule.
Section::holdnow refuses a second live persistent view whose bytes touch an existing one, and that request falls back to a temporary copy instead (a_second_overlapping_persistent_view_falls_back_to_a_temporary_one). So actual overlaps are excluded rather than merely being unlikely, and only ranges that never touch share the mapping.Tests:
a_view_slice_covers_only_the_bytes_asked_forpins the range; the fallback test pins the refusal.cargo test -p victory-hle --lib xmem::tests→ 19 passed, 0 failed.Probe: hand back the whole run again (
from_raw_parts_mut(ptr, whole), the old shape) → six tests fail, including the range one and the write path; restored byte-identical.Still true, and worth saying so that it is not read as covered: this is proven against the simulated fixtures only. The DirectStorage workers run outside the scheduler lock, and the only thing that can confirm the ordering in production is the title running a real streaming load with
VICTORY_XMEM_PERSIST=1.ec89292d0ftoad67d2557d@avt0m4t48 on GitHub · 2026-11-24 22:58 UTC · original · reviewed
P2. Borrow only the requested byte range in a persistent view. On
5faabc4,View::bytes()constructs an exclusive slice over every page in the mapped run.PageWindow::bytes()and the buffered write path then narrow it. Because the persistent view shares one virtual address, independent DirectStorage requests writing disjoint ranges of the same page can still form overlapping mutable references. The workers execute outside the scheduler lock. View · Scheduler · Rust slice safety contractCould the view take a checked offset and length and create the mutable slice only over those bytes? Actual overlapping writes would still need an enforced exclusion rule before using one address for both. The existing DirectStorage fixture submits overlapping destinations through the in-place and buffered paths. This is a source-review finding. Existing fixture
@HailLucifer666 on GitHub · 2026-11-26 13:50 UTC · original
Rebased onto
maina20809de; head is nowad67d255. Four conflicts inxmem.rs, all the same shape: main added framelog meters and thesplit_lateretry, while this branch renames the section handle and makesViewan enum. Resolution keeps both — main's meters and retry path, the branch'sView::{Temporary, Persistent}andsection.handle— and only a temporary view is unmapped, with that unmap still metered asDiskViewUs. Verified:cargo check/clippy -D warningson victory-hle clean;victory-hle xmem::21 passed, 0 failed.@EFriseer on GitHub · 2026-11-26 14:31 UTC · original
Measured on hardware, in case it helps this PR along. I wrote the same change independently before I found this PR: one kept view per section, a commit bit per 64 KiB page, behind a switch that is off by default. So these numbers are for the idea, not for this branch's exact code.
Setup: Ryzen 7 5800X3D (16 threads), RTX 5070 12 GB, 64 GB RAM, game on a PCIe 4.0 NVMe drive. Release build of
a20809dwith the fork's tuned switch set (CARD_WINDOWS_MB=1024, TEXTURE_ROOM_PERCENT=96, HEAP_WORKER, SETTLE_ROOM, PARALLEL_REFRESH, LOOKUP_CACHE and the rest). Free play in the open world and cutscenes of a large retail title. One launch without the change (22 min) and one with it (11 min). The routes differed.No new warnings or errors, and no device loss in either run.
On this 8-core CPU, the per-request map, commit and unmap seemed to compete with the title's own page remapping (map_alias) for the process's address space. The slow requests bunched up exactly where the title streams. That's also where cutscene models stayed low-detail and the frame stalled for 0.5 to 3 s. With the kept view, both mostly went away.
Address space: the kept views reserve the sections' size (6.1 GiB for the largest), and commit charge didn't change visibly.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.