fix(shader): memory amplification in ShaderDiskCache and quadratic scans in GraphBuilder and InstructionTranslator #1523
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1523"
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 silentone12725 on 2026-10-08T11:03:15Z.
What
Three fixes in the shader recompiler:
ShaderDiskCache — memory amplification from wrong List minimum
The invocation decoder passed
minimum=8toreader.ListforDescriptorBindingentries. The actual minimum serialised size is 114 bytes (5×uint32 + 11×uint64 list-count headers + 2×bool + 1×uint32 enum). A crafted cache file with a large entry count could cause the list allocator to reserve ~14× the expected memory before decoding a single entry. Fix: pass 114 as the minimum.GraphBuilder — O(K×N) scan in resolveJumpTable / resolveDwordJumpTable
Both functions scanned all N program instructions to find any direct branch whose target falls in a given range, called once per indirect-branch block. With K indirect branches the total work was O(K×N). Fix: build a sorted
std::vectorof direct-branch targets once inBuild(shared withsplitIntoBlocksandlinkBlocks, next to the swappc call analysis), then usestd::upper_boundfor an O(log N) check per call.InstructionTranslator — O(N×M) loop via isCodeTableLoad
isCodeTableLoadcalledstd::findovercfg.codeTableLoadProgramCounters(M entries) at every one of N instructions, in two loops, and the second loop searchedcfg.codeTableLoadsagain for each hit. Fix: build anstd::unordered_setand anstd::unordered_mapfrom the cfg data once before the loops, and remove the now-unusedisCodeTableLoad.Tested
shader_recompiler_agc_driverwith submodules;agc_shader_control_flow_tests,agc_shader_memory_tests,agc_shader_fusion_testsandagc_shader_disk_cache_testspassedpython3 tools/check_conventions.py --base origin/main)guestDescriptor(8), two bools (2), the shape enum (4), nine flag lists (72) andimageSamplers(8) is 114.g++ -O2, best of 5; not a measurement of a whole shader compile): theGraphBuilderrange check takes 8.2 ms against 0.11 ms for 100 000 instructions and 100 indirect jumps, and 77 ms against 0.16 ms at 1 000 jumps. TheInstructionTranslatorlookup takes 4.1 ms against 0.46 ms for 100 000 instructions and 256 table loads. With 16 table loads the set is no faster (0.28 ms against 0.34 ms), so the gain only shows on programs with many jump tables.eb4bb29b: passed for Linux, Windows, and the relinker tests on Ubuntu (g++), macOS (clang++) and Windows (g++) (run 37835318482). It is not in the PR checks because the upstream run for this head is waiting for maintainer approval (#1631).Changes since the first revision
main. Conflicts inGraphBuilder(the new swappc call analysis) andShaderDiskCache(bindingsremoved from the artifact and allocation structs upstream) are resolved; the 114-byte minimum now applies toinvocation.bindings, the only remaining caller.UpdateStampearly return is reverted: theerror_codeoverload oflast_write_timedoes not throw, and the early return would skip stale-cache and.tmpcleanup when the stamp cannot be written.isCodeTableLoadis removed, as nothing calls it any more.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T11:16:00Z.
Review: COMMENTED
Good performance and correctness improvements across three areas. The fixes are well-justified with clear explanations of the problems (memory amplification, O(n²) scans, out-of-bounds read).
Strengths:
Minor notes:
This looks ready to merge once rebased and CI passes.
Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T11:22:29Z.
Review: APPROVED
Excellent performance and correctness fixes across three areas of the shader recompiler. This PR addresses real issues that could impact both memory usage and compilation speed.
Strengths:
reader.Listfor DescriptorBinding entriesstd::upper_bound- this will significantly improve compilation time for shaders with many indirect branchesTechnical notes:
Build()and passing it through is clean and efficientThese optimizations will improve shader compilation performance without changing behavior. Ready to merge.
Imported from boykopovar/AnyPS5. Originally posted by silentone12725 on 2026-10-08T13:29:28Z.
Hi @klosixgaming-arch, the branch has been rebased onto main and conventions checks pass. Would you be able to merge this when you get a chance? Thanks!
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T08:56:17Z.
Closing: these are three unrelated changes, and none shows a need. The
Listminimum only matters for a crafted local cache file, and the speedups are measured on isolated loops rather than a shader compile (#218). If a title's shader compile is slow because of one of them, please reopen that one on its own with before/after numbers.Pull request closed