perf(agc): avoid heap allocations and atomic refcounts in draw resource key memo #27273
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/27273"
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 birdmarty on 2026-11-25T17:32:03Z.
What
On every keyed draw,
planDrawconstructed a freshResourceCache::Key(std::vector<std::uint32_t>), copied it whenearlyKeywas present, and insideDrawResourceKeyloadedshader.program->drawKeyMemo.wordsthroughAtomicSharedPtr(atomic reference-count increment/decrement on every shader stage of every draw), while also heap-allocating astd::shared_ptr<std::vector<std::uint32_t>>on the very first draw of one-shot shader variants.Changes
core/shader/recompiler/Recompiler.hpp: add an atomic raw pointerResultKeyMemo::rawalongsidewordsso readers can load the memoized words without atomic refcount churn whilewordsretains ownership for the lifetime of theRecompileResult.core/shader/recompiler/ShaderDiskCache.cpp: update the#if defined(__cpp_lib_atomic_shared_ptr)static_assert(sizeof(RecompileResult) == 568)check for the 8-byteResultKeyMemo::rawmember and resetresult.drawKeyMemo = {}inDecodeResultso decoding into a reusedRecompileResultclears bothwordsandraw(the memo is derived runtime cache and is not serialized).core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp: populate bothdrawKeyMemo.wordsanddrawKeyMemo.rawinverifyResultRoundTrip()beforeEncodeResultand onmemoizedDecodedbeforeDecodeResult, verifying neither is encoded and both are cleared on decode.core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp:FillDrawResourceKey(..., ResourceCache::Key& key)writing into a reusable buffer, and use a two-stepseenOncesentinel onmemo.rawso one-shot shader variants do not heap-allocate ashared_ptr<vector>until seen a second time.const ResourceCache::Key* contentKeyonDrawPlanandResolvedResources, pointing directly atearlyKeywhen supplied or at a thread-local reusablethreadPlanContentKey()buffer otherwise (avoiding per-draw vector allocations and copies while keepingAPS5_VERIFY_DRAW_PLANintact via an explicitscratchKeyparameter).Tested
Windows 11 Pro (build 26300), i9-9900K, MinGW-w64 GCC 15.3.0, Release build, AMD Radeon RX 6800 (
vendor=0x1002,device=0x73bf), AMD proprietary driver26.10.1(Windows driver32.0.21045.16010, Vulkan1.4.315).Unit tests on
f0d7da4003(follow-up on98bcfa54ae, baseorigin/mainc032ba7c52) compared againstorigin/main0af6efad52withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(Vulkan SDK1.4.363.0),VK_LOADER_DEBUG=layer:ctest -R "^(agc_shader_disk_cache|agc_driver_target_memo(_plan_verify)?)$": 3 / 3 passed on bothorigin/main0af6efad52andf0d7da4003(includingAPS5_VERIFY_DRAW_PLAN),Insert instance layer "VK_LAYER_KHRONOS_validation"confirmed 2 times (the two Vulkan tests) on both, 0VUID-errors.ShaderDiskCache.cpponly):agc_shader_disk_cachefails witha decoded result has a draw key memo; passes withf0d7da4003.sizeof(RecompileResult) == 568under#if defined(__cpp_lib_atomic_shared_ptr)(__cpp_lib_atomic_shared_ptr == 201711L) with GCC 15.3.0 (-fsyntax-only).tools/check_conventions.py --base origin/main: passed.tools/debt_fold.py --check: passed.Lies of P (
PPSA10621,v01.012.000,3200x1800, Krat Central Station Plaza,~1,688draws/frame):61250ca018):10.83 → 10.97 → 11.25 FPS(p50 91.0 → 87.9 ms,gpu 62.9 → 58.4 ms/f), 0 mismatches across 243,421 live cross-checks (98bcfa54ae;f0d7da4003changes onlyShaderDiskCache.cppand its test).Performance captures (
APS5_TRACE_SECONDS=120:30, clean warm shader cache, no extraAPS5_TRACE_*orAPS5_PROFILE_*switches)Same machine, title (
PPSA10621,v01.012.000,3200x1800, Krat Central Station Plaza), and flags (APS5_DRAW_PIPELINE=1 APS5_NO_DRAW_STAGING=1 APS5_PIPELINE_DISPATCH=1 APS5_PIPELINE_DISPATCH_MISSES=1 APS5_FAST_F32=1):main,c032ba7c52):9.00 FPS,p50 110.5 ms,p90 117.7 ms,max 151.1 ms,gpu 70.31 ms/f,Graphics.Draw 72.33 ms/f(aps5-trace-c032ba7c5273-23148.json.gz).98bcfa54ae):8.69 FPS(-0.31),p50 111.7 ms(+1.2 ms),p90 127.3 ms(+9.6 ms),max 201.8 ms,gpu 72.22 ms/f(+1.91 ms/f),Graphics.Draw 74.62 ms/f(+2.29 ms/f) (aps5-trace-98bcfa54aede-35264.json.gz).Baseline
aps5-summary.jsonl(c032ba7c52):Candidate
aps5-summary.jsonl(98bcfa54ae):Series context & interleaved ablation
This PR is part of a general AGC/libc performance series measured on Lies of P (
PPSA10621,v01.012.000,3200x1800, UE4) covering #27249, #27250, #27251, #27252, #27253, #27254, #27268, #27269, #27270, #27271, and #27273 (tested together with #20130, #18757, #26778, #27117, and now-merged #26581 and #26681). Combined on base61250ca018(plus remaining unsplit stack changes), steady-state performance rose from7.25 FPS(p50 135.6 ms,gpu 92.7 ms/f, default settings) to12.15 FPSaverage (12.00/12.30 FPS, peak12.33 FPS,p50 81.5 ms,gpu 57.12 ms/f,+67.6%to+70.1%).Because five of the smaller PRs (#27253, #27254, #27270, #27271, and #27273) looked like they slightly lost average FPS (
-0.03to-0.31 FPS) in single isolated runs onc032ba7c52, we ran an interleaved 4-run ablation (A1 -> B1 -> A2 -> B2,APS5_TRACE_SECONDS=120:30, identical warm shader cache and flags) on the full stack to test whether removing those 5 PRs helped or hurt:61250ca018(WITH all 11 PRs): #27249, #27250, #27251, #27252, #27253, #27254, #27268, #27269, #27270, #27271, #27273 + #20130, #18757, #26778, #27117, #26581, #26681 + remaining unsplit stack changes (per-64 KiB proof/pending tracking and ShapeRecipe).61250ca018MINUS the 5 PRs (#27253, #27254, #27270, #27271, #27273): keeps #27249, #27250, #27251, #27252, #27268, #27269 + #20130, #18757, #26778, #27117, #26581, #26681 + remaining unsplit stack changes.DrawWithRecipe(ms/f)DrawFinish(ms/f)61250ca018, WITH all 11 PRs incl. #27253, #27254, #27270, #27271, #27273)61250ca018, MINUS #27253, #27254, #27270, #27271, #27273)61250ca018, WITH all 11 PRs incl. #27253, #27254, #27270, #27271, #27273)61250ca018, MINUS #27253, #27254, #27270, #27271, #27273)+0.39 FPS(+3.3%)-2.1 ms-4.6 ms-10.5 ms-3.21 ms/f-1.02 ms/f-0.88 ms/fRemoving those 5 PRs hurt every run (
A1 > B1andA2 > B2across all metrics), showing that their single-run isolated dips onc032ba7c52were within±0.25 FPSscene variance while larger bottlenecks (#27268, #27249, #27252, #20130, #18757) still dominated. Individual PR captures above remain isolated againstc032ba7c52.Not tested: Linux, NVIDIA/Intel GPUs.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)main0af6efad52and this change, 0 VUIDsImported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T18:01:24Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the reported performance captures show a regression, which is an optional point to address.
What I ran
Inspected head
98bcfa54aede976e6a4b063f92c5764096d1469busingrepo_read.py. I did not run commands, tests, builds, or a merged-preview check. The author reports passing unit tests and checks; those results were not independently verified.Results
Findings
Blocking
None.
Non-blocking
Not verified
core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp,core/shader/recompiler/Recompiler.hpp, andcore/shader/recompiler/ThreadOwned.hpp, including all changed lines. Excluded by the prepared review: none (files.jsonwas not available; the manifest and review-folder listing show both changed files included).To get this to a good state
Nothing is required. Optional: investigate the reported performance regression and share an updated matched capture.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T10:59:12Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing in the inspected code shows a blocking defect; the review is incomplete because none of the files listed as reviewed in the manifest were read in full.
What I ran
No commands were run. I inspected the prepared diff and targeted ranges through the pinned reader; the author reports tests and performance captures in the PR description, which I did not run.
Results
Findings
Blocking
None found in the inspected ranges.
Non-blocking
None.
Not verified
The full text of all four changed files was not read, so an approval is not justified. The prepared materials
description.md,commits.md,manifest.json, anddiff.patchwere read;files.jsonwas not available. The manifest marks all four changed files as included, but only targeted ranges were read. No tests or performance measurements were run. Linux and NVIDIA/Intel GPU behavior were not tested, per the PR description.To get this to a good state
Read all changed files listed as reviewed in the manifest, then reassess whether a formal approval is supported.