perf(libkernel): skip a used run at once when allocating direct memory #23866
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/23866"
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 mariushub on 2026-11-19T20:15:03Z.
What
PhysicalMemoryPool::Allocsearches the direct memory pool first-fit. For each candidate window it first asked whether a release claims the range (DirectMemoryReleaseClaimed, which takesg_directLock; added by #17794), then read the page map, and on a used page moved just past the last used page of the window. Through a long allocated run it therefore advanced by at most the requested length and took the lock at every step. SuperTuxKart (PPSA69421) allocates about 1,000 64 KiB blocks a second above a 1.4 GiB allocated prefix: about 22,000 steps and lock acquisitions per call, a third of its main thread on the kart selection screen.The search now reads the page map first and, on a used page, moves to the first free page after it (
_nextFreePage), never past the search end, so an allocated run costs one step; it asks for release claims only for a window whose pages are all free. An allocation still needs both checks to pass, and only windows that overlap a used page or a claim are skipped, so the first fitting address and every error result are unchanged. Two newMemoryPoolTestcases check this:alloc_matches_a_first_fit_modelcompares 12,000 random allocations, frees and release claims with a model that tries every aligned address, andalloc_skips_a_long_used_runallocates after 16,384 adjacent one-page blocks and next to a used run that reaches the pool end.#7469 sped up the same search by walking the block map and was closed for showing no frame-rate change; a review there measured it 17 times slower with many one-page blocks. This change keeps the page map, so that case gets faster as well (table below). At #7469's time a search over 1.4 GiB cost about 33 us; since #17794 it costs about 320 us, which is why it now shows in the frame time.
Tested
Windows 11 Pro build 26100, AMD Ryzen 7 9800X3D, NVIDIA GeForce RTX 5080 (driver 596.36), WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON.SuperTuxKart (PPSA69421) 00.001.000, catalog release
PPSA69421.zip(SHA-256f2240bf0e67cf585ae7c6acc16068e1b8bef5e3c1b5f7e8ae550053d937b712a), converted with--trap-sdk-syscall-stubs(bring-up discussion #17047). Scene: the story mode kart selection screen, reached within 80 s and left untouched; warm caches; libraries built frommainb979a71704and from this PR's headc8bec67ef4. Captures withAPS5_TRACE_SECONDS=90:10, run one after the other.mainb979a71704c8bec67ef4The 10 s
APS5_TRACE_SUMMARY_EVERYintervals of the same runs agree over the whole stay on that screen: 60 to 70 presents per interval at a p50 of 143 to 164 ms onmain(12 intervals from 30 s on), 99 to 107 presents at a p50 of 94 to 99 ms with this PR (7 intervals from 30 s on).tools/performance_compare.pyPerformance comparison
b979a71704c8bec67ef4Frame and GPU times (ms)
Top phases (ms per frame)
Worker busy time (%)
Graphics track (per present)
The AGC phases per frame barely move; the time comes back on the title's main thread, which the trace does not instrument. With
APS5_SAMPLE_THREADSonmain, 34 % of that thread's samples were undersceKernelAllocateDirectMemory, nearly all inDirectMemoryReleaseClaimedand the mutex calls. Counted with temporary instrumentation ofAlloc(not in this PR), per 5 s on that screen:mainabout 5,100 calls, 115 million loop steps, 1,670 ms inAlloc; this PR about 4,800 calls, 9,700 steps (2 per call), 55 ms.MemoryPool.cppcompiled on its own (-O3) with a claim query that takes astd::mutex, asDirectMemoryReleaseClaimedtakesg_directLock; one 64 KiB allocation and free from search start 0, mean of 2,000:mainEAGAIN)Tests:
MemoryPoolTestpasses, and its new cases also pass againstmain'sMemoryPool.cpp(same addresses and errors), while two deliberately broken variants (skipping one page too far, dropping the claim check) fail them; a separate simulation of both loops over 300,000 random pool states found no difference. The full test suite (6,425 tests, GPU tests on the RTX 5080) fails the same 9 tests on this branch and onmainb979a71704, each also on a second run: three AGC tests already reported for NVIDIA on Windows (#22427, #22931, #23708) and six network tests that time out on this machine.python tools/check_conventions.py --base upstream/mainpasses, andpython tools/pr_overlap.py --branchfinds no overlap with the open pull requests.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 PrivacyOnion on 2026-11-19T20:16:48Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
c8bec67ef4:tools/check_conventions.pypassed.MemoryPoolTestbuilt cleanly.MemoryPoolTest):core/libs/prx/libkernel/DirectMemory/MemoryPool.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T09:46:34Z.
Review: APPROVED
Reviewed — looks good
Nothing in the inspected allocator change shows a verified defect; the search advances past used pages and checks release claims only for candidate windows that are free in the page map.
What I ran
Reviewed head
c8bec67ef49544855d16913ced9d99a98700e4d1using the prepared diff andrepo_read.py. No tests or build commands were run. The performance captures and test results in the PR description are author-reported.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No tests, build, or physical-console behavior were verified in this review. Read:
core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp,core/libs/prx/libkernel/DirectMemory/MemoryPool.hpp, andcore/libs/tests/MemoryPoolTest.cpp; the changed files listed inmanifest.jsonwere both read. No changed files were excluded byfiles.json(which was not available in the review folder).To get this to a good state
Nothing is required.