perf(libkernel): reuse released direct-memory sections that never held data #8857
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8857"
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 Dripl1x on 2026-10-21T03:30:52Z.
What
On Windows every
sceKernelAllocateDirectMemorycreates a pagefile-backed section (CreateFileMappingWwithSEC_RESERVE) for the new physical range, and everysceKernelReleaseDirectMemorycloses it. SuperTuxKart allocates, maps and releases about 48 direct-memory blocks per frame, so it creates and closes about 48 sections per frame.A released section is still all zeros when no page of it was ever stored to: resident pages keep their contents in private memory and in saved buffers, not in the section.
WindowsMappings::ForgetSectionnow reports that case: no physical page of the section is marked stored and none still has a live view. On release such a section goes into a pool keyed by size. The pool holds at most 256 sections of at most 16 MiB each, 256 MiB in total. The next allocation of the same size takes a pooled section instead of creating one. A section that held data is closed as before, as is any section when the pool is full.APS5_NO_DIRECT_MEMORY_SECTION_POOLkeeps the old behaviour (documented indocs/dev/ENVIRONMENT.md).Tested
guest_memorygainsCheckReusedDirectMemorySections: an allocation after a release reuses the released section (process handle count unchanged), reads as zeros, and its aliases share data; a released section that held data is closed (handle count drops); eight reuse rounds with and without a second alias read zeros. On main the test fails at the handle-count check after the first release.guest_memoryandguest_mmap_fixed_anonymous, three repeats on this head: 2/2 passedAPS5_NO_DIRECT_MEMORY_SECTION_POOL=1: 2/2 passedlibkernelbuilds; the change itself is Windows-only (#ifdef _WIN32).SuperTuxKart captures for the before/after requirement: Windows 11 Pro build 26300, Ryzen 7 5800H, RTX 3070 Laptop GPU, NVIDIA 617.42, WinLibs GCC 15.2.0 r7, Release; SuperTuxKart (PPSA69421) tutorial prompt over the main menu, 1920x1080 internal rendering in a maximized 2560x1369 window, warm shader caches, audio muted, no input;
APS5_TRACE_SECONDS=20:8withAPS5_TRACE_EVENTS=1048576, console output not redirected, no profiling switches other than the local counters'APS5_REVIEW_PROBE; 2026-10-21, order main, this PR, (other PRs), main.nr21-s01-maincd700fe9ec+ probes7aaad9d81f9bnr21-s05-maincd700fe9ec+ probes7aaad9d81f9bnr21-s02-sectionsd43dfea82d02The FPS difference is within the run-to-run spread of main (8.86 / 9.44); this PR claims no FPS change. What it saves is shown under Where it helps.
Summary lines (main first, then this PR)
tools/performance_compare.py(first capture of each side)Performance comparison
Configuration differences
Frame and GPU times (ms)
Top phases (ms per frame)
tools/trace_digest.pyoutput (first capture of each side; all digests are in the evidence archive)nr21-s01-mainseconds 20:8 from present 43: 71 presents, frame p50 110.0 / p90 122.9 / p99 158.2 / max 158.2 ms, GPU busy 5.6 ms per present
NVIDIA GeForce RTX 3070 Laptop GPU, NVIDIA 617.42, Windows 10.0.26300, 1920x1080, commit 7aaad9d81f9b, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
nr21-s02-sectionsseconds 20:8 from present 58: 80 presents, frame p50 96.7 / p90 115.3 / p99 125.1 / max 125.1 ms, GPU busy 5.7 ms per present
NVIDIA GeForce RTX 3070 Laptop GPU, NVIDIA 617.42, Windows 10.0.26300, 1920x1080, commit d43dfea82d02, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Where it helps
The title allocates and releases about 48 direct-memory blocks per frame. Creating the backing (
CreateFileMappingW) and closing it are what the pool removes:cd700fe9ecPhysicalBacking (create)callsPhysicalBacking (create)µs per callPhysicalBacking (create)ms per frame~PhysicalBackingcalls~PhysicalBackingµs per call~PhysicalBackingms per framesceKernelAllocateDirectMemorycallssceKernelAllocateDirectMemoryµs per callsceKernelAllocateDirectMemoryms per framesceKernelReleaseDirectMemorycallssceKernelReleaseDirectMemoryµs per callsceKernelReleaseDirectMemoryms per frameCounters come from a local probe build that is not part of this PR (
review-probe-nr21.patchin the evidence archive): the same 13 counters and timers were inserted into maincd700fe9ecand into this PR, anchored on function definitions present in both trees, and the captures above ran on those builds (their summary lines name the probe commits). Values are per flip over the same 8-second window as the trace, 20 s after the first flip; main is the median of its two runs. The allocation itself is still dominated by the free-range search (#7469 addresses that).Reproduction evidence
nr21-sections-evidence.zip (traces, summary lines,
trace_digest.pyandperformance_compare.pyoutput, loaded-module hashes), the counters where-nr21.json and where-nr21-pair.json, and the probe review-probe-nr21.patch.Checklist
maincd700fe9ec; 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 github-actions[bot] on 2026-10-21T03:42:39Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libkernel/DirectMemory/DirectMemory.cppL367-368Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libc/include/GuestArena.hppcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/tests/GuestMemory.cppcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/tests/GuestMemory.cppdocs/dev/ENVIRONMENT.mdcore/libs/tests/GuestMemory.cppdocs/dev/ENVIRONMENT.mdcore/libs/tests/GuestMemory.cppcore/libs/prx/libc/src/GuestArena.cppcore/libs/tests/GuestMemory.cppdocs/dev/ENVIRONMENT.mdcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/tests/GuestMemory.cppdocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/tests/GuestMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppdocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mdImported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T07:44:24Z.
Independent Windows run of this PR (data only).
Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against
mainis clean. I built onlyguest_memory_tests;ctest -N -R guest_memorylists exactly one test.Results (3 runs each):
guest_memorypasses 3/3.core/libs/prx/libc/include/GuestArena.hpp,core/libs/prx/libc/include/WindowsMappings.hpp,core/libs/prx/libc/src/GuestArena.cpp,core/libs/prx/libkernel/DirectMemory/DirectMemory.cppwith the version from the PR's merge base (cd700fe9ec), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards.Not run: other platforms, the other tests in the same family, the full repository CTest, and any title or console run.
AI-assisted (Claude Code).