Repository navigation
perf(agc): share snapshot copies and the BDA table of repeated address-based builds - #5542
Conversation
CnRJay
left a comment
There was a problem hiding this comment.
This is a good one. It removes the work at its source (the table differs only because snapshot copies get new device addresses), and the evidence is what CONTRIBUTING asks for: summary lines, traces, alternated runs, APS5_VERIFY_BDA_TABLES on the measured run, and regression runs on other titles.
Things I checked that look right:
- A snapshot copy is only shared for regions that are not writable, not host-backed and not sparse, and a copy whose bytes differ is replaced, never overwritten. A batch still reading the old copy on the GPU keeps its
shared_ptr, so there is no write-after-read hazard on reuse. - The table key includes the own regions' device address and permissions, so a table can't be shared between builds whose copies differ.
Smaller points:
- The byte compare on every use reads the cached
Buffer's mapped memory.BufferprefersHOST_CACHEDfor the default host properties but falls back to plainHOST_VISIBLE | HOST_COHERENT, which is usually write-combined. On a device where that happens, thememcmpinsnapshotCopyreads from uncached memory on every build, which is much slower and could cancel part of the gain. Keeping a CPU-side copy of the bytes in the cache entry, or a hash plus the guest generation, would make the check independent of the memory type. SnapshotCopyCacheis process-wide and holds one device at a time: a call with anotherVkDeviceclears it. During device replacement, or with a compile-only device alongside, two devices would keep clearing each other's entries. A map keyed by device, like the other per-device caches, would avoid that.space->tablesstill evictsbegin()of astd::maponce it reaches 64 entries, which is the lexicographically smallest key, not the oldest. That was harmless while the keys were only write sets. Now that own-region keys share the map, a scene with more than 64 combinations would evict an arbitrary one per miss. An LRU like the onesnapshotCopyuses would be more predictable.- On the remaining 15% of misses (tables whose buffer expired because the cache holds weak references): if that matters in other titles, keeping the most recent N table buffers alive would turn those into hits too. That can be a follow-up.
|
Windows test of #5542: Windows 10 (19045), MinGW-w64 GCC 15.2, Release, AI-assisted (Claude Code). |
|
Correction to my Windows test above: I wrote that the GPU selected by the tests was the NVIDIA GeForce RTX 4080 SUPER ( AI-assisted (Claude Code). |
|
Thanks for the careful review. Changed in 138c309: each cached snapshot copy now keeps a CPU copy of its bytes and the reuse check compares against that, so it no longer reads the buffer's mapping whatever its memory type; and the space's table map evicts the least recently used table instead of the smallest key (new test case). On The Pathless the CPU copies hold about 17 KB, and address-based builds measure the same as before in 3+3 alternated runs. The snapshot cache holds one device at a time on purpose, like the image mirrors next to it: only one device builds resources at a time, |
…s-based builds Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… recency Each cached snapshot copy keeps a CPU copy of its bytes, so the reuse check no longer reads the buffer's mapping, which is uncached on devices without host-cached coherent memory. An address space's table map now evicts its least recently used table instead of its smallest key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
138c309 to
97c3687
Compare
|
Checked the snapshot-copy cache (key, byte compare against the CPU copy, LRU/budget, teardown) and the |
…s-based builds (PR boykopovar#5542)
What
Address-based builds that repeat the same work could not reuse their BDA table. On The Pathless's main menu every address-based build (about 44 per frame) takes the cached address space and adds two snapshot regions of its own; the snapshots are copied into new buffers on each build, so their device addresses, and with them the table, never repeat.
BdaResourcesthen merged the space's few thousand ranges with the build's own, hashed them, searched the table cache and built a new table buffer every time ([bda-table]0 hits of 3,911 in 10 s), although the same five space and snapshot combinations recur on every frame.Two changes make the table repeat:
ClearSnapshotCopies). A dropped copy stays alive in every build and batch that still holds it.CachedAddressTablenow also serves a build with regions of its own over the cached address space. Its table is a function of the space's ranges (immutable while the space lives), the build's written ranges and its own regions' table entries, so those two are the key (AddressTableKey) of the space's table map, which already held the tables of builds with written ranges.BdaResourcesbinds a cached table's buffer by its serial, as it does for the space's own table.APS5_VERIFY_BDA_TABLES=1rebuilds the table on every hit and throws when the bytes differ.Two existing comments still describe the earlier condition (the
CachedTablecomment inGuestBufferMemory.hppand the serial note aboveTableEntryinBdaResources.cpp); the conventions check only takes comment changes with a technical-debt change, so I left them as they are.Proposed wording for the two comments
GuestBufferMemory.hpp, aboveCachedTable:BdaResources.cpp, the last two lines of the comment aboveTableEntry:#1364 also reuses buffer copies, but only for builds that are not address-based (
!addressableinUploadPrepareandcopyRegion) and only for host-backed or writable regions, so it does not reach these snapshots.Tested
Windows 11, RTX 5070 Ti, WinLibs GCC 15.2.0 r7, Release.
agc_driver_graphics(tests/BdaResources.cpp) gets two cases:addressTableKeyTests: keys of builds that differ only in their written ranges, or only in the device address of an own region, differ; written ranges and own regions with the same words give different keys.snapshotTableTests, over a real cached address space: two builds of one snapshot with the same bytes share its device copy and their BDA table; a snapshot with other bytes gets a copy of its own and another table; a build bound by descriptor never takes a copy made for BDA; a build with written ranges shares the snapshot copy; every cached table equals the build'sAddressRanges(); afterClearSnapshotCopiesa build gets a new copy, and no Vulkan object is left behind.Full
ctest(Windows, RTX 5070 Ti): 1097 of 1098 pass.agc_driver_wave32_subgroupfails (thread 0 s_bcnt1 of a VCC compare is 0, expected 8) and fails the same way onmainhere.python tools/check_conventions.py --base upstream/mainpasses.CI on the fork, same steps as
build.yml(run 38477341209): Linux and Windows pass. The first Windows attempt lostguest_kqueue(EVFILT_VNODE: FindFirstChangeNotification failed, error=5, nothing this change touches); the rerun passed.The Pathless (PPSA01825, 02.000.000), main menu,
main0aa4cb1 against this PR:APS5_PROFILE_DRAWrun per side: an address-based build went from 854 to 592 µs on average, at about 44 builds per frame on both sides, so the address-based builds went from 37.5 to 26.1 ms per frame (-11.4 ms).[bda-table]went from 0 hits of 3,911 to 4,269 of 4,994 (85 %). The remaining misses are tables whose buffer expired: the table cache holds weak references, so when every build that used a table has finished, the next build of the same key builds the buffer again from the cached ranges. The snapshot copies saw no differing bytes and no evictions in steady state (64,034 reuses, 14 new copies, all at start-up).APS5_VERIFY_BDA_TABLES=1: every hit was rebuilt and compared, none differed.APS5_TRACE_SECONDSwindow of 12 s, warm caches, 5 valid runs per side alternated base/change (runs lost to the Wwise crash of Windows: resuming a write fault overwrites the guest red zone when AVX state is live #361 repeated): 102.5 ± 5.8 ms onmainagainst 94.7 ± 6.1 ms with this PR (9.78 ± 0.58 against 10.60 ± 0.67 FPS). The change was faster in each of the five pairs, by 0.8 to 21.4 ms; the spread of this batch is large, so the per-build numbers above are the measurement and the frame time only confirms their direction.Summary lines of the ten runs,
mainfirst (commit0aa4cb1 ismain; d60ec357fbea is this change beforeAddressTableKeywas split out ofCachedAddressTable, with the same behaviour); the ten traces are in the attached zip.aps5-summary.jsonl
Hades (PPSA03355), ANIMAL WELL (PPSA22520), Balatro (PPSA21402) and Dead Cells (PPSA15552),
main0aa4cb1 against this PR, 90 s each with 8 dumped frames: the same points reached (the first three keep running at their menus; Dead Cells stops on both sides atNGS2: non-default listener orientation is unsupported), the same set of[gpu]log lines, ANIMAL WELL's frames identical; Hades's and Balatro's frames differ only where their animations or loading were when dumped (Hades still loading on one side at frame 4, at the menu on both from frame 6), which I checked by eye.LEGO Horizon Adventures (PPSA14632, 01.004.000),
main0aa4cb1 against this PR, 2 runs per side of 160 s with 8 dumped frames into the intro: all four reach the intro cinematic and keep running, with the same[gpu]log lines and no errors; the dumped frames differ only where the cinematic was when each one was taken (the two runs with this PR also differ from each other there), and the last frame matches.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)bda-table-hits-traces.zip