perf(shader): cut hot-path overhead in recompile cache lookups #218
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/218"
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 slick-daddy on 2026-10-01T16:10:21Z.
What
Two behaviour-preserving changes to the recompile cache, which is rebuilt on every dispatch and draw. Cache-key strings are packed eight bytes per word instead of one word per byte, and the key vectors reserve their working size up front so repeated builds stop regrowing. materializeMemoized records whether it hit in a flag rather than looking memoIndex up a second time to decide whether to insert.
snapshotHash is main's one-dword-per-round hash (the packed two-dwords-per-round variants are dropped: any full-width round input lets the next input cancel a state difference, and the rotl variant collided deterministically on adjacent-dword bit flips). snapshotHash is exposed for tests, with a regression test pinning top-bit coverage per snapshot location plus the reported cancelling pair.
The key-layout change alters the bytes written into the on-disk shader cache key, but SourceVersion hashes CacheKey.hpp, so this PR invalidates existing cache entries either way.
Tested
Key numbers carried over from the prior harness run (key code unchanged since, comments only); the snapshotHash rows are gone with the revert.
Windows 11, Release, MinGW-w64 GCC 15.2.0 (WinLibs x86_64-ucrt-posix-seh, 15.2.0posix-14.0.0-ucrt-r7) - the toolchain and configuration the CI Windows job uses.
memoIndex: one hash lookup saved (~10 ns), below that rig's noise floor; same three outcomes as main in all cases.
No local toolchain in this environment (no cmake/ninja/compilers), so agc_shader_memory_tests was not run locally; a Python FNV model confirmed the reported pair is distinct on the restored hash and collides on the dropped rotl variant. CI conventions and report jobs pass; linux/windows builds pending.
Not tested on Linux locally, and no game title or homebrew was run.
Checklist
Imported from boykopovar/AnyPS5. Originally posted by boykopovar on 2026-10-01T18:41:48Z.
Could you measure the speed before and after optimization? The changes seem logical to me, but I need to check.
Imported from boykopovar/AnyPS5. Originally posted by slick-daddy on 2026-10-01T19:55:06Z.
Sure. Here are the results run by AI.
Windows 11, MSVC 19.44 x64, /O2, /std:c++20, 200k iterations after warmup.
snapshotHash— light, no vertexsnapshotHash— light, 12 vertex resourcessnapshotHash— heavy, no vertexsnapshotHash— heavy, 12 vertex resourcesRecompileCacheKey::BuildContextHashvariantIndex— 1 variantvariantIndex— 8 variantsvariantIndex— 32 variantsvariantIndex— 64 variantsvariantIndex— 128 variantsvariantIndex— 256 variants_visitingunordered_set— depth 2_visitingunordered_set— depth 4_visitingunordered_set— depth 8_visitingunordered_set— depth 16_visitingunordered_set— depth 32_visitingunordered_set— depth 64Verdict per commit
724a012snapshotHashtwo dwords per FNV roundd7df0e3CacheKeyreserve + 8 bytes per word packingc208992memoIndexlookupa0b60e4_visiting→std::unordered_set91a42b7variantIndexspecialization hashWhy
_visitingregressesstd::vector+std::findbeats the hash set at every depth measured. The cost is allocation, notasymptotics: an
unordered_setallocates a bucket array plus one node per insert, andEvaluator::EvaluateWideruns per SRT slot, so that allocation is paid on the hot path repeatedly.It would only win at recursion depths that do not occur in practice.
Why
variantIndexregressesvariantKeyhashes every buffer and image field through FNV — roughly 90 dependent multiply rounds.That serial dependency chain costs more than a linear scan of 32 cached variants with early-exit
comparisons. The scan only loses past ~150 variants, and a shader holding 150+ distinct
specializations is unusual.
If you want I can drop the last two commits as table suggested.
Imported from boykopovar/AnyPS5. Originally posted by boykopovar on 2026-10-02T05:27:10Z.
Yes, let's remove the changes that caused the slowdown and keep the ones that gave the acceleration. And after that, it is advisable to take measurements again, just in case.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-03T06:28:31Z.
A note for the merge order: my #415 replaces
_cachein SrtEvaluator.hpp with a flat table, next to_visiting. The two PRs conflict only through a0b60e4d (theunordered_setchange). With that commit dropped as discussed above, they merge cleanly in either order.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:39:06Z.
Following up on boykopovar's note: since #415 merged, this conflicts in
SrtEvaluator.hpp, and the conflict is exactly theunordered_setcommit (a0b60e4d) that measured slower, so dropping it fixes both. Could you also remove the three code comments (CacheKey.hpp and Recompiler.cpp), since the repo allows comments only for technical debt, and post the new before and after numbers for what remains? Then it looks good to me.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:37:44Z.
Thanks for removing the comments. The last commit (
eaf02e38, theunordered_setfor_visiting) is back in, though, and the description says it was restored on purpose. Your own numbers from 1 October show it 4 to 8 times slower at every depth measured (39 ns to 163 ns at depth 2, 217 ns to 1890 ns at depth 32), and @boykopovar asked to drop the changes that slowed things down. Could you drop that commit again and update the description, then post the before and after numbers for the three that remain (snapshotHash, the key packing and the memoIndex lookup), as he asked? With that it looks good to me.Imported from boykopovar/AnyPS5. Originally posted by slick-daddy on 2026-10-05T15:46:59Z.
Done.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T23:37:31Z.
Thanks for dropping it and for the new numbers. I checked what's left.
_visitingis unchanged againstmain, thememoIndexchange behaves the same in all three cases, and the key packing is unambiguous because the length still comes first. A quick GCC-O2microbenchmark here agrees with your direction: about -40% forsnapshotHashand -25% for the key build.There's one problem with the two-dwords-per-round
snapshotHash. FNV's multiply only carries upward, so the odd dword of each pair, which goes into bits 32..63, never reaches the low half of the hash. A change confined to the top bits of odd dwords therefore stays in the top bits of the hash, and two such changes can cancel. For example, flipping bit 31 of dword 1 and of dword 3 of the same buffer gives the samesnapshotHash. Onmainthe two hashes differ:A single bit-31 flip changes only hash bit 63.
materializeMemoizedtrustsvariantIdplus this hash with no further comparison, so such a pair would return a result materialized from the other snapshot's descriptor, SRT or user-data words. That's silent wrong rendering rather than a slowdown. It's unlikely for real descriptors, but the memo runs on every draw.A rotate after the multiply,
hash ^= value; hash *= 0x100000001b3ull; hash = std::rotl(hash, 31);, carries the high half back down. It removes the cancellation above and kept most of the gain here (about -25% instead of -40%). A finalhash ^= hash >> 32per round fixes it as well, but costs almost all the gain. Keeping one dword per round is also fine. Whichever you prefer, a small test with two snapshots that differ only in the top bits of odd dwords would pin it down.Minor: the table in the description still lists the
variantIndexrows, which are no longer part of this PR.Imported from boykopovar/AnyPS5. Originally posted by slick-daddy on 2026-10-06T12:12:32Z.
Fixed in
77d6d681.You were right, and the effect is stronger than a cancellation risk: a multiply only carries upward, so the low 32 bits of the hash never saw the odd dword at all. Flipping bit 31 of any odd dword left the low half bit-identical, and the pairs I found cancel outright.
Took the rotate, but only on the packed rounds. Putting it in the shared
mixcost the vertex family, whose V# fields are mixed one per round and gained nothing from packing:mixmixPairmixPairis used for the packed loop only; the odd tail dword and the single-value rounds stay plain FNV. New numbers are in the description, and thevariantIndexrows are gone.snapshotHashmoved out of the anonymous namespace and is declared inRecompiler.hppnext toResourceCapture, soagc_shader_memory_testscan call it. The newverifySnapshotHashflips bit 31 of an odd dword in the descriptor, the flattened SRT and the user data and requires both the hash and its low half to move. Confirmed it fails on the pre-fix function with"a top bit of an odd dword left the memo key's low half unchanged", and the full 288-test suite passes with the fix.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T15:35:35Z.
Review: CHANGES_REQUESTED
Thanks for the numbers. The key packing and the
memoIndexchange check out (length first, same three outcomes asmain). One blocker in the packedsnapshotHash, plus two smaller points:mixPaircollides deterministically (Recompiler.cpp,mixPair/mixWords/mixDescriptors). Flipping bit 31 of an odd dword flips bit 63 of the packed input, i.e. adds 2^63; times the odd FNV prime that is still exactly 2^63, androtl(…, 31)turns it into bit 30 alone. Flipping bit 30 of the next dword then cancels it, and the rest of the hash is identical. Example: user data{0x3f800000, 0x3f800000, 0x3f800000, 0x3f800000}(1, 1, 1, 1) and{0x3f800000, 0xbf800000, 0x7f800000, 0x3f800000}(1, -1, inf, 1) give the same hash. With random words it collides 100000 times out of 100000;mainhashes all of these apart. The same holds in buffer/image/sampler words and the flattened SRT. Root cause: once each round's input is as wide as the state, any state difference can be cancelled by the next input, and FNV's multiply leaves differences in the top bits as they are.variantIdandhash(materializeMemoized, both lookups), so a collision silently returns a result materialized for another snapshot (other descriptor words or user data baked in). A wrong result wheremainwas right can't go in, even for a speed-up.Possible directions, your call: keep one dword per round (
main's hash) and take the speed-up only from the key changes; or make a memo hit also compare the snapshot words (store them or a second, independent hash), so collisions can't return wrong data whatever hash is used. A stronger mixer per pair only makes the cancellation data-dependent; it doesn't remove it.ShaderMemory.cpp,verifySnapshotHash): each case flips the buffer, SRT and user-data dword together, so if one of the three paths were wrong, the other two would still change the hash and the test would pass. Flip one location per case, and add the colliding pair above (bit 31 of dword2k+1together with bit 30 of dword2k+2) as a row that must hash differently.upstream/mainwithout the merge commit (f1945105) and theeaf02e38+ revert pair.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-07T15:43:56Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppL1527, L1549-1550Imported from boykopovar/AnyPS5. Originally posted by slick-daddy on 2026-10-07T16:03:27Z.
Addressed the blocker in
6740c2c1(branch rebased onto origin/main atcea1ef13):30cb3236memoIndex lookup,dc856da6key packing,6740c2c1test + snapshotHash exposure. The two carried-over subjects were also reworded to Conventional Commits (conventions job passes).Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T08:56:08Z.
Closing: #639 moved the cache key build out of the draw path (draws now use the key built once at registration), so the
RecompileCacheKey::Buildsaving no longer lands per draw, and thememoIndexlookup alone was below noise. If it still shows up in a profile on current main, please reopen with those numbers.Pull request closed