perf(libc): walk shared views in order and remember collectable ranges in Windows write collects #5062
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5062"
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 TideOPL on 2026-10-13T10:14:23Z.
What
A clearing write collect on Windows (
WindowsMappings::Collectwithclear) first checks the whole range withcollectable(), then walks it. Both looked up the shared-view map once per 16 KiB page (views.find(base)). A title that maps gigabytes of direct memory as shared views has hundreds of thousands of entries in that map. A collect over a 36 MB render target then costs thousands of tree searches, each a chain of cache misses, before any page is reported.Collectandcollectable()take onelower_boundat the start of the range and step the iterator forward (viewAt). A jump past a clean range or a private region falls back tolower_bound. The pages reported and the order are unchanged.collectable()keeps the ranges it proved, merged, incollectableRanges. A clearing collect inside a proven range skips the check. Every call that can change the answer clears the set:Commit,Map,SetProtectionandreset(behindResetandCommit's placeholder replacement).Pin,Unpin,HandleWriteand the host-write calls change neither view membership nor a view's protection, so they leave it.#4875 also edits
Collect, but a different part: theGetWriteWatchbuffer sizing on the private-memory path. The two touch different lines.Tested
System: Windows 11 (26200), Ryzen 7 7800X3D, RTX 5070, WinLibs GCC 15.2, Release,
BUILD_TESTING=ON, rebased onmain872f52320.Suites: after the rebase onto
872f52320(it walks the resident views from #5596 through the same in-order lookup, and keeps #5730's shared section slots), with every test executable rebuilt: the fullctestpasses 1140 of 1140 (skips as onmain), includingctest -R "guest_memory|write_watch|libc|libkernel"(9 of 9) andctest -R "^agc"(494 of 494,agc_driver_stencil_ref_exportskipped as onmain).No new test. The change only affects speed: the pages reported are the same, so no test can fail on
mainand pass here. The existingCheckFailedCollectKeepsWritesinguest_memoryproves a shared range collectable,mprotects half of it to no access, and requires the next clearing collect to fail and keep the write. With the clear inSetProtectionremoved, it fails atGuestMemory.cpp:1363, so it guards the cache.tools/check_conventions.py --base origin/mainpasses.Minecraft Dungeons II (PPSA16064), gameplay at the Squid Coast spawn, standing still. This is not a clean-
mainA/B: the title needs patches that are not inmainyet to reach gameplay (#4515/#4517, #425 and local ones). Both sides are the same build with and without each step, captured with the #4526 trace,APS5_TRACE_SECONDS=25:15opened 25 s after the HUD appears. Each A/B had a warm-up boot per side and interleaved runs.libc.prx, not Windows. After both steps the collect samples fell from 461 to 254 in comparable runs; what remains is mostlyVirtualProtectre-arming written views.About 6% at p50 together. I can attach the traces.
Not run: Linux and macOS (the change is in the Windows-only
WindowsMappings), AMD, cleanmainin a title; the MCD2 numbers below were measured before this rebase and were not rerun.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)Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T11:13:24Z.
Review note (static):
collectableRangesis cleared inCommit,Map,SetProtectionandreset, butcollectable()also accepts private regions that areMEM_COMMITandMEM_PRIVATE.munmapincore/libs/prx/libkernel/DirectMemory/DirectMemory.cpp(the non-arena branch,VirtualFree(addr, len, MEM_DECOMMIT)) andmunmap_release(MEM_RELEASE) change that state without going throughWindowsMappings. If a proven range can contain such memory, a later clearingCollectwould skip the check that used to return false for decommitted pages. Is that path unreachable for collected ranges (for example because only arena memory is collected), or should those two calls also invalidate the set?CheckFailedCollectKeepsWritescovers theSetProtectioncase only.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by TideOPL on 2026-10-13T11:40:19Z.
Thanks for checking. That path can't reach a collected range:
PageStates/the tracker take their range fromGuestArenaRange(coverage.Initialize(base, size)inGuestMemory.cpp), andWatched/coversrequire it, soCollectis only ever called on arena memory.DirectMemory.cppboth calls are on the non-arena side:munmaprunsVirtualFree(addr, len, MEM_DECOMMIT)only whenKernelArena::Contains(addr, len)is false, andmunmap_releaseis only called in theelseof the same check. Arena memory is unmapped throughGuestArenaReset, which reachesWindowsMappings::resetand clearscollectableRanges.So no proven range can contain memory those two calls change.
CheckFailedCollectKeepsWritescovers theSetProtectioncase because that is the one way an arena view becomes uncollectable without a reset.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-13T19:24:35Z.
Heads-up: #3864 changes the same code. It keeps an iterator hint for the view lookup, skips ranges already in
cleanRangesincollectable(), and remembers clean pages once per run inCollect. The view-lookup change duplicates this PR's in-order walk; the other two parts differ. Whichever merges second will rebase.Imported from boykopovar/AnyPS5. Originally posted by TideOPL on 2026-10-13T19:27:51Z.
All good, I will rebase. I imagine your PR will be merged first.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T19:28:34Z.
Thanks for coordinating. I can’t predict maintainer merge order, and I don’t think either change is a functional prerequisite for the other. #5062 is clean on current main with its listed checks completed; #3864 still needs its requested rebase and a repeat of the original PPSA28416 measurement on the new base. If #5062 merges first, please rebase #3864 onto it; if #3864 merges first, I’ll rebase #5062 and retain both changes.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T10:43:24Z.
Checked the diff:
collectableRangesis cleared inCommit,Map,SetProtectionandreset(which also covers view erasure on unmap),Collectdoes not mutateviewswhile walking, so the iterator inviewAtstays valid, and the in-order walk reports the same pages. Pin/Unpin/HandleWrite/host writes change neither membership nor protection. The private-memorymunmapquestion is answered above (the tracker only covers the guest arena). Merging once the Windows build job is green.Imported from boykopovar/AnyPS5. Originally posted by TideOPL on 2026-10-14T10:54:23Z.
@adriadam10 thanks! The checklist was missing the newer "GPUs tested" line; it's filled in now (n/a, libc only), and
check_conventions.pywith--bodypasses on it, as does the re-run conventions check. AI-assisted is answered: yes.