perf(libc): keep no per-page physical state for resident guest pages on Windows #11152
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11152"
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?
Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-25T19:46:55Z.
What
On Windows,
WindowsMappings::Mapgives each 16 KiB page of a resident run (singly mapped private direct memory) five heap objects: aView, aSharedPage, aPhysical, thephysicalmap node for it, and an aliases buffer. That is about 480 bytes per page, or 30.9 MiB of private memory per GiB mapped. A title that maps its whole direct-memory pool pays about 0.4 GiB for this.For a page that is never mapped at a second address, evicted, re-protected or pinned, the
SharedPage, thePhysicaland the aliases hold only default values.This change keeps only the
Viewfor a resident page. Its pin count and compare mark move into padding in theView, which stays 128 bytes. Resident runs of a physical page are found through a per-run index keyed by section and offset. The other objects are created only when needed:demotecreates theSharedPageandPhysicalwhen a page becomes shared.evictcreates aPhysicalonly for pages that hold data or carry a compare mark.ForgetSectiondrops the section's entries from the index.Demoting a run creates its two objects per 16 KiB page at that point, for example 65,536 pairs for a 1 GiB run, so the cost moves from every resident mapping to the mappings that become shared.
demoteandevicterase a run's index entry only after their per-page loops, anddemoteadds its newphysicalentries only after the run's private pages are released.Tested
Windows 11 26200, Ryzen 5 5600X, WinLibs GCC 15.2.0 r7, Release. Head
83c8315d70, rebased ontomainbe9effaeb0, which includes #9505.A new check in
guest_memorymaps 1 GiB with protection0x33. It requires less than 16 MiB per GiB of private bytes beyond the mapped bytes. It then maps one 64 KiB slice at a second address and one 2 MiB run as an alias, checks the data in both directions, unmaps and releases everything, and requires private bytes to return to within 4 MiB of where they started. Withmain'sWindowsMappings.hppand this change's tests it fails at the bound in 3 of 3 runs, at 30.90, 30.87 and 30.92 MiB per GiB. With this change it passes in 3 of 3 runs, at 13.40 MiB per GiB.Five new checks in
windows_write_watchcover the new index. They pass withmain'sWindowsMappings.hpptoo (2 of 2 runs), since they test behaviour thatmaingets from the per-page objects:ForgetSectiondoes not report a section as blank while a resident run of it is still mapped;ForgetSection, a second resident run of the same physical pages replaces the first in the index; releasing the first run, all of it or one page of it, leaves the second run's data visible to a third mapping, and writes through that mapping reach the second run.All,FreshandArmed, reports exactly its two written pages, once.With the run-identity check in the index lookup removed, the replaced-run check fails in 2 of 2 runs.
^(guest_memory|windows_write_watch.*|windows_mappings_release.*|agc_write_tracking.*|agc_write_watch_coverage|agc_.*host_import.*)$, 3 rounds: 21 pass andagc_host_import_sparseskips.agc_host_import_cut_lifetimefails, as it does onmain(#10746).^guest_: 788 of 788 pass.Independent run by ycbuzi (Windows 11 Home 26200, WinLibs GCC 15.2.0 r7): this head merged onto
main35ac3402c8builds without warnings;guest_memory,guest_mmap_fixed_anonymousandwindows_write_watchpass 2 of 2, and withmain'sWindowsMappings.hppguest_memoryfails atGuestMemory.cpp:2202.PPSA05110 (own dump), memory only, no speed claim. Base:
main61fc572cee+ #10614, #10913, #7844, #9181 and a local equivalent of #10887, which the title needs to load and run. Change: the same base plus this change at an earlier review state (the same data structures; later commits moved two steps indemoteand added the null check and tests). Each run follows the same scripted route (boot, title, New Game, opening) for 330 s on a shared GPU, with no stop. Private bytes are sampled from outside the process. The runs went base, change, change, base. The two runs of each pair rendered at about the same rate: pair 1 at 500-600 presents per 10 s, pair 2 at 40-80.Within each pair the change saves 274 and 293 MiB of private bytes at 290 s, and 0.27 and 0.28 GiB of peak private bytes. The unit check predicts about 237 MiB for the 13,793 MiB of direct memory the title maps (17.6 MiB per GiB × 13.5 GiB).
Summary lines at about 290 s, pair 1 (shared GPU; frame times are not compared)
Overlap: #9505 has merged, and this change is rebased onto it. In #9505's run mapping, a resident run is now demoted through the new index before
slotOf, because resident pages are no longer in aSharedPage's aliases. #9505'sViewinitializers carry the two new fields, and its unmap loop skips the nullpageof a resident view. #9098 (on #9505) still changes the same loops; whichever of #9098 and this PR merges second takes the same resolution.Not tested: Linux and macOS (
WindowsMappingsis Windows-only), and titles other than PPSA05110.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T19:53:59Z.
Review: DISMISSED
The implementation and the memory-footprint measurements are clearly scoped, and the PR explicitly avoids claiming a speedup. The
perflabel still needs the artifacts requested by the repository's Performance report form: the body includes twoaps5-summary.jsonllines from the same title and machine, but both have"trace":"", the recorded switches only includeAPS5_TRACE_SUMMARY_EVERY, and no trace files ortools/trace_digest.pyoutput are attached. Please add pairedmainand change trace captures from the same scene/machine and include both digests, or get maintainer confirmation that a memory-only change is exempt before treating the performance evidence as complete.I could not run the reported Windows tests locally. The available MinGW compiler successfully syntax-checked
WindowsMappings.hppafter defining the placeholder-allocation constants missing from its older Windows headers; this is compile-only coverage, not a Windows runtime test. Hosted Windows/product tests are skipped by repository policy; the Windows relinker remains pending.Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-25T21:45:47Z.
Review: CHANGES_REQUESTED
Reviewed head
65720d76abby reading the diff and the existing review thread. I did not build or run this, so everything below is from the code, not from a test of mine.Requesting changes. I think there is a null dereference in a path this PR makes reachable, and the PR is not mergeable as it stands.
What it does. Resident runs (large private mappings that stand in for section views) no longer get a
SharedPageand aPhysicalslot per 16 KiB page up front. Resident views are created withpage == nullptr, aresidentIndexfinds the run for a (section, offset), and the per-page state is created lazily indemote. The memory numbers (a 1 GiB mapping staying under 16 MiB of bookkeeping) are a real improvement, and the PR is careful to say it does not claim a speed-up.The concern. After this change, every resident
Viewhas a nullpage. I applied the PR'sWindowsMappings.hpphunks to the current file and then searched for remaining dereferences. The PR guardsinvalidate,Pin,Unpin,Collectand the unmap path, butarmResidentis not touched:if (!it->second.resident || ... || it->second.page->pins != 0) return false;That loop only continues past the first test when the view is resident, so it evaluates
page->pinson exactly the views whosepageis now null.armResidentis called from the collect path for a run of at least 0x40000 bytes (run.end - start >= 0x40000). The PR usesview.pinsfor residents inPin/Unpin, so the intended replacement is clearlyit->second.pins. If I am wrong, tell me wherepageis set for resident views; if I am right, the new tests (which useBytes-sized runs) may simply not reach a run that large.Other points:
WindowsWriteWatch.cpp.demoteallocates before it mutates. Thecreatedvector (aSharedPageand aPhysicalper page) is built, andphysical.insert_or_assignis called, before theVirtualProtectthat canfail(). If that throws,physicalhas entries for a run that is still resident. Insert intophysicalonly after the protection and mapping steps succeed.demotenow creates 65,536 pairs ofshared_ptrallocations in one go. That is the cost moved from "always" to "on demote", which is the right trade, but the spike is worth one line.perflabel still needs the artifacts MarcZX10 asked for.The idea is sound. Please fix
armResident, add a test with a resident run of at least 0x40000 bytes that goes throughCollectwithArmedorAll, and rebase.AI-assisted: yes. Written with AI assistance and reviewed by me.
Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-26T07:21:20Z.
Review: APPROVED
Re-reviewed head
83c8315d70by reading the diff and the thread. I did not build or run it.My main objection was wrong:
armResidentdoes not exist on this head or on currentmain, so there is no null dereference there (I said so in my earlier comment). The other points are addressed in the code:demotestages the newPhysicalentries and doesphysical.merge(staged)only after theVirtualFreestep,CheckLargeResidentRunCollectsnow covers a 0x40000-byte resident run, and the allocation spike is in the description. Replacing my request for changes with an approval.AI-assisted: yes. Written with AI assistance and reviewed by me.