perf(libc): edit the guest allocation registry in place #2668

Closed
ps54all wants to merge 1 commit from upstream-pr/2668 into main
ps54all commented 2026-10-10 10:30:37 +00:00 (Migrated from github.com)

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


What

Every protect, unmap and fixed remap of a guest allocation went through replaceRange, which copied the whole registered-range map (auto replacement = registry().ranges;), edited the copy and swapped it in. GuestAllocationsCovers, Overlaps and RequireAvailable walked the map from its first entry. All of this runs under the registry mutex that every guest map call takes, so each call costs O(registered ranges) and serialises the mapping threads. Titles that remap in small steps (Unreal's binned allocator) register tens of thousands of ranges.

  • replaceRange now returns a RangeEdit (keys to erase, ranges to add) for the affected span only. applyEdit applies it after the caller's apply succeeded, so a throwing apply still leaves the registry untouched, as before.
  • Unmap decides whether a piece is the allocation's last one with allocationRemainsAfter, which looks only inside the allocation's extent, instead of scanning the whole copied map.
  • Covers, Overlaps and RequireAvailable start at the first entry that can overlap (upper_bound, one step back) instead of the first entry.
  • Behaviour is unchanged.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, Release, BUILD_TESTING=ON, on main a29ef592.

  • New guest_memory case CheckRegistryEditsBetweenManyRanges: two adjacent allocations, one split into 32 ranges by protecting every other page; a protect whose apply throws leaves all 32 ranges as they were; an unmap across both allocations reports last == false for both pieces, and unmapping the rest reports last == true for each. The case passes on unmodified main as well, which is the point: it pins the behaviour the rewrite must keep.

  • Cost per call with 20,000 registered one-page ranges (scratch benchmark, not committed: 4,000 protects at spread positions, then 20,000 unmaps; three runs each, all within 2%):

    main this PR
    protect 1,171-1,191 µs 0.8 µs
    unmap 646-648 µs 0.3 µs
  • An earlier measurement on Split Fiction (PPSA08560, Unreal Engine 5) on a local integration branch with the same change: map calls taking 5 ms or more in the first 90 s went from 398 to 77 (scratch timer around DoMapDirect). Not re-measured on this branch.

  • ctest: all 326 agc_ / video_out / guest_video tests passed (run on 6675e60b, before the rebase); on a29ef592, of the other 378, three fail and fail the same way on main on this machine: guest_memory at the existing read-only sceKernelPread check (after the new case, which passes), guest_file_vector (/dev/urandom reads) and guest_sce_net (DNS timeout).

  • tools/check_conventions.py --base origin/main and tools/check_test_registration.py pass.

  • Not tested: Linux, macOS.

Overlap: #1961 (open) also edits replaceRange and Unmap, to carry its GPU-mapping note. A trial merge of #1961 onto this branch merges GuestAllocations.cpp cleanly; only core/libs/tests/GuestMemory.cpp conflicts, where both add test functions next to each other. Whichever lands second needs that one-line rebase; happy to do it.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes (Claude Code). I reviewed and take responsibility for every line.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4810534926 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2668). Originally posted by [ps54all](https://github.com/ps54all) on 2026-10-10T10:30:37Z. --- ### What Every `protect`, `unmap` and fixed remap of a guest allocation went through `replaceRange`, which copied the whole registered-range map (`auto replacement = registry().ranges;`), edited the copy and swapped it in. `GuestAllocationsCovers`, `Overlaps` and `RequireAvailable` walked the map from its first entry. All of this runs under the registry mutex that every guest map call takes, so each call costs O(registered ranges) and serialises the mapping threads. Titles that remap in small steps (Unreal's binned allocator) register tens of thousands of ranges. - `replaceRange` now returns a `RangeEdit` (keys to erase, ranges to add) for the affected span only. `applyEdit` applies it after the caller's `apply` succeeded, so a throwing `apply` still leaves the registry untouched, as before. - `Unmap` decides whether a piece is the allocation's last one with `allocationRemainsAfter`, which looks only inside the allocation's extent, instead of scanning the whole copied map. - `Covers`, `Overlaps` and `RequireAvailable` start at the first entry that can overlap (`upper_bound`, one step back) instead of the first entry. - Behaviour is unchanged. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, Release, `BUILD_TESTING=ON`, on `main` `a29ef592`. - New `guest_memory` case `CheckRegistryEditsBetweenManyRanges`: two adjacent allocations, one split into 32 ranges by protecting every other page; a protect whose `apply` throws leaves all 32 ranges as they were; an unmap across both allocations reports `last == false` for both pieces, and unmapping the rest reports `last == true` for each. The case passes on unmodified `main` as well, which is the point: it pins the behaviour the rewrite must keep. - Cost per call with 20,000 registered one-page ranges (scratch benchmark, not committed: 4,000 protects at spread positions, then 20,000 unmaps; three runs each, all within 2%): | | `main` | this PR | |---|---|---| | protect | 1,171-1,191 µs | 0.8 µs | | unmap | 646-648 µs | 0.3 µs | - An earlier measurement on Split Fiction (PPSA08560, Unreal Engine 5) on a local integration branch with the same change: map calls taking 5 ms or more in the first 90 s went from 398 to 77 (scratch timer around `DoMapDirect`). Not re-measured on this branch. - ctest: all 326 `agc_` / `video_out` / `guest_video` tests passed (run on `6675e60b`, before the rebase); on `a29ef592`, of the other 378, three fail and fail the same way on `main` on this machine: `guest_memory` at the existing read-only `sceKernelPread` check (after the new case, which passes), `guest_file_vector` (`/dev/urandom` reads) and `guest_sce_net` (DNS timeout). - `tools/check_conventions.py --base origin/main` and `tools/check_test_registration.py` pass. - Not tested: Linux, macOS. Overlap: #1961 (open) also edits `replaceRange` and `Unmap`, to carry its GPU-mapping note. A trial merge of #1961 onto this branch merges `GuestAllocations.cpp` cleanly; only `core/libs/tests/GuestMemory.cpp` conflicts, where both add test functions next to each other. Whichever lands second needs that one-line rebase; happy to do it. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes (Claude Code). I reviewed and take responsibility for every line. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-10 10:30:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T10:30:46Z.


Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096583793 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2668#issuecomment-6096583793). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T10:30:46Z. --- Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

Pull request closed

Sign in to join this conversation.
No reviewers
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!2668
No description provided.