libc: GuestAllocations protect/unmap cost grows with the number of registered ranges #2688
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 ps54all on 2026-10-10T10:56:08Z.
Every
protect,unmapand fixed remap of a guest allocation goes throughreplaceRange, which copies the whole registered-range map, andCovers/Overlaps/RequireAvailablescan from the first entry, all under the registry mutex every guest map call takes. With 20,000 registered one-page ranges (scratch benchmark, Windows 11, i9-10850K): protect costs about 1,180 µs per call and unmap about 646 µs. Unreal Engine 5 titles register tens of thousands of ranges at start-up; on Split Fiction (PPSA08560) map calls taking 5 ms or more in the first 90 s went from 398 to 77 with the fix (integration branch measurement).Fix:
ps54all/AnyPS5:perf/libc-registry-inplace(one commit,a8cacb2f, onmaina29ef592): edits only the affected span in place and starts the range scans at the first entry that can overlap. Same benchmark: 0.8 µs per protect, 0.3 µs per unmap. Behaviour unchanged; a newguest_memorycase pins it (it passes onmainand on the branch). #1961 touches the same function; a trial merge only conflicts in the test file. Details: #2668 (closed by the intake pause).Happy for anyone with intake access to carry it.
Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T12:16:09Z.
I reviewed #2668's proposed in-place edit against current
mainand the relevant callers. I found one exception-safety gap to resolve before carrying it forward.In the old code,
replaceRangeallocates a complete replacement map before calling the OS operation; if the OS callback throws, the registry is unchanged, and a successful callback is followed by a map swap. In the proposed code,replaceRangepreparesRangeEdit, the callback changes the mapping/protection, and thenapplyEditerases existing registry entries and inserts new ones withranges[key] = .... Thosestd::mapinsertions allocate nodes and can throw (for example,std::bad_alloc). If one throws after the callback succeeded, the OS mapping has changed but the registry may be only partly edited.The added regression covers a callback that throws before
applyEdit, which is useful, but it does not cover allocation failure during the post-callback edit. One way to preserve the old guarantee while avoiding a full registry copy is to allocate the replacement nodes in a temporary map before calling the callback, then transfer those node handles into the registry after success.This is a source-level exception-safety finding, not a reproduced failure. Also, #1961 is still open and edits the same
GuestAllocations.cppmethods for a separate GPU-import change; any future carry of #2668 should be reconciled with that branch rather than submitted as a competing overlapping patch.Imported from boykopovar/AnyPS5. Originally posted by SCD28 on 2026-10-10T12:57:00Z.
I reproduced the exception-safety issue identified by @MarcZX100 in the optimization proposed by @ps54all in #2668.
The regression test uses deterministic allocation-failure injection: the simulated system callback succeeds, but the subsequent
std::mapinsertion throwsstd::bad_alloc, leaving the registry partially modified.I implemented a local fix that prepares replacement map nodes before the callback and transfers them afterward using C++17 node handles (
extract/insert). This avoids allocating new nodes after the system operation succeeds.Validation (Linux/WSL, GCC Release):
git diff --checkpass.The benchmark uses an empty system callback. Windows, macOS and the full test suite remain untested.
This is a complementary fix for the existing optimization, not a competing implementation. I can provide the minimal patch and regression test for integration into @ps54all's branch.
AI-assisted: yes. The implementation and tests were developed with Codex and reviewed locally.
Imported from boykopovar/AnyPS5. Originally posted by ps54all on 2026-10-10T14:09:48Z.
Thanks both, agreed: once the callback has changed the mapping, nothing may allocate. Fixed on the branch the way you suggested:
replaceRangenow builds the replacement ranges in a node map before the callback, andapplyEditonly erases and splices those nodes in withextract/insert, so no allocation happens after the OS change. Branchperf/libc-registry-inplace, rebased ontomain(head0062e839). @SCD28, your allocation-failure injection test is the piece I don't have (the registry lives in libc.prx, so I couldn't inject from the test executable): if you can share it, I'll add it to the branch with your authorship. On #1961: agreed, whoever carries this should rebase it onto #1961 if that lands first; a trial merge earlier only conflicted in the test file.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by SCD28 on 2026-10-10T14:19:13Z.
Thanks @ps54all ! I've extracted the allocation-failure injection test into a standalone commit based directly on your updated branch (
0062e839).Commit:
github.com/SCD28/AnyPS5@6e6a5df5fcYou can apply it directly with:
The commit only adds
GuestAllocationExceptionSafety.cppand its CMake test registration. It doesn't modifyGuestAllocations.cpp.The test compiles
GuestAllocations.cppdirectly into a dedicated executable, so the overridden globaloperator newcan inject an allocation failure into the registry without relying on interception insidelibc.prx.Validation:
operator[]implementation (Subprocess aborted).The commit preserves my authorship and is ready to cherry-pick into your branch.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T21:24:43Z.
Thanks, I applied SCD28's test commit as authored on a local review branch with ps54all's node-handle correction; in the earlier clean Release build based on main
de5e541d, guest_allocation_exception passed 1/1. I have not opened a competing PR: #1324 already covers the overlapping registry performance change and remains open. This test can be offered to #1324's author if they want the exception-safety case there.Imported from boykopovar/AnyPS5. Originally posted by ps54all on 2026-10-11T07:04:31Z.
Thanks both. Understood that #1324 owns the registry performance change and stays open; this issue was only the carry request for our branch, so there is nothing left to do here from our side. SCD28's
GuestAllocationExceptionSafetytest is worth offering to #1324's author, since the exception-safety case (the allocatingoperator[]inreplaceRange) applies to any in-place rewrite of that function. Our branchperf/libc-registry-inplace(0062e839) has the node-handle fix if they want to compare. Closing from my side unless someone prefers it open.Written with AI assistance.