From c558edf4e6dedbb9fb0fafd2f28a09040ddaf79f Mon Sep 17 00:00:00 2001 From: mrfelek <17274927+mrfelek@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:09:02 +0100 Subject: [PATCH 1/3] perf(kyty): remember the mapped length of a clamped buffer range until a view change ClampRangeSize walks every 16 KiB page of a partly mapped range for each draw and dispatch that binds it. Each thread now keeps the length it found for a range while the probe and backing epochs are unchanged, behind the temporary switch KYTY_CLAMP_MEMO. --- core/kyty/common/runtimeSwitches.cpp | 1 + core/kyty/common/runtimeSwitches.h | 1 + .../KytyShim/src/MemoryBacking.cpp | 22 +++++++++++++ .../KytyShim/src/MemoryTests.cpp | 32 +++++++++++++++++++ docs/dev/ANYKYTY_SETTINGS.md | 2 +- docs/dev/THIRD_PARTY_KYTY.md | 4 +++ 6 files changed, 61 insertions(+), 1 deletion(-) diff --git a/core/kyty/common/runtimeSwitches.cpp b/core/kyty/common/runtimeSwitches.cpp index bd1ce7f729f..d2cd14e5670 100644 --- a/core/kyty/common/runtimeSwitches.cpp +++ b/core/kyty/common/runtimeSwitches.cpp @@ -25,6 +25,7 @@ struct Entry { constexpr Entry Entries[] = { {"KYTY_GENERIC_COMPUTE_VARIANT", "0", "1"}, + {"KYTY_CLAMP_MEMO", "0", "1"}, }; static_assert(std::size(Entries) == SwitchCount); diff --git a/core/kyty/common/runtimeSwitches.h b/core/kyty/common/runtimeSwitches.h index 4f389a0bd64..607596ce29b 100644 --- a/core/kyty/common/runtimeSwitches.h +++ b/core/kyty/common/runtimeSwitches.h @@ -13,6 +13,7 @@ namespace Common::RuntimeSwitches { enum class Switch : uint32_t { GenericComputeVariant, + ClampMemo, Count }; diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp b/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp index ebcfd3d6bc6..2256cf78f17 100644 --- a/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp +++ b/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp @@ -2,6 +2,7 @@ #include "common/assert.h" #include "common/hackNotice.h" +#include "common/runtimeSwitches.h" #include "MemoryShim.hpp" #include "prx/libc/include/GuestArena.hpp" @@ -921,12 +922,33 @@ bool IsFullyMapped(uint64_t vaddr, uint64_t size) { return Shim::IsBacked(vaddr, size) || MappedPrefix(vaddr, size) == size; } +namespace { + +struct RememberedPrefix { + uint64_t vaddr = 0; + uint64_t size = 0; + uint64_t prefix = 0; + uint64_t probe_epoch = ~uint64_t {0}; + uint64_t backing_epoch = ~uint64_t {0}; +}; + +} + uint64_t ClampRangeSize(uint64_t vaddr, uint64_t size) { if (!ValidRange(vaddr, size)) { Shim::Fail("KytyShim memory: attempted to access invalid address 0x%016" PRIx64 " with size 0x%016" PRIx64 "\n", vaddr, size); } if (Shim::IsBacked(vaddr, size)) return size; + const bool remember = Common::RuntimeSwitches::On(Common::RuntimeSwitches::Switch::ClampMemo); + thread_local std::array remembered {}; + const auto probe_epoch = g_probe_epoch.load(std::memory_order_seq_cst); + const auto backing_epoch = g_backing_epoch.load(std::memory_order_acquire); + auto& slot = remembered[((vaddr >> PageBits) ^ (size * 0x9e3779b97f4a7c15ull >> 56u)) & 255u]; + if (remember && slot.vaddr == vaddr && slot.size == size && slot.probe_epoch == probe_epoch && slot.backing_epoch == backing_epoch) return slot.prefix; const auto clamped = MappedPrefix(vaddr, size); + if (remember && clamped != 0 && g_probe_epoch.load(std::memory_order_seq_cst) == probe_epoch && g_backing_epoch.load(std::memory_order_acquire) == backing_epoch) { + slot = {vaddr, size, clamped, probe_epoch, backing_epoch}; + } if (clamped == size) return size; if (clamped == 0) { Shim::Fail("KytyShim memory: attempted to access invalid address 0x%016" PRIx64 " with size 0x%016" PRIx64 "\n", vaddr, size); diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryTests.cpp b/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryTests.cpp index 999e36e970b..4c5d8e0ce6b 100644 --- a/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryTests.cpp +++ b/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryTests.cpp @@ -1,6 +1,7 @@ #include "kernel/memory.h" #include "MemoryShim.hpp" +#include "common/runtimeSwitches.h" #include "prx/libc/include/General.hpp" #include "prx/libc/include/GuestArena.hpp" #include "prx/libc/include/GuestHeap.hpp" @@ -767,6 +768,36 @@ void ProtectOwned(int protection) { GuestArena::GuestArenaNoteHostProtection_nid_no_patch(g_copyFaults.owned, Page, false); } +void TestClampRememberedUntilViewChange() { + namespace Switches = Common::RuntimeSwitches; + constexpr std::size_t bytes = Page * 4; + constexpr auto offset = 11u; + void* heap = GuestArena::GuestArenaAllocate_nid_postfix(bytes, Page); + Require(heap != nullptr); + const auto address = Address(heap) + offset; + const auto size = bytes - offset; + const auto expect = [&](std::uint64_t mapped) { + for (int i = 0; i < 4; ++i) Require(Memory::ClampRangeSize(address, size) == mapped); + Switches::Assign(Switches::Switch::ClampMemo, false); + Require(Memory::ClampRangeSize(address, size) == mapped); + Switches::Assign(Switches::Switch::ClampMemo, true); + std::uint64_t other = 0; + std::thread([&] { other = Memory::ClampRangeSize(address, size); }).join(); + Require(other == mapped); + }; + GuestArena::GuestArenaCommit_nid_postfix(heap, Page, 0x04, Page); + expect(Page - offset); + GuestArena::GuestArenaCommit_nid_postfix(heap, bytes, 0x04, bytes); + expect(size); + GuestArena::GuestArenaReset_nid_postfix(static_cast(heap) + Page * 2, Page * 2); + expect(Page * 2 - offset); + GuestArena::GuestArenaCommit_nid_postfix(heap, bytes, 0x04, bytes); + expect(size); + GuestArena::GuestArenaReset_nid_postfix(heap, bytes); + GuestArena::GuestArenaRelease_nid_postfix(heap, bytes); + std::printf("clamped range 0x%llx: the remembered mapped length follows every commit and reset, on this thread and another, with and without KYTY_CLAMP_MEMO\n", static_cast(address)); +} + void TestGuestCopyOut() { void* owned = nullptr; void* sealed = nullptr; @@ -1136,6 +1167,7 @@ int main() { TestHeapBlockEvicted(); TestHostView(); TestUnreportedPrivateMemory(); + TestClampRememberedUntilViewChange(); TestGuestCopyOut(); TestNoCrossWaitWithGpuThread(); TestSparseReadsGuestFile(); diff --git a/docs/dev/ANYKYTY_SETTINGS.md b/docs/dev/ANYKYTY_SETTINGS.md index 6d0e6b4ecbb..113452526e4 100644 --- a/docs/dev/ANYKYTY_SETTINGS.md +++ b/docs/dev/ANYKYTY_SETTINGS.md @@ -38,7 +38,7 @@ On by default unless the row says otherwise. ## Comparing two settings in one run `KYTY_AB` alternates two arms inside one session, so both play the same scene. Phase A keeps the listed settings as set, -phase B turns them over, at a flip, between two submissions. Only `KYTY_GENERIC_COMPUTE_VARIANT` can change while the +phase B turns them over, at a flip, between two submissions. Only `KYTY_GENERIC_COMPUTE_VARIANT` and `KYTY_CLAMP_MEMO` can change while the title runs; any other name stops at start with the list ([why](THIRD_PARTY_KYTY.md#details)). | Variable | Values | Effect | diff --git a/docs/dev/THIRD_PARTY_KYTY.md b/docs/dev/THIRD_PARTY_KYTY.md index d80234a81ad..72a23010ae4 100644 --- a/docs/dev/THIRD_PARTY_KYTY.md +++ b/docs/dev/THIRD_PARTY_KYTY.md @@ -243,6 +243,7 @@ This build does not read KytyPS5's `KYTY_PIPELINE_PRECOMPILE`: stopping the game | `KYTY_PIPELINE_CHAIN_THREADS` | whole number, clamped to 1 to 16 | `4` | Threads of the pipeline chain above; a value above 16 runs 16 threads and logs one `[kyty-gpl]` line | perf | all | `51c397b379` | | `KYTY_SHARE_EMPTY_BUFFERS` | `0` is off | on | A shader buffer slot whose V# has `num_records = 0` does not make a shader variant of its own. Its reads return zero and its writes are dropped, as on the console, and that holds for the module built for the slot bound: a raw slot of a compute shader that reads its stride at run time and a scalar slot take the bound form, and a draw or dispatch with such a slot uses a module already built for the other slots when every formatted component it reads gives the same out-of-range value. A module built for such a draw takes the bound form of a slot from a module already built. Manifests recorded before this setting read their empty forms back the same way. `0` builds one variant per combination of empty and bound slots | perf | all | AnyKyty | | `KYTY_GENERIC_COMPUTE_VARIANT` | `0` is off | on | A dispatch of a compute shader that already has a module, whose descriptor tables of images or samplers take a new shape (other slots with several descriptors, more candidates, a new numeric class in the image heap), runs at once on a module already built for a wider shape: the search tables and heap words of the dispatch are copied into that module's layout, and every key reaches the same descriptor, so the result is the same. A module whose pipeline is ready is preferred. When no module can take the shape, the module built is the union of the shapes already built for the shader, with room to grow, instead of the shape of the dispatch alone. `0` builds and waits for one module per shape. `KYTY_AB` can change it at a flip; its arms differ only for shapes first seen during a phase (see [Details](#details)) | perf | all | AnyKyty | +| `KYTY_CLAMP_MEMO` | `0` is off | on | Each thread remembers the mapped length `ClampRangeSize` found for a buffer range, until a view or protection change, instead of walking the range's 16 KiB pages for every draw and dispatch that binds it. `0` walks them every time. `KYTY_AB` can change it at a flip (see [Details](#details)) | perf | all | AnyKyty | | `KYTY_GENERIC_COMPUTE_SPECIALIZE` | `0` is off | on | With `KYTY_GENERIC_COMPUTE_VARIANT`, a dispatch run on a wider module also queues its own module on the background compile threads; once that module and its pipeline are ready, the dispatch uses it instead. `0` keeps the wider module | perf | all | AnyKyty | | `KYTY_PIPELINE_STALL_LOG_MS` | milliseconds; text that is not a number reads as `0`, which logs every one | `100` | Logs a `[kyty-gpl]` line for a pipeline stall or shader translation that takes at least this long. If explicitly set, also logs completed `vkCreateComputePipelines` calls at or above the same threshold, with elapsed time, shader hash, module ID, SPIR-V word count, lookup or create mode, and Vulkan result, and `[kyty-frames]` lines: presentation gaps over 250 ms, and every 10 s the counts of gaps over 50 ms, 250 ms and 1 s. A graphics or compute stall line ends with `why=`, the reason it was not skipped | debug | all | `76827a9140` | | `KYTY_PGO_DUMP_SECONDS` | seconds; `0` or unset is off | off | In an instrumented build (`-DKYTY_PGO_GENERATE=ON`), writes the profile counters every this many seconds, since a converted game does not run its exit handlers | debug | all | `a1b17740ba` | @@ -665,6 +666,9 @@ change: compiled while it is off stays built. Once the scene's shapes are known, both arms run the same modules; only the shapes first met during a phase differ, which its longest frame and its frames of 50 ms or more show. Compare its arms from a cold shader store, from the start of the game, not in a scene played before. +- `KYTY_CLAMP_MEMO`: read once per `ClampRangeSize` call. Both values give the same mapped length: a remembered length is + kept only while the probe and backing epochs that every view and protection change moves are the ones it was found + under, so turning it off or on changes how often the pages are walked, not the answer. Refused, with every name not listed above. A setting that changes how shaders are compiled cannot follow a flip: the modules already built keep their form, and the shader store records the profile once per session, so a phase would mix From 56a8b0fdab0a9342d41d3b2bb363c86be81c6c7c Mon Sep 17 00:00:00 2001 From: mrfelek <17274927+mrfelek@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:44:27 +0100 Subject: [PATCH 2/3] perf(kyty): keep the remembered clamp length without a switch The in-run A/B decided: KYTY_CLAMP_MEMO is removed and ClampRangeSize always remembers the mapped length. --- core/kyty/common/runtimeSwitches.cpp | 1 - core/kyty/common/runtimeSwitches.h | 1 - .../prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp | 6 ++---- core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryTests.cpp | 7 +------ docs/dev/ANYKYTY_SETTINGS.md | 2 +- docs/dev/THIRD_PARTY_KYTY.md | 4 ---- 6 files changed, 4 insertions(+), 17 deletions(-) diff --git a/core/kyty/common/runtimeSwitches.cpp b/core/kyty/common/runtimeSwitches.cpp index d2cd14e5670..bd1ce7f729f 100644 --- a/core/kyty/common/runtimeSwitches.cpp +++ b/core/kyty/common/runtimeSwitches.cpp @@ -25,7 +25,6 @@ struct Entry { constexpr Entry Entries[] = { {"KYTY_GENERIC_COMPUTE_VARIANT", "0", "1"}, - {"KYTY_CLAMP_MEMO", "0", "1"}, }; static_assert(std::size(Entries) == SwitchCount); diff --git a/core/kyty/common/runtimeSwitches.h b/core/kyty/common/runtimeSwitches.h index 607596ce29b..4f389a0bd64 100644 --- a/core/kyty/common/runtimeSwitches.h +++ b/core/kyty/common/runtimeSwitches.h @@ -13,7 +13,6 @@ namespace Common::RuntimeSwitches { enum class Switch : uint32_t { GenericComputeVariant, - ClampMemo, Count }; diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp b/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp index 2256cf78f17..559e0c21ba3 100644 --- a/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp +++ b/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp @@ -2,7 +2,6 @@ #include "common/assert.h" #include "common/hackNotice.h" -#include "common/runtimeSwitches.h" #include "MemoryShim.hpp" #include "prx/libc/include/GuestArena.hpp" @@ -939,14 +938,13 @@ uint64_t ClampRangeSize(uint64_t vaddr, uint64_t size) { Shim::Fail("KytyShim memory: attempted to access invalid address 0x%016" PRIx64 " with size 0x%016" PRIx64 "\n", vaddr, size); } if (Shim::IsBacked(vaddr, size)) return size; - const bool remember = Common::RuntimeSwitches::On(Common::RuntimeSwitches::Switch::ClampMemo); thread_local std::array remembered {}; const auto probe_epoch = g_probe_epoch.load(std::memory_order_seq_cst); const auto backing_epoch = g_backing_epoch.load(std::memory_order_acquire); auto& slot = remembered[((vaddr >> PageBits) ^ (size * 0x9e3779b97f4a7c15ull >> 56u)) & 255u]; - if (remember && slot.vaddr == vaddr && slot.size == size && slot.probe_epoch == probe_epoch && slot.backing_epoch == backing_epoch) return slot.prefix; + if (slot.vaddr == vaddr && slot.size == size && slot.probe_epoch == probe_epoch && slot.backing_epoch == backing_epoch) return slot.prefix; const auto clamped = MappedPrefix(vaddr, size); - if (remember && clamped != 0 && g_probe_epoch.load(std::memory_order_seq_cst) == probe_epoch && g_backing_epoch.load(std::memory_order_acquire) == backing_epoch) { + if (clamped != 0 && g_probe_epoch.load(std::memory_order_seq_cst) == probe_epoch && g_backing_epoch.load(std::memory_order_acquire) == backing_epoch) { slot = {vaddr, size, clamped, probe_epoch, backing_epoch}; } if (clamped == size) return size; diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryTests.cpp b/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryTests.cpp index 4c5d8e0ce6b..70fb74d1612 100644 --- a/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryTests.cpp +++ b/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryTests.cpp @@ -1,7 +1,6 @@ #include "kernel/memory.h" #include "MemoryShim.hpp" -#include "common/runtimeSwitches.h" #include "prx/libc/include/General.hpp" #include "prx/libc/include/GuestArena.hpp" #include "prx/libc/include/GuestHeap.hpp" @@ -769,7 +768,6 @@ void ProtectOwned(int protection) { } void TestClampRememberedUntilViewChange() { - namespace Switches = Common::RuntimeSwitches; constexpr std::size_t bytes = Page * 4; constexpr auto offset = 11u; void* heap = GuestArena::GuestArenaAllocate_nid_postfix(bytes, Page); @@ -778,9 +776,6 @@ void TestClampRememberedUntilViewChange() { const auto size = bytes - offset; const auto expect = [&](std::uint64_t mapped) { for (int i = 0; i < 4; ++i) Require(Memory::ClampRangeSize(address, size) == mapped); - Switches::Assign(Switches::Switch::ClampMemo, false); - Require(Memory::ClampRangeSize(address, size) == mapped); - Switches::Assign(Switches::Switch::ClampMemo, true); std::uint64_t other = 0; std::thread([&] { other = Memory::ClampRangeSize(address, size); }).join(); Require(other == mapped); @@ -795,7 +790,7 @@ void TestClampRememberedUntilViewChange() { expect(size); GuestArena::GuestArenaReset_nid_postfix(heap, bytes); GuestArena::GuestArenaRelease_nid_postfix(heap, bytes); - std::printf("clamped range 0x%llx: the remembered mapped length follows every commit and reset, on this thread and another, with and without KYTY_CLAMP_MEMO\n", static_cast(address)); + std::printf("clamped range 0x%llx: the remembered mapped length follows every commit and reset, on this thread and another\n", static_cast(address)); } void TestGuestCopyOut() { diff --git a/docs/dev/ANYKYTY_SETTINGS.md b/docs/dev/ANYKYTY_SETTINGS.md index 113452526e4..6d0e6b4ecbb 100644 --- a/docs/dev/ANYKYTY_SETTINGS.md +++ b/docs/dev/ANYKYTY_SETTINGS.md @@ -38,7 +38,7 @@ On by default unless the row says otherwise. ## Comparing two settings in one run `KYTY_AB` alternates two arms inside one session, so both play the same scene. Phase A keeps the listed settings as set, -phase B turns them over, at a flip, between two submissions. Only `KYTY_GENERIC_COMPUTE_VARIANT` and `KYTY_CLAMP_MEMO` can change while the +phase B turns them over, at a flip, between two submissions. Only `KYTY_GENERIC_COMPUTE_VARIANT` can change while the title runs; any other name stops at start with the list ([why](THIRD_PARTY_KYTY.md#details)). | Variable | Values | Effect | diff --git a/docs/dev/THIRD_PARTY_KYTY.md b/docs/dev/THIRD_PARTY_KYTY.md index 72a23010ae4..d80234a81ad 100644 --- a/docs/dev/THIRD_PARTY_KYTY.md +++ b/docs/dev/THIRD_PARTY_KYTY.md @@ -243,7 +243,6 @@ This build does not read KytyPS5's `KYTY_PIPELINE_PRECOMPILE`: stopping the game | `KYTY_PIPELINE_CHAIN_THREADS` | whole number, clamped to 1 to 16 | `4` | Threads of the pipeline chain above; a value above 16 runs 16 threads and logs one `[kyty-gpl]` line | perf | all | `51c397b379` | | `KYTY_SHARE_EMPTY_BUFFERS` | `0` is off | on | A shader buffer slot whose V# has `num_records = 0` does not make a shader variant of its own. Its reads return zero and its writes are dropped, as on the console, and that holds for the module built for the slot bound: a raw slot of a compute shader that reads its stride at run time and a scalar slot take the bound form, and a draw or dispatch with such a slot uses a module already built for the other slots when every formatted component it reads gives the same out-of-range value. A module built for such a draw takes the bound form of a slot from a module already built. Manifests recorded before this setting read their empty forms back the same way. `0` builds one variant per combination of empty and bound slots | perf | all | AnyKyty | | `KYTY_GENERIC_COMPUTE_VARIANT` | `0` is off | on | A dispatch of a compute shader that already has a module, whose descriptor tables of images or samplers take a new shape (other slots with several descriptors, more candidates, a new numeric class in the image heap), runs at once on a module already built for a wider shape: the search tables and heap words of the dispatch are copied into that module's layout, and every key reaches the same descriptor, so the result is the same. A module whose pipeline is ready is preferred. When no module can take the shape, the module built is the union of the shapes already built for the shader, with room to grow, instead of the shape of the dispatch alone. `0` builds and waits for one module per shape. `KYTY_AB` can change it at a flip; its arms differ only for shapes first seen during a phase (see [Details](#details)) | perf | all | AnyKyty | -| `KYTY_CLAMP_MEMO` | `0` is off | on | Each thread remembers the mapped length `ClampRangeSize` found for a buffer range, until a view or protection change, instead of walking the range's 16 KiB pages for every draw and dispatch that binds it. `0` walks them every time. `KYTY_AB` can change it at a flip (see [Details](#details)) | perf | all | AnyKyty | | `KYTY_GENERIC_COMPUTE_SPECIALIZE` | `0` is off | on | With `KYTY_GENERIC_COMPUTE_VARIANT`, a dispatch run on a wider module also queues its own module on the background compile threads; once that module and its pipeline are ready, the dispatch uses it instead. `0` keeps the wider module | perf | all | AnyKyty | | `KYTY_PIPELINE_STALL_LOG_MS` | milliseconds; text that is not a number reads as `0`, which logs every one | `100` | Logs a `[kyty-gpl]` line for a pipeline stall or shader translation that takes at least this long. If explicitly set, also logs completed `vkCreateComputePipelines` calls at or above the same threshold, with elapsed time, shader hash, module ID, SPIR-V word count, lookup or create mode, and Vulkan result, and `[kyty-frames]` lines: presentation gaps over 250 ms, and every 10 s the counts of gaps over 50 ms, 250 ms and 1 s. A graphics or compute stall line ends with `why=`, the reason it was not skipped | debug | all | `76827a9140` | | `KYTY_PGO_DUMP_SECONDS` | seconds; `0` or unset is off | off | In an instrumented build (`-DKYTY_PGO_GENERATE=ON`), writes the profile counters every this many seconds, since a converted game does not run its exit handlers | debug | all | `a1b17740ba` | @@ -666,9 +665,6 @@ change: compiled while it is off stays built. Once the scene's shapes are known, both arms run the same modules; only the shapes first met during a phase differ, which its longest frame and its frames of 50 ms or more show. Compare its arms from a cold shader store, from the start of the game, not in a scene played before. -- `KYTY_CLAMP_MEMO`: read once per `ClampRangeSize` call. Both values give the same mapped length: a remembered length is - kept only while the probe and backing epochs that every view and protection change moves are the ones it was found - under, so turning it off or on changes how often the pages are walked, not the answer. Refused, with every name not listed above. A setting that changes how shaders are compiled cannot follow a flip: the modules already built keep their form, and the shader store records the profile once per session, so a phase would mix From 73b5cceff2a67818898a452914724e3fc07d53fa Mon Sep 17 00:00:00 2001 From: mrfelek <17274927+mrfelek@users.noreply.github.com> Date: Thu, 26 Nov 2026 19:19:08 +0100 Subject: [PATCH 3/3] perf(kyty): look up the remembered clamp length before checking the range is backed IsBacked walks every 16 KiB page of a range up to its first unbacked page, so for a partly mapped range it walks the whole mapped prefix on each call, before the remembered length is looked up. Look it up first; a range's backing changes only together with the backing epoch the remembered length is checked against. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EqZu9J6tUMvmRP7RkxVCQr --- core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp b/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp index 559e0c21ba3..f529caf87b5 100644 --- a/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp +++ b/core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp @@ -937,12 +937,12 @@ uint64_t ClampRangeSize(uint64_t vaddr, uint64_t size) { if (!ValidRange(vaddr, size)) { Shim::Fail("KytyShim memory: attempted to access invalid address 0x%016" PRIx64 " with size 0x%016" PRIx64 "\n", vaddr, size); } - if (Shim::IsBacked(vaddr, size)) return size; thread_local std::array remembered {}; const auto probe_epoch = g_probe_epoch.load(std::memory_order_seq_cst); const auto backing_epoch = g_backing_epoch.load(std::memory_order_acquire); auto& slot = remembered[((vaddr >> PageBits) ^ (size * 0x9e3779b97f4a7c15ull >> 56u)) & 255u]; if (slot.vaddr == vaddr && slot.size == size && slot.probe_epoch == probe_epoch && slot.backing_epoch == backing_epoch) return slot.prefix; + if (Shim::IsBacked(vaddr, size)) return size; const auto clamped = MappedPrefix(vaddr, size); if (clamped != 0 && g_probe_epoch.load(std::memory_order_seq_cst) == probe_epoch && g_backing_epoch.load(std::memory_order_acquire) == backing_epoch) { slot = {vaddr, size, clamped, probe_epoch, backing_epoch};