perf(libc): size each write-watch query to the region it covers #4875
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4875"
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 nikowolf123 on 2026-10-13T01:20:27Z.
What
WindowsMappings::Collectwalks a range oneVirtualQueryregion at a time and gives eachGetWriteWatchcall the whole remaining output capacity, up to the tracker's 65536 entries. The call's cost follows that capacity, not the size of the range it walks. Flexible memory is committed in 16 KiB allocations, one region each, so a collect over it pays the full-capacity price once per 16 KiB.Each query now gets room for at most the pages its region spans, which is the most it can report. The pages returned, their order and the overflow handling when the shared buffer fills are unchanged: the capacity is still capped by what is left in it. The span counts every 4 KiB page the walked part of the region touches, so a walk that starts inside a page still has room for that page.
This is the
GetWriteWatchbuffer sizing from DotDebian's #2863, split out as oneandonlydean and adriadam10 asked in that PR's discussion and review. #2863 carries it inside its queue-worker prewalk change and conflicts withmain; once this lands, #2863 can drop its copy. #2863's formula,(stop - cursor + 4095) / 4096, is one page short when the walk starts inside a page and ends inside the next one; this version rounds both ends. Other open PRs that touchWindowsMappings.hpp(#4661, #4535, #4367) merge cleanly with it.Numbers on Windows 11 26200, i9-14900KF, median of 2000 calls or 31 rounds (other build jobs were running, load average about 21):
GetWriteWatchon a clean 16 KiB allocationmain(remaining capacity)memcmpof the same bytesThe benchmark is standalone and reproduces the region walk of
Collectoutside the repository:clamp_bench.cpp
No title trace: I have no PS5 or game files. Which titles keep most of their write-watched memory in 16 KiB flexible-memory allocations is not measured here; the saving applies to every collect over such memory.
Tested
Windows 11 Pro 26200, i9-14900KF, RTX 5080 (NVIDIA 616.92), WinLibs GCC 15.2.0, Release with tests:
CheckSplitPrivateCollectinguest_memory: a resetting collect over eight 16 KiB allocations reports every stored page exactly once and in order with an output capacity of 32, 5 and 1, then nothing until the next store; a single store in the fourth allocation reports only that page. It passes withmain'sCollectand with this change, which is the point: the change must not alter what a collect returns.guest_memory: 30 of 30 runs (ctest --repeat until-fail:30).ctest(ANYPS5_REQUIRE_VULKAN=1,--parallel 4) on this commit: 1021 tests, 0 failed.WindowsMappings.hppand the new check are Windows only.tools/check_conventions.py --base origin/mainwith this title and body: passed.main2f969f147together with #4878 (no conflicts in this PR's files): native fullctestwith both changes, 1064 tests, 0 failed;guest_memory10 of 10 runs. Oncac4e9688the only failure waspatched_libraries, which failed the same way on unmodifiedmainthen (#4877; fixed by #5025).In a title, measured by @gjonn on Windows 11 with an RTX 5090 Laptop GPU while profiling Gran Turismo 7 (PPSA01317), on a local branch with the same per-segment cap (comment):
GetWriteWatchcalls went from about 8 µs to about 0.5 µs on average.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 gjonn on 2026-10-13T14:45:48Z.
Measured this while profiling PPSA01317 on Windows 11 (RTX 5090 Laptop). The cost of
GetWriteWatchgrows with the output count it is given, not with the range it walks. Same clean 192 KiBMEM_WRITE_WATCHregion,WRITE_WATCH_FLAG_RESET, 3,000 calls per row:In game, on a local branch with the same per-segment cap in the collect loop:
GetWriteWatchcalls went from mostly 4-16 µs (about 8 µs average) to mostly under 1 µs (about 0.5 µs average).That branch also calls
GetWriteWatchonce per resident run, which main does not, so the call counts there are higher than on main.On the count itself: rounding both ends to pages, as this PR does, matters. The simpler
(stop - cursor + 4095) / 4096under-counts by one page when both ends are unaligned, which can leave a dirty page unreported.Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-13T14:57:20Z.
Thank you, that's the in-title measurement this PR was missing: per-call cost following the output capacity (0.29 µs at 49 entries vs 7.10 µs at 65,536), and the GT7 race scene's walk time falling from 2.5-3.6 s to 0.1-0.4 s per 10 s.
The page count is computed as you describe: both ends are rounded to pages, so a 4 KiB range starting mid-page counts the two pages it touches, where
(stop - cursor + 4095) / 4096gives one.