libc: GuestAllocations protect/unmap cost grows with the number of registered ranges #2688

Closed
opened 2026-10-10 10:56:08 +00:00 by ps54all · 6 comments
ps54all commented 2026-10-10 10:56:08 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by ps54all on 2026-10-10T10:56:08Z.


Every protect, unmap and fixed remap of a guest allocation goes through replaceRange, which copies the whole registered-range map, and Covers/Overlaps/RequireAvailable scan 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, on main a29ef592): 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 new guest_memory case pins it (it passes on main and 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5790568509 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2688). Originally posted by [ps54all](https://github.com/ps54all) on 2026-10-10T10:56:08Z. --- Every `protect`, `unmap` and fixed remap of a guest allocation goes through `replaceRange`, which copies the whole registered-range map, and `Covers`/`Overlaps`/`RequireAvailable` scan 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`](https://github.com/ps54all/AnyPS5/tree/perf/libc-registry-inplace) (one commit, `a8cacb2f`, on `main` `a29ef592`): 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 new `guest_memory` case pins it (it passes on `main` and 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.
MarcZX10 commented 2026-10-10 12:16:09 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T12:16:09Z.


I reviewed #2668's proposed in-place edit against current main and the relevant callers. I found one exception-safety gap to resolve before carrying it forward.

In the old code, replaceRange allocates 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, replaceRange prepares RangeEdit, the callback changes the mapping/protection, and then applyEdit erases existing registry entries and inserts new ones with ranges[key] = .... Those std::map insertions 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.cpp methods 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6097385903 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2688#issuecomment-6097385903). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-10T12:16:09Z. --- I reviewed #2668's proposed in-place edit against current `main` and the relevant callers. I found one exception-safety gap to resolve before carrying it forward. In the old code, `replaceRange` allocates 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, `replaceRange` prepares `RangeEdit`, the callback changes the mapping/protection, and then `applyEdit` erases existing registry entries and inserts new ones with `ranges[key] = ...`. Those `std::map` insertions 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.cpp` methods 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.
SCD28 commented 2026-10-10 12:57:00 +00:00 (Migrated from github.com)
Author
Contributor

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::map insertion throws std::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):

  • Negative regression test fails on the original in-place implementation and passes with the fix.
  • All 6 focused tests pass.
  • Conventions and git diff --check pass.
  • Synthetic benchmark (20,000 ranges, 5 runs): protect 0.610 → 0.485 µs/call; unmap 0.118 → 0.115 µs/call.

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6097734227 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2688#issuecomment-6097734227). Originally posted by [SCD28](https://github.com/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::map` insertion throws `std::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):** - Negative regression test fails on the original in-place implementation and passes with the fix. - All 6 focused tests pass. - Conventions and `git diff --check` pass. - Synthetic benchmark (20,000 ranges, 5 runs): protect 0.610 → 0.485 µs/call; unmap 0.118 → 0.115 µs/call. 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.
ps54all commented 2026-10-10 14:09:48 +00:00 (Migrated from github.com)
Author
Contributor

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: replaceRange now builds the replacement ranges in a node map before the callback, and applyEdit only erases and splices those nodes in with extract/insert, so no allocation happens after the OS change. Branch perf/libc-registry-inplace, rebased onto main (head 0062e839). @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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6098345346 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2688#issuecomment-6098345346). Originally posted by [ps54all](https://github.com/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: `replaceRange` now builds the replacement ranges in a node map before the callback, and `applyEdit` only erases and splices those nodes in with `extract`/`insert`, so no allocation happens after the OS change. Branch [`perf/libc-registry-inplace`](https://github.com/ps54all/AnyPS5/tree/perf/libc-registry-inplace), rebased onto `main` (head `0062e839`). @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.
SCD28 commented 2026-10-10 14:19:13 +00:00 (Migrated from github.com)
Author
Contributor

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@6e6a5df5fc

You can apply it directly with:

git cherry-pick 6e6a5df5fce146b51345a24dc7de77488fe8868d

The commit only adds GuestAllocationExceptionSafety.cpp and its CMake test registration. It doesn't modify GuestAllocations.cpp.

The test compiles GuestAllocations.cpp directly into a dedicated executable, so the overridden global operator new can inject an allocation failure into the registry without relying on interception inside libc.prx.

Validation:

  • Builds and passes with your node-handle fix.
  • Fails as expected with the previous allocating operator[] implementation (Subprocess aborted).
  • Convention checks pass.

The commit preserves my authorship and is ready to cherry-pick into your branch.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6098434364 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2688#issuecomment-6098434364). Originally posted by [SCD28](https://github.com/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:** https://github.com/SCD28/AnyPS5/commit/6e6a5df5fce146b51345a24dc7de77488fe8868d You can apply it directly with: ```bash git cherry-pick 6e6a5df5fce146b51345a24dc7de77488fe8868d ``` The commit only adds `GuestAllocationExceptionSafety.cpp` and its CMake test registration. It doesn't modify `GuestAllocations.cpp`. The test compiles `GuestAllocations.cpp` directly into a dedicated executable, so the overridden global `operator new` can inject an allocation failure into the registry without relying on interception inside `libc.prx`. **Validation:** - Builds and passes with your node-handle fix. - Fails as expected with the previous allocating `operator[]` implementation (`Subprocess aborted`). - Convention checks pass. The commit preserves my authorship and is ready to cherry-pick into your branch.
MarcZX10 commented 2026-10-10 21:24:43 +00:00 (Migrated from github.com)
Author
Contributor

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6102333390 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2688#issuecomment-6102333390). Originally posted by [MarcZX10](https://github.com/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.
ps54all commented 2026-10-11 07:04:31 +00:00 (Migrated from github.com)
Author
Contributor

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 GuestAllocationExceptionSafety test is worth offering to #1324's author, since the exception-safety case (the allocating operator[] in replaceRange) applies to any in-place rewrite of that function. Our branch perf/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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106500380 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2688#issuecomment-6106500380). Originally posted by [ps54all](https://github.com/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 `GuestAllocationExceptionSafety` test is worth offering to #1324's author, since the exception-safety case (the allocating `operator[]` in `replaceRange`) applies to any in-place rewrite of that function. Our branch `perf/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.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#2688
No description provided.