perf(vk): keep the per-draw thread caches in a flat table #2774
Open
github-sync
wants to merge 2 commits from
github/pr/2774 into main
pull from: github/pr/2774
merge into: MagicBeanDev:main
MagicBeanDev:main
MagicBeanDev:github/pr/2893
MagicBeanDev:github/pr/2890
MagicBeanDev:github/pr/2887
MagicBeanDev:github/pr/2878
MagicBeanDev:github/pr/2875
MagicBeanDev:github/pr/2874
MagicBeanDev:github/pr/2873
MagicBeanDev:github/pr/2871
MagicBeanDev:github/pr/2756
MagicBeanDev:github/pr/2897
MagicBeanDev:github/pr/2896
MagicBeanDev:github/pr/2718
MagicBeanDev:github/pr/2842
MagicBeanDev:github/pr/2691
MagicBeanDev:github/pr/2895
MagicBeanDev:github/pr/2743
MagicBeanDev:github/pr/2765
MagicBeanDev:github/pr/2708
MagicBeanDev:github/pr/2732
MagicBeanDev:github/pr/2755
MagicBeanDev:github/pr/2817
MagicBeanDev:github/pr/2876
MagicBeanDev:release-please--branches--main
MagicBeanDev:release-please--branches--main--release-notes
MagicBeanDev:github/pr/2778
MagicBeanDev:github/pr/2888
MagicBeanDev:github/pr/2866
MagicBeanDev:github/pr/2877
MagicBeanDev:github/pr/2800
MagicBeanDev:github/pr/2829
MagicBeanDev:github/pr/2850
MagicBeanDev:github/pr/2889
MagicBeanDev:github/pr/2882
MagicBeanDev:github/pr/2860
MagicBeanDev:github/pr/2849
MagicBeanDev:github/pr/2886
MagicBeanDev:github/pr/2794
MagicBeanDev:github/pr/2840
MagicBeanDev:github/pr/2830
MagicBeanDev:github/pr/2834
MagicBeanDev:github/pr/2719
MagicBeanDev:github/pr/2742
MagicBeanDev:github/pr/2741
MagicBeanDev:github/pr/2820
MagicBeanDev:github/pr/2771
MagicBeanDev:github/pr/2859
MagicBeanDev:github/pr/2858
MagicBeanDev:github/pr/2823
MagicBeanDev:github/pr/2757
MagicBeanDev:github/pr/2769
MagicBeanDev:github/pr/2844
MagicBeanDev:github/pr/2843
MagicBeanDev:github/pr/2673
MagicBeanDev:github/pr/2855
MagicBeanDev:github/pr/2739
MagicBeanDev:github/pr/2807
MagicBeanDev:github/pr/2851
MagicBeanDev:github/pr/2789
MagicBeanDev:github/pr/2814
MagicBeanDev:github/pr/2818
MagicBeanDev:github/pr/2772
MagicBeanDev:github/pr/2750
MagicBeanDev:github/pr/2786
MagicBeanDev:github/pr/2785
MagicBeanDev:github/pr/2782
MagicBeanDev:github/pr/2779
MagicBeanDev:github/pr/2775
MagicBeanDev:github/pr/2706
MagicBeanDev:github/pr/2766
MagicBeanDev:github/pr/2747
MagicBeanDev:github/pr/2759
MagicBeanDev:github/pr/2745
MagicBeanDev:github/pr/2740
MagicBeanDev:github/pr/2733
MagicBeanDev:github/pr/2725
MagicBeanDev:github/pr/2727
MagicBeanDev:github/pr/2728
MagicBeanDev:github/pr/2729
MagicBeanDev:github/pr/2721
MagicBeanDev:github/pr/2722
MagicBeanDev:github/pr/2715
MagicBeanDev:github/pr/2714
MagicBeanDev:github/pr/2707
MagicBeanDev:github/pr/2703
MagicBeanDev:github/pr/2705
MagicBeanDev:github/pr/2697
MagicBeanDev:github/pr/2679
MagicBeanDev:github/pr/2676
MagicBeanDev:github/pr/2669
MagicBeanDev:docs/what-hookshot-connects-to
MagicBeanDev:fix/hdr-encode-test
MagicBeanDev:fix/main-tests
MagicBeanDev:fix/play-cache-files
MagicBeanDev:perf/game-barrier-order
MagicBeanDev:feat/heap-lazy
MagicBeanDev:feat/texture-detail
MagicBeanDev:perf/astc-opaque-bc1
MagicBeanDev:fix/fs-dir-lock
MagicBeanDev:fix/framegen-native-kept
MagicBeanDev:fix/update-no-downgrade
MagicBeanDev:port/k10-gcn-f16-mirrors
MagicBeanDev:fix/framegen-mult-cap
MagicBeanDev:chore/release-as-2.1.1
MagicBeanDev:perf/pcache-load-switch
MagicBeanDev:fix/prepare-progress-log
MagicBeanDev:fix/update-closes-stale-game
MagicBeanDev:fix/hdr-scaled-sampled
MagicBeanDev:perf/first-start-preset
MagicBeanDev:perf/zero-upload
MagicBeanDev:fix/vcore-escape-2s
MagicBeanDev:test/fake-gh-atomic
MagicBeanDev:perf/batch-idle-flush
MagicBeanDev:test/finish-stub-a
MagicBeanDev:chore/record-2.1.0
MagicBeanDev:fix/main-red-tests
MagicBeanDev:fix/test-display-startup-driver-cache
MagicBeanDev:feat/mesh-shader-mode
MagicBeanDev:fix/menu-probes-at-startup
MagicBeanDev:fix/fps-counter-classic
MagicBeanDev:fix/test-log-batch-flake
MagicBeanDev:fix/narrow-screen-hud
MagicBeanDev:ci/prereleases
MagicBeanDev:release/v2.0.15
MagicBeanDev:hotfix/v2.0.15
MagicBeanDev:fix/present-ahead-replay
MagicBeanDev:release/v2.0.13
MagicBeanDev:fix/bracket-paths-2013
MagicBeanDev:fix/hdr-overlays-2013
MagicBeanDev:fix/fps-counter-scale-2013
MagicBeanDev:fix/stick-seen-at-rest
MagicBeanDev:hotfix/menu-fits-screen
MagicBeanDev:fix/f1-always-opens-menu
MagicBeanDev:fix/ini-keep-lines
MagicBeanDev:claude/cranky-pike-7cf247
MagicBeanDev:perf/texture-answer-flat
MagicBeanDev:fix/stamp-test-modules
MagicBeanDev:fix/appimage-fonts
MagicBeanDev:fix/display-awake
MagicBeanDev:release/v2.0.12
MagicBeanDev:fix/discord-windows
MagicBeanDev:rc/v2.0.12-tests
MagicBeanDev:fix/dlss-keep-feature
MagicBeanDev:rc/v2.0.12
MagicBeanDev:button-icons
No reviewers
Labels
Clear labels
accessibility
Barrier affecting people with disabilities
autorelease: pending
autorelease: tagged
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
duplicate
This issue or pull request already exists
enhancement
New feature or request
github_actions
Pull requests that update GitHub Actions code
good first issue
Good for newcomers
help wanted
Extra attention is needed
invalid
This doesn't seem right
question
Further information is requested
testing wanted
wontfix
This will not be worked on
No labels
accessibility
autorelease: pending
autorelease: tagged
bug
dependencies
documentation
duplicate
enhancement
github_actions
good first issue
help wanted
invalid
question
testing wanted
wontfix
Milestone
Clear milestone
No items
No milestone
Assignees
Clear assignees
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Hookshot!2774
Loading…
Reference in a new issue
No description provided.
Delete branch "github/pr/2774"
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?
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.
In the game
Two A/B runs by reviewers, each swapping only
hookshot_vk.dll:graphics_pipelinelookup is 25-30% cheaper in every run and every draw-count bin (about 0.08 ms a frame at 2,500 draws),picksabout 0.07 ms cheaper. The total submit change is within run-to-run noise on that CPU.No crash, device loss or picture difference in either set of runs. The gain is a small share of the submit on a fast CPU and shows more on a mid-range one.
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.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: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.
mainba005ca4with #2711, #2731, #2685, #2709, #2825 and #2826 merged locally, with and without this PR on top. Two renderer DLLs built from the two trees and swapped between launches (each launch prepares its shaders again first), arms alternated, three launches each.RX_VK_FRAMEGEN=0,HOS_FPS_MAX=240. CPU is the process's user + kernel time over the window divided by the frames shown.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).
In-game A/B on Windows: the per-thread pipeline lookup gets clearly cheaper, but the total replay submit shows no change above run-to-run noise.
Setup
main51da0464(A) against this PR ataff9633d(B), both built release withRX_VK_ONLY=1. Onlyhookshot_vk.dllwas swapped, in one game folder.vk_shader.cppis in the translator stamp. Each arm had one warm-up run first.HOS_FPS_MAX=1000(uncapped). The log confirms "frame rate uncapped", and the GPU-bound wait was about 0 throughout.Replay time on the Presentation Thread, µs per draw (median of each run's windows)
Run 1 (A) was about 25% slower on every timer, including memory sync and queue submit, which this PR does not touch. That points to the machine's state, so the comparison below leaves it out. Leaving out an A run favours neither arm here: the slow run would only make B look better.
At equal draw counts (A = runs 3 and 6, B = runs 2, 4 and 5, ms per frame)
Reading
graphics_pipelinethread cache is the clear effect.In short, the change does what it says for the pipeline lookup. On this CPU at Kokiri's draw counts, that lookup was a small share of the submit to begin with.
Thanks @klikso and @Jiro131 for the in-game runs. I added both to the description under "In the game" and dropped the frame-time figure extrapolated from the micro-benchmark. In short: the pipeline lookup is clearly cheaper on both machines, and the total submit gain shows on the 5600X (-4%) but stays within noise on the 13900K.