Repository navigation
perf(vk): keep the per-draw thread caches in a flat table - #2774
corentinbonjour wants to merge 2 commits into
Conversation
handle_hits, picks and graphics_pipeline's mine are searched on every draw of the replay's render thread, the frame's bottleneck. std::unordered_map paid a modulo by a prime and a node pointer chase per lookup; FlatMap (open addressing, linear probing, at most half full) keeps key and value in one array. HandleHit's tex_epoch takes texture_epoch()'s 32 bits, so a slot is 56 bytes. tests/test_flat_map.py checks it against std::unordered_map and texture_view_of over both. Assisted-by: Claude Opus 5.5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Joey-Magnus
left a comment
There was a problem hiding this comment.
I merged this into current main and ran tests/test_flat_map.py with clang++ and g++, and the vk/shader subset of run_all. All pass, and it still passes merged together with #2644. On reference stability: in program_for, Pick& pk = picks[key] is held across program_pick(), but program_pick never reaches picks or program_for, so no insert happens in between. In graphics_pipeline and texture_view_of, the insert comes after the last use. tex_epoch as uint32_t matches texture_epoch(). Two things before merging:
- Performance: the 0.3-0.6 ms a frame is extrapolated from a micro-benchmark, and the description says it hasn't been measured in the game. Please add an A/B in Ocarina (replay submit ms or frame time, same scene) before this goes in as a perf change.
- Memory: at the 65536 clear threshold (vk_draw.cpp:4721), handle_hits goes from about 5 MB to 14 MB per recording thread, and FlatMap::clear (flat_map.h:38) keeps the array for good. That is per thread, so it multiplies with RX_VK_PARALLEL. A 3/4 load factor, or dropping the array on clear when it is far above the size, would keep it closer to what main has.
…clear At handle_hits' 65536-entry clear threshold the half-full table took 14 MB per recording thread against about 5 MB for the node map, and clear kept it. Up to 3/4 full it is 7 MB there, and a clear gives back an array over 4096 slots, so a cache cleared at its limit does not keep its peak. Assisted-by: Claude Opus 5.5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for checking the reference lifetimes. On memory: FlatMap now fills up to 3/4 and its clear gives back an array over 4096 slots, so handle_hits at the 65536 threshold is 7 MB per recording thread instead of 14 (main's node map: about 5.3 MB), and it does not keep that after the clear. Re-measured at 3/4: flat/std 0.63-0.78 across the same configurations (it was 0.53-0.70 half full). tests/test_flat_map.py still passes, the description has the new numbers. |
|
An in-game measurement on a slower desktop CPU: the replay's submit is ~0.4 ms a frame shorter in Hyrule Field in all three pairs; the Market was too noisy on this card to say. Windows 11, Ryzen 5 5600X (6 cores, 12 threads), RTX 3060 Ti 8 GB (driver 32.0.16.2114), output 2560x1440, DLSS model K. Hyrule Field child, 30 s of the camera turning (~1,200 draws a frame):
Submit is 10.03 -> 9.62 ms on the means (-4%) and every run with the PR is under every run without; CPU a frame 54.7 -> 52.7 ms. The fps difference (+0.8) is inside the spread. Castle Town Market entered from the field, 40 s (1,500-3,000 draws): base 45.9 / 49.4 / 34.2 fps, this PR 51.9 / 46.4 / 47.7. One base launch landed in this card's slow state (GPU 14.7 ms a frame against 7.6-7.8 in the other five), so I would not read a gain into it; without that launch the two arms overlap (submit 13.9-14.2 against 12.8-14.2 ms). No crash, device loss or picture difference seen in the six launches with it (start-of-window screenshots only; I did not watch them live). |
The frame is bound by the Vulkan replay's CPU time on the game's render thread (submit 23-28 ms for 2300-3000 draws against ~12 ms of GPU on an RTX 5070 Laptop / 275HX), and every draw searches three thread-local
std::unordered_maps: a modulo by a prime and a node pointer chase each time, often a cache miss once the draw's other work has gone through the cache.What changed
gpu/vk/flat_map.h:FlatMap<K, V>, an integer-keyed table in one array (open addressing, linear probing, at most 3/4 full, slot from a multiplicative hash). It has only what these caches use:find/end,operator[],size,clear. No single-key erase;clearkeeps a small array and gives back one over 4096 slots.handle_hits(texture_view_of),graphics_pipeline'smine(vk_draw.cpp), andpicks(program_for, vk_shader.cpp).texture_answer's cache was already a flat array by slot on main, so it is left as it is.HandleHitis reordered, andtex_epochis nowuint32_t(the typetexture_epoch()returns). The struct drops from 48 to 40 bytes and a table slot from 64 to 56.handle_hitsclear threshold (more than 65536 entries, checked once a submission) and thepicksone are unchanged, so entries are kept or dropped exactly as before.Measurements
Micro-benchmark, llvm-mingw clang 23, -O2 -march=x86-64-v3, on the 275HX. One draw does what the replay does: a
pickslookup, aminelookup and 6handle_hitslookups with the version/generation compare. Draws pick from N materials on a skewed distribution.std::unordered_map(with the oldHandleHit) andFlatMapalternate within each process, 21 repetitions, two processes per configuration. ns per draw, median (p25-p75), at the 3/4 load limit:The first version, at most half full, measured 0.53-0.70; 3/4 trades a little of that for half the memory. At ~2500 draws a frame this is about 0.2-0.5 ms of render-thread time.
Memory of
handle_hitsper recording thread (bytes allocated):A clear at that threshold now gives the array back, so the peak is not kept.
Testing
tests/test_flat_map.py, stdlib only, run by run_all with--cxx:std::unordered_map, 1.1M operations. It covers the replay's key shapes (handle keys, hashes, dense keys that share low bits), the replay's value types, growth and whole clears.HandleHit,handle_hitsandtexture_view_ofare cut out of vk_draw.cpp and built twice, once over each map. Both copies get the same 300k calls while slots are registered anew, textures destroyed and epochs pass. The views, levels, textures, and the stub calls and their order must match.vk_draw.cppandvk_shader.cppcompile with the build's renderer flags (llvm-mingw, -O2).python -I -B tests/run_all.py, with llvm-mingw on PATH and RX_CPM set: 434 passed, 24 failed, 38 skipped. That is the same set of failing checks as an untouched origin/main worktree (missing tools on this PC). The only differences weretest_gpu_hang_watchandtest_setup_tar_diagnostics, one in each run under 24-way load; both pass alone in both trees.Notes
handle_hits' 65536-entry clear threshold the table is 7 MB against about 5 MB for the node map, per recording thread, and the clear gives it back. The threshold is unchanged, so entries are kept or dropped as before.HOOKSHOT_SHADER_STAMP, so players' program caches are translated again once after updating, as with any vk_shader.cpp change.picksinprogram_for(what a pick keeps) and lists "a flat table for the picks" as later work: this is that table, forpicksand the two other per-draw maps. The two touch neighbouring lines, so whichever lands second needs a small rebase.picks[key]is the only insert in program_for, andmine/handle_hitsinsert after their last use of the lookup result.