Skip to content

perf(agc): count the finisher's pending writes in a fixed table - #235

Merged
utkuhalis merged 1 commit into
AnyTogether:mainfrom
hedibravofox:perf/agc-finisher-write-block-table
Dec 5, 2026
Merged

utkuhalis merged 1 commit into
AnyTogether:mainfrom
hedibravofox:perf/agc-finisher-write-block-table

Conversation

@hedibravofox

Copy link
Copy Markdown

What

With APS5_DRAW_PIPELINE, the draw finisher counts the 1 MiB blocks that its pending jobs write, so that a pending-overlap query can answer "no overlap" without walking the jobs (APS5_NO_PENDING_WRITE_BLOCKS turns this off). The counts were kept in an std::unordered_map with one node per block, and every job also copied its write ranges into countedWrites so they could be uncounted later. The queue worker allocated both for every draw and the finisher thread freed them. On macOS under Rosetta, freeing memory allocated by another thread is slow. In a GTA V scene this was about 13% of the queue worker's busy time and 9% of the finisher's.

This PR keeps the counts in a fixed table of 4096 slots indexed by the block number, and uncounts from the job's own writes, which stay unchanged while the job is pending. No memory is allocated per draw. Two blocks 4 GiB apart share a slot, so a shared slot can only make the counts say "maybe", and the query then walks the pending jobs as before. The answers are unchanged. docs/dev/ENVIRONMENT.md describes the table under APS5_NO_PENDING_WRITE_BLOCKS.

Tested

macOS 27.2 (Darwin 27.2.0), Apple M5 Pro, MoltenVK 1.4.2. The binaries are x86_64 under Rosetta.

Tests: I ran the 179 agc_driver tests that run with the draw finisher or the draw pipeline, with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, on main and on this branch.

  • agc_driver_pending_write_blocks, _verify and _off pass.
  • 135 pass and 44 fail, and the same 44 fail on main. They are the color_memo_* and target_memo_* tests, draw_allocations_draw_pipeline and _plan_verify, draw_recapture_on_change_finisher, the pipelined_flush_read_order* tests and the storage_write_back_overhang_* tests.
  • I ran the same tests again on both with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, using the Khronos validation layer vulkan-sdk-1.4.363.1 built from source for x86_64. In both logs the loader inserts the layer 195 times and never reports it as not found. The results are the same 135 passes and 44 failures, and the only validation error is VUID-vkCreateDescriptorSetLayout-support-09582, 4 times in each log, so this PR adds none.

