Texture reads: one load of a word nobody writes before the tables every read asks (SEEDS, HOST_WRITTEN, HOST_WRITTEN_AT), on top of #2226 #3739
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3739"
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?
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Lyrnc on 2026-11-30T05:45:31Z.
Problem
T02: since v2026.11.29.01, 34 reporters (22 issues, e.g. #2129: 6.1 -> 13.6 µs a draw; #3232 #2891 #3146 #3249) see each draw cost about twice as much, folding off too. The suspect was the
SEEDSmutex thate530c003put on every texture read.What the bench logs on this PC say (RTX 5080, 9800X3D, 32 GB;
C:\upDt\runs\*\play.log, medians of the world seconds, i.e. aVICTORY_THINline with >= 20,000 calls a frame; scriptsC:\lx\notes\agents\L\percall.py,submit.py,C:\lx\notes\agents\L3\cardpass.py):Recording threads, µs per title list call (
VICTORY_THIN: thread time / calls):829188e9(.01), compile admission on829188e9, admission off (VICTORY_COMPILE_MEMORY=0, or + 299c4aa3 = our #3347)With the compile admission off, a call costs what it cost on .28.03 (the teammate's 3.08-4.10 µs against 3.3 is the same picture). The "twice per draw" is the admission: every refused compile held a recording thread up to a second, folding or not. That part is #3347's.
Submitting thread, the same Series S scene (s1 runs, ~9,400-9,900 draws a frame):
(The .01 s1 runs have 17 world seconds each; the other pairs do not share a scene.) What really grew on the submitting thread is the card copies (+4.3-4.6 ms a frame, with fewer passes: hash passes 239-269 -> 74-87, compare passes ~390 -> ~375, so each pass costs more) and the sweep (+2.1). Neither is a per-draw lock. Frame time over draws: 5.4-7.5 µs on .28.03 (median 6.0, 3 runs), 6.5-8.7 µs on .01 with the admission off (median 7.3, 10 runs), about +20 % over runs that do not all share a scene, not 2x.
What the draw path asks more since .28.03 (diff of
draw.rs::readable,resource::texture_at,texture.rs; both frome530c003,VICTORY_UPLOAD_GUARD/VICTORY_PLACED_COPIED, on by default):seed_pendingtakes the globalSEEDSmutex to learn that nothing waits (in 9 world runs: 0 or 1 view ever seeded);texture_attakes the globalHOST_WRITTEN_ATmutex (host_written_like) before its ownPLACED_TEXTURESone;upload_fromtakesHOST_WRITTEN(a mutex on main; #2226 makes it andSEEDSRwLocks).The other draw-path changes (
finish_depth_writes, the frame-generation camera check for 3-vertex draws,barstat::on(), fixed arrays instead ofVecs inbind/tables) are flag tests or remove allocations.Root cause
Two separate things were counted as one:
60a08ef2, on in .01): with the commit Windows can still promise under its 4 GB floor, every compile was put off and the recording threads waited up to a second for each (15-128 µs a call above). #3347 (perf/regression-compilemem) turns it off by default; with it off, a call costs 3.7-4.8 µs, as on .28.03.e530c003. Of these tables, a run seeds 0 or 1 view, soSEEDSis empty for nearly every read; the host writes some of the textures (copies and resolves; release logs do not count them), soHOST_WRITTENandHOST_WRITTEN_ATsay "no" for most reads too. Still, aMutexacquisition is a locked read-modify-write of one cache line that all 8 recording threads (VICTORY_THIN_THREADS) take in turn, and even #2226'sRwLock::readwrites the lock word twice (count up, count down), so the line still moves between cores on every read.HOST_WRITTEN_ATstays aMutexin #2226, andtexture_atasks it on every read of a placed view.The same
host_wroteis also asked by the sweep's texture-table scan (resource::let_go_idle,slot_evict, the censuses) for every texture it looks at, on the submitting thread.Fix
On top of #2226 (unchanged), in
crates/victory-gpu/src/texture.rsonly:KeyFilter<WORDS>: a bit per hash of each key of a table, inAtomicU64words. A read loads one word: a clear bit says the key is not in the table, and the lock is not taken; a set bit says it may be, and the table is asked under its lock as before. The bits are written only under the table's write lock: a key's bit is set once the key is in, and when keys go the words are made again from the keys left (only), so a key still in has its bit in the old words and the new and is never missed, also by a reader that sees half-written words.SEEDS_KEYS(16 words, 1,024 bits: 0-1 views per run),HOST_WRITTEN_KEYSandHOST_WRITTEN_AT_KEYS(1,024 words, 65,536 bits each: some thousands of textures, a few % false "maybe").seed_pending's lookup isseed_take(filter, then a read lock, then the write lock to take it): of two threads reading the view at once, one takes it, as before.HOST_WRITTEN_ATis aRwLocktoo;host_written_likeandhost_written_block_likeask its filter first.placed_in_host_written(a range lookup, only when a placed texture is made) takes the read lock.upload_from,is_host_writtenandhost_wroteshare one path (filter, then the read lock).forget(every resource the title releases): each table's lock only for its own change, andHOST_WRITTEN_ATis walked only when the released resource was host-written (its entries are made only byhost_written); before, every release took that mutex and walked the whole map.Behaviour is unchanged: the filter only skips lookups whose answer is "not in the table".
Verification
Micro-benchmark (
texture::read_tables_tests::per_read_lookups_bench,#[ignore], CPU only):cargo test --release -p victory-gpu --lib texture::read_tables_tests::per_read_lookups_bench -- --ignored --nocaptureOne "texture read" is the three lookups a read can make:
seed_take(seed_pending),host_wrote(upload_from) andhost_written_like(texture_at, placed views). The tables hold 2,048 host-written textures; a read in 16 is of one, and a placed view in 16 lies in one's memory. Back to back is the worst case for a shared lock. "Work between" puts ~0.94 µs of a thread's own work (dependent multiplies) before each read and takes that work's own time off, closer to the recording threads, which spend ~3.8 µs a list call. Least of 3 (back to back) or 7 (spaced) timings. "Before" = main's / #2226'stexture.rswith the same bench (C:\lx\notes\agents\L\variants.pyputs it in; only the bench and aseed_takewrapper of each version's own locking are added). RTX 5080 PC, 9800X3D (8 cores, 16 threads), Windows 11, release profile, the shared heavy lock held (no game or build running beside it). Logs:C:\lx\notes\agents\L3\bench_{main,2226,final}.log(round1\: an earlier run with fewer spaced timings, same picture).ns a texture read (no view waiting for a seed / one view waiting):
41adb551(Mutexx3)RwLockfor SEEDS, HOST_WRITTEN)RwLockx3)By lookup, 8 threads back to back, no view waiting (main -> #2226 -> this branch): the seed 685 -> 234 -> 1.4 ns; host wrote 1,045 -> 189 -> 28 ns (the 1 in 16 that is host-written still takes the read lock); a copied texture at a placed view 923 -> 929 -> 21 ns. With work between, the spaced figures are differences of two ~950 ns timings: about +-4 ns of noise (this branch's parts read -1 to 10).
What that means:
RwLockread still writes the lock's word.HOST_WRITTENon a read (the "host wrote" column: 12.9 ns alone, 41 ns with work between on 8 threads); main added the seed (+4-21 ns) and, for placed views,HOST_WRITTEN_AT(+15-45 ns). This branch is under what .28.03 paid.host_wroteonce per texture it looks at: 12.9 -> 2.0 ns a call on one thread (0.8-1 ms less per scan of 72,000-100,000 textures; the sweep itself takes 62-149 ms).Tests (
cargo test -p victory-gpu --lib texture::, dev profile): 17 passed, 0 failed, 5 ignored (the timings), 965 filtered out. New intexture::read_tables_tests:a_seed_is_taken_once_and_its_bit_goes: another view does not wait; taken once; with nothing waiting every filter word is 0.a_bit_stays_while_its_view_waits: two views with the same bit; taking one leaves the other's bit;forgetof a seed's source drops it.of_threads_reading_one_view_at_once_one_takes_it: 50 rounds, 8 threads behind a barrier, exactly one gets the source.host_written_like_finds_a_copied_texture_by_its_layout: found by address and layout, not at another address or layout, gone afterforget(andhost_wrotefalse).KeyFilter::onlymade to clear every bit (words[word] |= bit & 0):a_bit_stays_while_its_view_waitsfails ("the twin still waits"), the other three pass. LogC:\lx\notes\agents\L3\negative.log.a73bb40a:cargo test -p victory-gpu --lib -- texture:: resource:: list:: draw:: descriptor::: 210 passed, 0 failed, 10 ignored, 767 filtered out.cargo check -p victory-gpu --all-targets: clean (no warning intexture.rs).cargo clippy -p victory-gpu --all-targets: no warning on the touched lines (one,manual_is_multiple_ofin the bench, fixed before the commit); the 25 lib / 24 test warnings left are main's own code. Not run with-D warnings: victory-shader's 3 warnings on main's code would stop it before victory-gpu.NOT tested
VICTORY_UPLOAD_GUARD=0as an upper bound. The saving per draw above is the benchmark's per-read figure times an assumed 4-16 texture reads a draw; the logs do not count reads per draw.host_wrote); more hits move those reads back to the read lock.e530c003(twoSetPredicationcalls and a deferred closure per upload from title memory, aWRITE_SUBMITTEDmutex per upload at submission): driver time, not measurable CPU-only. Uploads are per changed texture, not per draw.PLACED_TEXTURES(a global mutex on every read of a placed view, intexture_at) is as it was on .28.03 and untouched.victory-gputest suite (onlytexture::,resource::,list::,draw::,descriptor::were run) and the other crates (onlyvictory-gpuchanges).Overlap
SEEDSandHOST_WRITTENas RwLocks. This branch carries it unchanged (patch-id equal to itsad26de65) and builds on it. Without the filter it takesSEEDSandHOST_WRITTENoff the mutex but leavesHOST_WRITTEN_ATon one, and every read still writes the lock word.perf/regression-compilemem): the compile memory admission off by default. That is the "twice per draw"; this branch is the smaller rest.seed_waits,forget_seed,seed_pendingpeeks instead of taking). It conflicts textually with #2226 and this branch inseed_pending. Whichever lands second:forget_seedmust make the words again (SEEDS_KEYS.only(...), asseed_takedoes) andseed_waitsshould askSEEDS_KEYS.maybefirst.perf/card-copy-batch(ours, not opened): the submitting thread's card copies, the bigger part of what grew on that thread since .28.03 (7.1 -> 11.4 ms a frame above).perf/slot-evict-frees, #3272, #2890 (the sweep): the other part that grew. This branch only makes the sweep'shost_wrotecalls cheaper.SEEDS,HOST_WRITTEN,seed_pending,host_written_like, "per-draw cost": nothing else touches these tables (#3062 and #2862 touch other parts oftexture.rs).AI use
Claude Code (Claude Opus 5.5) read the per-second log lines of the existing bench runs (its own parsing scripts, listed above) to separate the compile admission from the per-draw cost, diffed the draw path between v2026.11.28.03 and main, wrote the filter, the tests and the benchmark, ran
cargo check/cargo test/cargo clippyand the benchmark, and wrote this description. The RwLocks are birdneststream's #2226, carried unchanged.🤖 Generated with Claude Code
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Lyrnc on 2026-11-30T18:59:15Z.
Rebased onto main
84bd61e2(v2026.11.30.02) and force-pushed (now2b50c173).#2226 is on main (
c1b845b5), so its commit dropped out and this PR is now one commit on top of main'sRwLocks. The patch is the same as before (git range-diff:=). The rebase had no conflicts: git skipped #2226's commit as already applied, and this PR's commit went on cleanly.Checks on
2b50c173(Windows 11, rustc 1.98.1, its own target dir):cargo check -p victory-gpupasses.cargo test -p victory-gpu --lib -- texture::read_tables_tests: 4 passed, 1 ignored (per_read_lookups_bench, a timing).Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-12-01T18:49:17Z.
Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.
Pull request closed