perf(libc): share one snapshot of the registered guest ranges between leases until the registry changes #4698

Merged
Nesyth merged 1 commit from upstream-pr/4698 into main 2026-10-13 04:53:48 +00:00
Nesyth commented 2026-10-12 19:13:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Nesyth on 2026-10-12T19:13:19Z.


What

GuestAllocationsAcquire and GuestAllocationsAcquireAll built a new vector of shared_ptrs to every registered range on every call, under the registry lock. The driver takes such a lease several times per draw and dispatch: the import lookup in GuestBufferMemory::UploadFinish, HostImportFor when the imports are stale or the range has no import yet, and address-based builds. In Ready or Not (PPSA18605) this was the largest single cost on the GPU packet thread: 7 of 13 gdb samples of that thread were inside GuestAllocationsAcquire, six in its loop over the registry and one in malloc under the heap lock.

  • GuestAllocations::Lease is now a read-only view of an immutable, shared vector of ranges (Lease::Ranges). It keeps the reads its users rely on (iteration, size, empty, operator[], front, back), plus clear, which releases the snapshot. The ranges it holds cannot be modified. Code that narrowed a lease in place now copies the ranges it keeps into a Lease::Ranges.
  • The registry holds a weak reference to the newest snapshot of each kind (readable ranges, all ranges). An acquire made while a lease still holds that snapshot shares it; otherwise it builds a new one. A range's use_count() therefore counts the snapshots that hold it, not the leases: it is above 1 while any lease holds the range, and 1 when none is alive. The pin check in GuestAllocationsRequireUnpinned and failedImportRemapTests rely only on that.
  • A mutation drops the reference at three points: when it starts, after every re-lock in the pin wait of GuestAllocationsRequireUnpinned, and when it ends. The re-lock case matters because a snapshot taken while the wait had released the lock must not outlive the mutation's changes. A lease therefore never shares ranges that a later mutation replaced.
  • As before, a lease pins every range it holds.
  • Tests:
    • leaseSnapshotTests (run from RunGuestAllocationTests) checks three things: two leases taken without a registry change share one snapshot; a lease taken after a mutation gets a new snapshot that includes the new range; and once the leases are gone, no snapshot keeps the range alive, so use_count() is back to 1, the registry's own reference.
    • The lease-wait tests now copy out the ranges they keep instead of erasing from the lease.
  • The comment on the address-space cache in GuestBufferMemory.cpp still lists "the lease's ~1200 shared_ptrs" among the costs of a build. After this change a build shares them instead of copying them. The conventions check doesn't let this PR edit that comment.

Related:

  • #1324 (open) gives HostImportFor (when the imports are not stale) and the UploadFinish lookup a one-range lease instead.
  • #4216 (open) replaces AcquireAll in sceKernelVirtualQuery with GuestAllocationsLookup. Its current head (327e3b810) merges cleanly with this branch.

Overlaps with open PRs:

  • #1324, and #1360, #1361 and #1364, which build on it, return a one-range lease as {range}. With this change they would return Lease(std::make_shared<const Lease::Ranges>(...)) instead.
  • #637 narrows a lease with std::erase_if in a test. It would copy the ranges out instead, as the lease-wait tests here do.
  • Whichever of these lands second adapts.
  • This PR also adds its test at the top of tests/GuestAllocations.cpp, where #1324, #1330, #1360, #1361 and #1364 add theirs, so those get a text conflict there.

Tested

Windows 11 Pro 10.0.22631, AMD Ryzen 7 7800X3D, AMD Radeon RX 7900 XTX, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, on main ffa198407. The branch was rebased onto main d53960612 for this pull request and is not yet rebuilt or re-tested there:

  • ctest: 951/951 passed, as on main (8 tests skip there too).

  • With the snapshot sharing taken out of GuestAllocationsAcquire, leaseSnapshotTests fails with "two leases taken without a registry change do not share one snapshot".

  • tools/check_conventions.py --base origin/main passes.

  • Ready or Not (PPSA18605), standing in the LSPD station lobby with APS5_PROFILE_DRAW=1. One run without this change and one with it, on the same bring-up build otherwise. Each row is the range over that run's 10 s windows; FPS is presents per 10 s divided by 10:

    FPS Draws per 10 s Average draw
    Without 1.8–2.1 26400–29700 206–242 µs
    With, first 3 minutes 1.8–2.9 33200–42400 99–163 µs
    With, after that 3.0–3.3 43200–47200 76–97 µs

    The "without" row leaves out a 70 s dip to 0.9–1.6 FPS. The bring-up build is main bab7b3285 plus #4021, #4057, #1077, #425, #454, #4031, #2254, #3864, #3865, #4012, #4248, #1359 and local changes, all listed in #4184. It was not measured on this branch alone, because Ready or Not needs changes that are not in main to reach that scene.

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
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4833913233 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4698). Originally posted by [Nesyth](https://github.com/Nesyth) on 2026-10-12T19:13:19Z. --- ### What `GuestAllocationsAcquire` and `GuestAllocationsAcquireAll` built a new vector of `shared_ptr`s to every registered range on every call, under the registry lock. The driver takes such a lease several times per draw and dispatch: the import lookup in `GuestBufferMemory::UploadFinish`, `HostImportFor` when the imports are stale or the range has no import yet, and address-based builds. In Ready or Not (PPSA18605) this was the largest single cost on the GPU packet thread: 7 of 13 gdb samples of that thread were inside `GuestAllocationsAcquire`, six in its loop over the registry and one in `malloc` under the heap lock. - `GuestAllocations::Lease` is now a read-only view of an immutable, shared vector of ranges (`Lease::Ranges`). It keeps the reads its users rely on (iteration, `size`, `empty`, `operator[]`, `front`, `back`), plus `clear`, which releases the snapshot. The ranges it holds cannot be modified. Code that narrowed a lease in place now copies the ranges it keeps into a `Lease::Ranges`. - The registry holds a weak reference to the newest snapshot of each kind (readable ranges, all ranges). An acquire made while a lease still holds that snapshot shares it; otherwise it builds a new one. A range's `use_count()` therefore counts the snapshots that hold it, not the leases: it is above 1 while any lease holds the range, and 1 when none is alive. The pin check in `GuestAllocationsRequireUnpinned` and `failedImportRemapTests` rely only on that. - A mutation drops the reference at three points: when it starts, after every re-lock in the pin wait of `GuestAllocationsRequireUnpinned`, and when it ends. The re-lock case matters because a snapshot taken while the wait had released the lock must not outlive the mutation's changes. A lease therefore never shares ranges that a later mutation replaced. - As before, a lease pins every range it holds. - Tests: - `leaseSnapshotTests` (run from `RunGuestAllocationTests`) checks three things: two leases taken without a registry change share one snapshot; a lease taken after a mutation gets a new snapshot that includes the new range; and once the leases are gone, no snapshot keeps the range alive, so `use_count()` is back to 1, the registry's own reference. - The lease-wait tests now copy out the ranges they keep instead of erasing from the lease. - The comment on the address-space cache in `GuestBufferMemory.cpp` still lists "the lease's ~1200 shared_ptrs" among the costs of a build. After this change a build shares them instead of copying them. The conventions check doesn't let this PR edit that comment. Related: - #1324 (open) gives `HostImportFor` (when the imports are not stale) and the `UploadFinish` lookup a one-range lease instead. - #4216 (open) replaces `AcquireAll` in `sceKernelVirtualQuery` with `GuestAllocationsLookup`. Its current head (327e3b810) merges cleanly with this branch. Overlaps with open PRs: - #1324, and #1360, #1361 and #1364, which build on it, return a one-range lease as `{range}`. With this change they would return `Lease(std::make_shared<const Lease::Ranges>(...))` instead. - #637 narrows a lease with `std::erase_if` in a test. It would copy the ranges out instead, as the lease-wait tests here do. - Whichever of these lands second adapts. - This PR also adds its test at the top of `tests/GuestAllocations.cpp`, where #1324, #1330, #1360, #1361 and #1364 add theirs, so those get a text conflict there. ### Tested Windows 11 Pro 10.0.22631, AMD Ryzen 7 7800X3D, AMD Radeon RX 7900 XTX, WinLibs GCC 15.2.0, Release, `BUILD_TESTING=ON`, on `main` ffa198407. The branch was rebased onto `main` d53960612 for this pull request and is not yet rebuilt or re-tested there: - `ctest`: 951/951 passed, as on `main` (8 tests skip there too). - With the snapshot sharing taken out of `GuestAllocationsAcquire`, `leaseSnapshotTests` fails with "two leases taken without a registry change do not share one snapshot". - `tools/check_conventions.py --base origin/main` passes. - Ready or Not (PPSA18605), standing in the LSPD station lobby with `APS5_PROFILE_DRAW=1`. One run without this change and one with it, on the same bring-up build otherwise. Each row is the range over that run's 10 s windows; FPS is presents per 10 s divided by 10: | | FPS | Draws per 10 s | Average draw | |---|---|---|---| | Without | 1.8–2.1 | 26400–29700 | 206–242 µs | | With, first 3 minutes | 1.8–2.9 | 33200–42400 | 99–163 µs | | With, after that | 3.0–3.3 | 43200–47200 | 76–97 µs | The "without" row leaves out a 70 s dip to 0.9–1.6 FPS. The bring-up build is `main` bab7b3285 plus #4021, #4057, #1077, #425, #454, #4031, #2254, #3864, #3865, #4012, #4248, #1359 and local changes, all listed in #4184. It was not measured on this branch alone, because Ready or Not needs changes that are not in `main` to reach that scene. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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] Unblocks: none - [x] AI-assisted: yes
xXPBSXx commented 2026-10-12 21:47:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T21:47:06Z.


Windows test of #4698 (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER), built on main 44ab0c39e with this PR merged (29ca878ed, merges without conflicts). I could not build on current main: it needs a submodule that isn't checked out here.

  • ninja libs agc_driver_graphics_tests guest_memory_tests builds without errors (all libraries that use Lease compile).
  • agc_driver_graphics (which contains this PR's GuestAllocations.cpp test): 10 of 10 runs passed.
  • guest_memory and guest_memory_pool_stats: passed.

Not tested: no game run and no frame-time comparison. One thing I read in the code and did not measure: the snapshot is dropped in GuestAllocationsBegin (GuestAllocations.cpp:115-116), in GuestAllocationsEnd before the GPU map observer is called (:145-146), and in every round of GuestAllocationsRequireUnpinned (:322-323). A title that maps GPU memory several hundred times per second (Record of Lodoss War shows 130 to 535 maps per second in my logs) takes a new snapshot for the observer's lease after each map, so there the copy still happens once per map. For the draw and dispatch leases between mutations the sharing applies as described.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6133206886 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4698#issuecomment-6133206886). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T21:47:06Z. --- Windows test of #4698 (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER), built on main `44ab0c39e` with this PR merged (`29ca878ed`, merges without conflicts). I could not build on current main: it needs a submodule that isn't checked out here. - `ninja libs agc_driver_graphics_tests guest_memory_tests` builds without errors (all libraries that use `Lease` compile). - `agc_driver_graphics` (which contains this PR's `GuestAllocations.cpp` test): 10 of 10 runs passed. - `guest_memory` and `guest_memory_pool_stats`: passed. Not tested: no game run and no frame-time comparison. One thing I read in the code and did not measure: the snapshot is dropped in `GuestAllocationsBegin` (`GuestAllocations.cpp:115-116`), in `GuestAllocationsEnd` before the GPU map observer is called (`:145-146`), and in every round of `GuestAllocationsRequireUnpinned` (`:322-323`). A title that maps GPU memory several hundred times per second (Record of Lodoss War shows 130 to 535 maps per second in my logs) takes a new snapshot for the observer's lease after each map, so there the copy still happens once per map. For the draw and dispatch leases between mutations the sharing applies as described. AI-assisted (Claude Code).
Nesyth commented 2026-10-12 22:07:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Nesyth on 2026-10-12T22:07:43Z.


Thanks for testing it. Your reading is right: the snapshot only lives between mutations, so a title that maps hundreds of times per second gains less there. It never builds more copies of the range list than before, so nothing gets slower.

About 44ab0c39e - current main needs the new tracy and libusb-cmake submodules.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6133499687 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4698#issuecomment-6133499687). Originally posted by [Nesyth](https://github.com/Nesyth) on 2026-10-12T22:07:43Z. --- Thanks for testing it. Your reading is right: the snapshot only lives between mutations, so a title that maps hundreds of times per second gains less there. It never builds more copies of the range list than before, so nothing gets slower. About `44ab0c39e` - current main needs the new `tracy` and `libusb-cmake` submodules.
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!4698
No description provided.