Performance: GTA V (PPSA04263, 01.011.001), in the bank scene of the prologue, in Performance mode (not the title's default graphics mode). Each run covers 70 s from arrival in the scene, without input, with APS5_TRACE_SUMMARY_EVERY=10. The builds were run alternately, A, B, A, B.

GTA V does not run on main's driver library alone. Both builds are the main of 2026-12-04 plus the same unmerged local changes, with the A build lacking only this commit:

Both builds ran with APS5_DRAW_PIPELINE=1 APS5_PIPELINE_DISPATCH=1 APS5_PIPELINE_DISPATCH_MISSES=1 APS5_ASYNC_DRAW_COMPILE=1 APS5_ASYNC_PIPELINE_WORDS=1 APS5_NO_ADAPTIVE_STAGING=1.

Run Build Presents in 70 s FPS p50 per 10 s interval (ms)
A1 without this PR 1032 14.68 60.5, 60.6, 62.2, 64.3, 68.2, 72.3, 69.6
B1 with this PR 1094 15.57 59.2, 60.5, 61.8, 61.9, 61.8, 58.0, 67.2
A2 without this PR 1065 15.17 62.6, 59.7, 62.4, 61.4, 63.3, 67.8, 67.4
B2 with this PR 1083 15.41 59.6, 59.6, 61.7, 60.8, 61.9, 63.4, 68.2

Without this PR the mean is 14.93 FPS, and with it 15.49 FPS, about 4% more. Both B runs are above both A runs, but the gap is small compared with the run-to-run spread of the A runs. In a Metal System Trace of the same scene, countPendingWrites and uncountPendingWrites leave the profile, and the queue worker, the finisher and the committer are on the CPU about 20% fewer samples.

tools/trace_digest.py of A2 and B2

A2:

**13 intervals**, 0.000 to 130.346 s, 2840 presents
Apple M5 Pro, MoltenVK 1.4.2, Darwin 27.2.0, 3840x2160, commit 22fbe12c891f
| # | seconds | presents | p50 | p90 | p99 | max | GPU busy | busiest worker | largest phase |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 0.000-10.005 | 258 | 33.4 | 35.4 | 245.1 | 562.8 | 4.1 | 14 % | Shader.PrepareArtifact 22.0 |
| 2 | 10.005-20.011 | 272 | 33.8 | 35.3 | 43.3 | 771.4 | 4.0 | 11 % | VideoOut.Flip 5.6 |
| 3 | 20.011-30.018 | 292 | 34.2 | 35.8 | 37.9 | 48.4 | 4.5 | 4 % | VideoOut.Flip 5.3 |
| 4 | 30.018-40.048 | 419 | 16.7 | 34.9 | 37.2 | 52.1 | 5.4 | 64 % | VideoOut.Flip 16.5 |
| 5 | 40.048-50.106 | 412 | 16.7 | 35.4 | 79.5 | 140.2 | 2.9 | 61 % | Driver.Packet 14.8 |
| 6 | 50.106-60.124 | 157 | 62.6 | 72.8 | 84.4 | 89.2 | 22.2 | 99 % | Driver.Packet 60.7 |
| 7 | 60.124-70.164 | 164 | 59.7 | 67.7 | 75.7 | 79.3 | 23.0 | 98 % | Driver.Packet 58.0 |
| 8 | 70.164-80.169 | 158 | 62.4 | 68.9 | 81.7 | 82.3 | 21.9 | 98 % | Driver.Packet 60.0 |
| 9 | 80.169-90.200 | 161 | 61.4 | 66.3 | 76.1 | 76.3 | 27.8 | 98 % | Driver.Packet 55.8 |
| 10 | 90.200-100.205 | 156 | 63.3 | 70.2 | 77.6 | 78.8 | 28.5 | 98 % | Driver.Packet 59.9 |
| 11 | 100.205-110.294 | 127 | 67.8 | 136.3 | 199.5 | 200.3 | 28.1 | 98 % | Driver.Packet 74.7 |
| 12 | 110.294-120.299 | 142 | 67.4 | 76.0 | 148.5 | 159.7 | 26.3 | 98 % | Driver.Packet 66.2 |
| 13 | 120.299-130.346 | 122 | 86.2 | 93.7 | 109.1 | 109.6 | 26.2 | 98 % | Driver.Packet 77.8 |

B2:

**12 intervals**, 0.000 to 120.316 s, 2702 presents
Apple M5 Pro, MoltenVK 1.4.2, Darwin 27.2.0, 3840x2160, commit f2f8093a2eec
| # | seconds | presents | p50 | p90 | p99 | max | GPU busy | busiest worker | largest phase |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 0.000-10.002 | 258 | 33.4 | 35.4 | 196.6 | 542.2 | 3.6 | 14 % | Shader.PrepareArtifact 22.2 |
| 2 | 10.002-20.008 | 270 | 33.9 | 35.6 | 44.5 | 799.8 | 4.0 | 11 % | VideoOut.Flip 5.8 |
| 3 | 20.008-30.008 | 289 | 34.3 | 36.2 | 37.0 | 50.8 | 4.8 | 5 % | VideoOut.Flip 6.2 |
| 4 | 30.008-40.010 | 417 | 16.7 | 35.1 | 40.8 | 45.7 | 5.4 | 45 % | VideoOut.Flip 11.8 |
| 5 | 40.010-50.034 | 385 | 16.7 | 65.8 | 86.0 | 136.3 | 3.0 | 79 % | Driver.Packet 20.3 |
| 6 | 50.034-60.042 | 164 | 59.6 | 69.2 | 77.4 | 78.2 | 25.1 | 98 % | Driver.Packet 57.9 |
| 7 | 60.042-70.057 | 165 | 59.6 | 65.8 | 76.0 | 80.6 | 20.1 | 98 % | Driver.Packet 57.0 |
| 8 | 70.057-80.084 | 159 | 61.7 | 69.9 | 80.2 | 81.5 | 21.2 | 98 % | Driver.Packet 58.3 |
| 9 | 80.084-90.124 | 163 | 60.8 | 65.1 | 77.8 | 77.8 | 22.4 | 98 % | Driver.Packet 52.1 |
| 10 | 90.124-100.162 | 161 | 61.9 | 66.6 | 76.2 | 76.7 | 22.5 | 98 % | Driver.Packet 58.1 |
| 11 | 100.162-110.296 | 139 | 63.4 | 79.8 | 205.6 | 212.5 | 24.1 | 98 % | Driver.Packet 68.2 |
| 12 | 110.296-120.316 | 132 | 68.2 | 84.9 | 206.9 | 209.1 | 25.6 | 98 % | Driver.Packet 71.1 |

The four aps5-summary.jsonl files are available on request.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M5 Pro, MoltenVK 1.4.2, macOS 27.2
  • AI-assisted: yes

🤖 Generated with Claude Code

The draw finisher kept a hash map entry per 1 MiB block that a pending draw
writes, and copied each draw's write ranges to uncount them later. The worker
allocated both for every draw and the finisher freed them, about 13% of the
worker's busy time and 9% of the finisher's in a GTA V scene. Count the blocks
in a fixed table of 4096 slots instead: blocks that share a slot only send a
query to the walk over the pending jobs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@utkuhalis

Copy link
Copy Markdown
Collaborator

Thanks, nice to see the per-draw allocations go. I checked that uncounting from writes stays exact:

  • A Pending gets counted in Enqueue and is then moved into the pending deque. From then on DrawPipeline.cpp only reads writes (PendingOverlaps, PendingOverlapsAny, the verify walk) until run uncounts the front entry and pops it. So the ranges that get uncounted are the ones that were counted, and the snapshot in countedWrites was redundant.
  • counted is set only when the writes were counted (not for unknown jobs). Together with countedUnknown this keeps the unknown and known paths symmetric, as before.
  • pendingWriteWide is a pure function of the range, so the wide count stays balanced too. Ranges that go through the block loop are at most 256 blocks, so the table loop is bounded as before.
  • A shared slot can only make pendingBlocksClear return false, and then the query walks the jobs. That only costs a walk; it can't give a wrong "no overlap". APS5_VERIFY_PENDING_WRITE_BLOCKS still checks the same claim.

The A/B is fair and the doc line matches the code. We'll merge once CI is green.

@utkuhalis
utkuhalis merged commit 8f79a7a into AnyTogether:main Dec 5, 2026
10 checks passed
@utkuhalis

Copy link
Copy Markdown
Collaborator

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants