perf(agc): find a range's newest writer through a granule index instead of scanning the ring #12264
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12264"
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 Tehshortbus on 2026-10-28T05:56:32Z.
What
Driver::newestWriterLocked(used byclassifyPendingWritewhen a capture reads memory with pending GPU writes, and by write evidence) walked the 4096-entry written-buffer ring from the newest entry on every lookup, so a range without a recorded writer cost a full walk. In GTA V cutscenes it was the hottest function on the packet thread not touched by an open pull request.The ring is now a
WrittenBufferIndex(Execution/include/Driver/Memory/WrittenBufferIndex.hpp). It keeps the same deque, capacity and eviction order, and also files each entry's position under the 64 KiB granules it covers; entries wider than 16 granules (1 MiB) go on a short list of their own so a large buffer does not make a push expensive.Newest(begin, end)walks only the query's granules and that list, newest first, applies the same overlap test as before and returns the newest match, so it returns the same entry as the walk (an empty entry is filed under the granule of its address, since the old test also matches it). Queries wider than 16 granules, and empty queries, still walk the ring.WrittenBufferandWrittenBufferRingmove into the new header unchanged; the three push sites callPush, anddescribeWritersiteratesEntries().Memory. The entries stay in the same 4096-entry deque as before (64 bytes each, 0.25 MiB). On top of that, the index keeps one
unordered_mapnode with astd::deque<std::uint64_t>for each distinct 64 KiB granule that a non-wide entry covers. With its first 512-byte chunk, that is about 700 bytes per granule on libstdc++. A granule's 65th and later sequences cost 8 bytes each. A non-wide entry covers at most 16 granules, so the index files at most 4096 × 16 = 65,536 granules. Wider entries cost 8 bytes each on their own list. Measured with a countingoperator newaround the class (GCC 16.2.1, libstdc++), as the peak live heap of the class including its entries while pushing three times the ring capacity:So the upper bound is about 44 MiB, against 0.25 MiB for the ring on
main. It is reached only when the last 4096 recorded writes went to 4096 different buffers of about 1 MiB that share no granules. In GTA V (PPSA04264 01.011.001) the index stays far below that. I ran a 307 s session from the title through the save load, the first Story cutscene and 136 s of the second, with temporary counters sampled each second. The sample estimate counts the same allocations as the table and reads within 3 % of it, slightly high, for each row. The ring filled to 4096 entries within 66 s of the first recorded write. The peak was 2,360 granules and 639 wide entries, about 1.9 MiB, while the save loaded. During cutscene 1 the index held 25 to 65 granules (about 0.4 MiB), and during cutscene 2 at most 322 granules (about 0.5 MiB).Tested
CachyOS Linux (kernel 7.3.0-4-cachyos), GCC 16.2.1, Release, NVIDIA GeForce RTX 4070 SUPER (615.78.08).
agc_written_buffer_index(new): pushes 20,000 random entries (empty, small, medium and wider than 1 MiB) into a 64-entry index, so it evicts constantly, and after each push compares 8 random queries (also empty and wider than 16 granules) against the linear walk overEntries(); plus explicit newest-wins and eviction cases. With the wide-entry list left out ofNewest, it fails on the first differing query.Focused tests,
main0cd07dc4aband this change on it, withANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build-tests -V -R '^(agc_driver_copied_writer_copy|agc_driver_copied_writer_label_order|agc_driver_pending_exact_surface|agc_driver_indirect_arguments_pending_producer|agc_driver_gated_dispatch_capture|agc_space_pending_writes|agc_pending_write_snapshot|agc_written_buffer_index)$':main7/7 pass, this change 8/8 pass; noVUID-line and no missing-layer message in either log. Under gdb,WrittenBufferIndex::Pushis reached 65 times inagc_driver_gated_dispatch_captureand 85 times inagc_driver_indirect_arguments_pending_producer, andNewest4 times in the latter; the other tests do not reach the ring, so the lookup's equivalence rests mainly on the new test.GTA V (PPSA04264 01.011.001),
perf record -F 499 -e cpu-clock:u --call-graph dwarfover the first 20 s of the first two Story cutscenes, one run per build, both onmain86fb985abb+ #11580 (which the title needs, #11425) + #12244, the second with this change cherry-picked; same machine, warm shader cache. Share of all samples:newestWriterLocked(with callees)classifyPendingWrite(with callees)WrittenBufferIndex::Push+evictOldestThe saving is about 1 % of the packet thread, below the run-to-run variation of whole-thread and frame-time numbers, so I am not claiming a frame-rate change.
Performance captures (details, alignment and
trace_digest.pyoutput in the comment below; traces attached there):aps5-summary.jsonllines of the 75–105 s window, base9f2d365268then this PRe98000093a:python3 tools/check_conventions.py --base origin/mainpasses.Overlap:
tools/pr_overlap.pyreports #11439, which changes theprogramargument of thewrittenBuffers.push_backcall inBufferCopy.cpp(L131) that this PR turns intowrittenBuffers.Push. Whichever merges second keeps #11439's argument inside the other call; no behaviour overlaps. The 66 pull requests it lists as not compared (they conflict withmain) shareCMakeLists.txtorDriver.hpp; I checked their diffs (#5021 by its file list and itsCMakeLists.txtpatch, since its full diff is too large to fetch): none adds, removes or edits thewrittenBuffersmember,WrittenBuffer,WrittenBufferRingornewestWriterLocked.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T09:07:19Z.
Review: APPROVED
Reviewed the index against the previous reverse linear scan. Each entry is indexed in every 64 KiB granule it overlaps (or the wide-entry list), eviction removes exactly the oldest sequence, and candidates are rechecked with the original half-open overlap predicate before choosing the newest sequence. The randomized differential test also covers empty and wide queries, eviction, and newest-wins behavior. The PR now quantifies index memory and reports paired title captures, trace summaries, same-GPU focused baseline/change tests, no new VUIDs, and validation-layer presence. Hosted Windows and conventions pass; the other product build jobs are skipped by eligibility. The reported modest lookup improvement is scoped appropriately and does not claim a frame-rate win. No remaining blocker found.