Repository navigation
perf(agc): reuse the depth plane collect token within a draw - #17740
chitters2004 wants to merge 2 commits into
Conversation
A draw over a mirrorable depth surface collected the depth and stencil planes in SyncFromMemory and SyncStencilFromMemory and looked the same span up again in NoteMemory, in the same collect epoch. That second lookup is normally a collect-memo hit: it walks no pages, but it costs a map lookup and an increment of the shared memo-hit counter. The first collect now hands back a token (CollectReceipt: the memoized range, the covered range, the collect epoch, the allocation-mapping generation and the unwatch serial); the sync functions return it and the draw passes it to NoteMemory. GuestMemory::RecollectWrites returns the tracker's published generation only when the token proves the ordinary collector would memo-hit the same request now: same epoch on a thread that bumps it and issued the token, same mapping generation and unwatch serial, and the request inside the token's range, which the epoch map covers. In every other case, including a thread that never bumps its epoch, another thread that adopted the epoch, a refused or partial collection, a thread-ring-only hit, a ring eviction, a mapping or watch change, a shared memo and APS5_NO_COLLECT_MEMO, it calls the ordinary collector. Stores the driver makes between the two points are absorbed exactly as before. Adds CollectReceipt, a CollectWrites overload that fills it, RecollectWrites and the counters CollectMemoHits, CollectWalks and ThreadReceiptReuses. A new debt entry says a note can absorb a write another thread published after the sync. WriteTracking tests cover the token directly; the depth tests count the lookups per draw and check stores between the two points on both planes; focused ctest entries run those under APS5_NO_COLLECT_MEMO, APS5_SHARED_COLLECT_MEMO and APS5_NO_EPOCH_COLLECT_MAP. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
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.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
milescoviello
left a comment
There was a problem hiding this comment.
Read the receipt against the memo hit it replaces.
- Keys: in
collectWrites, a memo hit returnstracker.stablewhenepochCollectMap(epoch, unwatched, mapping).Covers(first, stop).RecollectWritesreturns the sametracker.stablewhen the receipt's epoch, unwatch serial and mapping generation all equal the current ones, and the requested pages lie inside the receipt's[first, stop). Those are the three keys the epoch map is indexed by, and it only ever gains ranges within an epoch, so a valid receipt answers exactly what the map lookup would. - Thread: the issuing-thread check covers the non-shared case, where the memo is per thread. With
sharedCollectMemo()the receipt is never set, so that path is unchanged. - Issue points: the receipt is filled only on a memo hit or after a walk that recorded its range, and is reset at the top of each
collectWrites. A refused or partial walk leaves it empty, which the newCheckCollectReceiptscases pin.
On the evidence, as you note, the GTA V pairs are inside the noise. That includes the phase the change targets: Driver.Draw.prepare is 63.8–67.7 ms on main and 65.0–67.8 ms with this. What the PR shows is the count, about 16,200 fewer collector lookups per pass, each a map lookup that was already a hit. That's a fair cleanup claim, but not a measured speed-up yet. A title whose committer spends visibly in collectWrites, such as a perf/sampler line where its share drops, would make it one.
AI-assisted: yes (Claude Code)
SyncFromMemory keeps main's write-index backing refresh and storing flag and returns the collect receipt that the draw hands to NoteMemory. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Closing this. A microbenchmark shows the token costs more than the lookups it saves.
The second lookup this PR removes is already a 15 ns memo hit on Source, script and runs: https://github.com/chitters2004/AnyPS5/releases/tag/pr-dedupe-t5 ( The per-draw collect and lookup work this touches is better removed as a whole (per-block dirty state and resource versions) than trimmed by one lookup. |
Title: perf(agc): reuse the depth plane collect token within a draw
What
A draw over a mirrorable depth surface collects the depth plane in
SyncFromMemoryand the stencil plane inSyncStencilFromMemory, then looks the same spans up again inNoteMemory, in the same collect epoch. That second lookup is normally a collect-memo hit: it walks no pages, but it costs a map lookup and an increment of the shared memo-hit counter, twice per depth-and-stencil draw.The first collect now hands back a token (
GuestMemory::CollectReceipt: the memoized range, the covered range, the issuing thread, the collect epoch, the allocation-mapping generation and the unwatch serial). The two sync functions return it and the draw passes it toNoteMemory; nothing is stored on the surface.GuestMemory::RecollectWritesreturns the tracker's published generation only when the token proves the ordinary collector would memo-hit the same request now: same epoch on the thread that issued the token and bumps it, same mapping generation and unwatch serial, and the request inside the token's range (the epoch map covers a contained request). Otherwise it calls the ordinary collector, so a thread that never bumps its epoch, another thread that adopted the epoch, a refused or partial collection, a thread-ring-only hit, a ring eviction, a mapping or watch change,APS5_SHARED_COLLECT_MEMO,APS5_NO_EPOCH_COLLECT_MAPandAPS5_NO_COLLECT_MEMOall behave as before. Stores the driver makes between the two points (fast clear, stencil storage take) are absorbed exactly as before. The memo policy stays insideGuestMemory; the graphics code only passes the token on.Not changed:
DepthSurface::CheckCurrent's collects (multisample only,CollectWritesUncachedrace guards), the debt entries (no behaviour change).Tested
Windows, RTX 3080,
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ctest -R 'agc_driver_(depth_|native_depth|native_multisample_depth|graphics$|sample_mask_depth_stencil|gcn_padded_depth)|agc_write_tracking'onmainand on this PR from the same tree (measured on the earlier head; the GTA timing below is on3fb1bd7422over maind03beaf110): 72 of 72 on main, 76 of 76 here (the PR addsagc_driver_depth_surface_reuseand threeagc_driver_depth_store_window_*entries, each about 0.03 s), 0VUID-lines on both, no missing-layer line.Head
9348089692is3fb1bd7422merged withmainf439f7f3a0;SyncFromMemorykeepsmain's backing refresh andstoringflag and returns the collect receipt. The results here are from3fb1bd7422and are not rerun on the merge.New tests:
CheckCollectReceiptsinagc_write_trackingexercises the token directly (a first walk, a block-widened walk, reuse inside an unchanged epoch against the memo hit it replaces, a new epoch, a refused and a partial collection clearing the token, a thread-ring-only hit, a contained range after 64 ring evictions (reused exactly when the ordinary collector memo-hits it), a partial unwatch and a remap of the plane itself, and another thread's walk published between the two points with a barrier).TestDepthPlanesCollectedOncePerDrawcounts memo hits per steady draw (main plus the test 2 for depth, 4 for depth and stencil; this PR 1 and 2) and checks that a CPU write between draws or after a draw is still seen.TestStoresBetweenObservationschecks a stamp, a driver byte store (StoreOwnBytes) or CPU store between the sync and the note on both planes with exact outcomes: absorbed after a new epoch, a mapping change, a watch change or on an unbumped thread, and still seen later when it lands inside an unchanged epoch. The three*_store_window_*entries rerun those cases underAPS5_NO_COLLECT_MEMO,APS5_SHARED_COLLECT_MEMOandAPS5_NO_EPOCH_COLLECT_MAP.Mutants (one build,
DD_MUTANT): always collecting, a stale repeat sync, the old generation reuse, skipping the whole token validation, skipping only the epoch, unwatch or mapping check, not clearing the token on a refused collection and not honouring the issuing thread and a walker that never reaches its held collect (bounded wait) each fail a named assertion.rk3 replay with #17338 head,
--repeat 5, steady passes, main versus this PR: collect calls per pass 78,991 to about 62,780 (-16,200, about 2 x 8,100), memo hits 73,536 to about 57,330,OverwrittenInMemory/OverwrittenOnGputrue 0 and 0, depth surface retires 4 and creates 4 per pass unchanged,pixels_identical1.000000.The rk3 replay is paced and its frames are dominated by
Driver.Rewind, so its frame times are not timing evidence for this change; the counts above are the claim (about 16,200 fewer collector lookups per pass, each a map lookup plus a shared counter increment). The GTA timing is below.Performance. GTA V (PPSA04263, save
play1-first-autosave), 3840x2160, RTX 3080, driver 621.14, Windows 11 (10.0.26320), Ryzen 7 5800X,APS5_TRACE_SECONDS=235:30withAPS5_DRAW_PIPELINE=1 APS5_PIPELINED_DRAWS=32 APS5_PIPELINE_DISPATCH=1. Maind03beaf110(A) against this PR3fb1bd7422(C): one warm-up boot per side (discarded), then three timed pairs in the order A C, C A, A C. Every run is a fresh boot from the same save, with a preflight check and a timing claim; all linesdirty: false; every capture dropped trace events.Pair deltas C minus A: +3.92, -2.92, +2.53 ms/frame, mean +1.18 ms (+0.9 %), range -2.92 to +3.92; the A runs alone span 4.56 ms. The change is inside the noise: this PR removes about 16,200 collector lookups per pass (the counts above), which at a map lookup each is well below what a 3,040-draw frame at about 125 ms can resolve (+0.4 us per draw, range -1.0 to +1.3).
Driver.Draw.preparemoves +3.0, -2.7, +2.2 ms/frame with the same sign as the frame time, so the PR's collect saving is not visible in the zones either. I am not claiming a frame-time gain; the claim is the counted reduction.The two summary lines below are the first pair (all runs, traces, digests and compares are in the release https://github.com/chitters2004/AnyPS5/releases/tag/pr-dedupe-t5):
{"commit":"d03beaf11059","dirty":false,"env":{"APS5_ADDRESS_COPY_MAX_MIB":"512","APS5_DRAW_PIPELINE":"1","APS5_DUMP_FRAMES":"80","APS5_DUMP_FRAMES_EVERY":"300","APS5_LOG_TIMESTAMPS":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"","APS5_PIPELINED_DRAWS":"32","APS5_PIPELINE_DISPATCH":"1","APS5_TRACE_DIR":"","APS5_TRACE_SECONDS":"235:30","APS5_TRACE_SUMMARY_EVERY":"10","APS5_VIDEOOUT_TELEMETRY_DIR":""},"gpu":"NVIDIA GeForce RTX 3080","driver":"NVIDIA 621.14","os":"Windows 10.0.26320","cpu":"AMD Ryzen 7 5800X 8-Core Processor","cpu_threads":16,"memory_mib":32676,"title_id":"PPSA04263","title":"Grand Theft Auto V","content_version":"01.011.001","resolution":"3840x2160","window":{"spec":"seconds 235:30","first_present":4182,"ms":30019.206},"presents":246,"frame_ms":{"p50":121.492,"p90":125.296,"p99":147.006,"max":150.669},"gpu_busy_ms_per_present":32.8,"worker_busy_pct":{"queue worker 0x0":94.5},"top_phases_ms_per_frame":{"Driver.Packet":109.256,"Graphics.Draw":92.843,"Driver.Packet.draw":80.086,"Driver.Draw":79.496,"Graphics.Draw.draw_and_resource_release":72.459,"Driver.DrawFinish":70.525,"Driver.Draw.prepare":63.845,"Driver.DrawFinish.draw_enqueued":39.031},"owned_draw_counts":{"OwnedDraw.arm_attempt":738,"OwnedDraw.arm_blocks_requested":738,"OwnedDraw.arm_refused":738,"OwnedDraw.evicted_other":2065},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":5.658,"events_lost":1136551,"totals_full":false},"draw committer":{"events_full_after_s":11.064,"events_lost":447799,"totals_full":false},"draw finisher":{"events_full_after_s":16.124,"events_lost":224836,"totals_full":false}},"trace":"aps5-trace-d03beaf11059-22608.json.gz"}
{"commit":"3fb1bd74229e","dirty":false,"env":{"APS5_ADDRESS_COPY_MAX_MIB":"512","APS5_DRAW_PIPELINE":"1","APS5_DUMP_FRAMES":"80","APS5_DUMP_FRAMES_EVERY":"300","APS5_LOG_TIMESTAMPS":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"","APS5_PIPELINED_DRAWS":"32","APS5_PIPELINE_DISPATCH":"1","APS5_TRACE_DIR":"","APS5_TRACE_SECONDS":"235:30","APS5_TRACE_SUMMARY_EVERY":"10","APS5_VIDEOOUT_TELEMETRY_DIR":""},"gpu":"NVIDIA GeForce RTX 3080","driver":"NVIDIA 621.14","os":"Windows 10.0.26320","cpu":"AMD Ryzen 7 5800X 8-Core Processor","cpu_threads":16,"memory_mib":32676,"title_id":"PPSA04263","title":"Grand Theft Auto V","content_version":"01.011.001","resolution":"3840x2160","window":{"spec":"seconds 235:30","first_present":4130,"ms":30102.994},"presents":239,"frame_ms":{"p50":125.485,"p90":129.754,"p99":137.446,"max":156.567},"gpu_busy_ms_per_present":33.048,"worker_busy_pct":{"queue worker 0x0":94.923},"top_phases_ms_per_frame":{"Driver.Packet":113.221,"Graphics.Draw":96.469,"Driver.Packet.draw":83.336,"Driver.Draw":82.738,"Graphics.Draw.draw_and_resource_release":75.05,"Driver.DrawFinish":73.284,"Driver.Draw.prepare":66.844,"Driver.DrawFinish.draw_enqueued":40.414},"owned_draw_counts":{"OwnedDraw.arm_attempt":717,"OwnedDraw.arm_blocks_requested":717,"OwnedDraw.arm_refused":717,"OwnedDraw.evicted_other":2019},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":5.491,"events_lost":1169789,"totals_full":false},"draw committer":{"events_full_after_s":10.733,"events_lost":472533,"totals_full":false},"draw finisher":{"events_full_after_s":15.504,"events_lost":246818,"totals_full":false}},"trace":"aps5-trace-3fb1bd74229e-6468.json.gz"}
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)Overlap to note when posting: #15058 changes
WrittenUnitsandSyncFromMemorysignatures and the same call site; #9895 edits the adjacentSyncFromMemoryline. Both will need a trivial rebase either way.🤖 Generated with Claude Code