Repository navigation
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
Open
Lyrnc wants to merge 2 commits into
Conversation
…n every recording thread, written only when a view is seeded, a texture is written by the host, or forgotten
…ry read asks (SEEDS, HOST_WRITTEN, HOST_WRITTEN_AT) Every shader-resource read of a texture asks SEEDS (seed_pending) and HOST_WRITTEN (upload_from), and every read of a placed view asks HOST_WRITTEN_AT (texture_at), on all the recording threads. A run seeds 0 or 1 view and the host writes some thousands of textures, so nearly every answer is "not there"; yet a Mutex is a locked write of one cache line all eight threads take in turn, and even a RwLock read writes the lock word twice. Each table now has a KeyFilter: one bit per hash of each key, in atomic words. A clear bit says the key is not in the table and no lock is taken; a set one sends the read to the table as before. The bits are written only under the table's write lock: set once a key is in, made again from the keys left when keys go, so a key still in is never missed. HOST_WRITTEN_AT is a RwLock too. forget() takes each table's lock only for its own change and walks HOST_WRITTEN_AT only for a texture the host wrote (the only ones with entries there). Tests: a seed is taken once, by one of eight threads reading at once; a bit stays while another key with the same bit waits; a copied texture is found by its layout and gone once released. per_read_lookups_bench (#[ignore]) times a texture read on 1, 8 and 16 threads, back to back and with a microsecond of other work between reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 that e530c00 put 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):VICTORY_COMPILE_MEMORY=0, or + 299c4aa = 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 from e530c00,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; texture: SEEDS and HOST_WRITTEN behind a RwLock — asked of every texture read on every recording thread (3–5× cheaper under contention) #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:
perf/regression-compilemem) turns it off by default; with it off, a call costs 3.7-4.8 µs, as on .28.03.SEEDSis 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 texture: SEEDS and HOST_WRITTEN behind a RwLock — asked of every texture read on every recording thread (3–5× cheaper under contention) #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 texture: SEEDS and HOST_WRITTEN behind a RwLock — asked of every texture read on every recording thread (3–5× cheaper under contention) #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):
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.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.SetPredicationcalls 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 its ad26de6) 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 texture: SEEDS and HOST_WRITTEN behind a RwLock — asked of every texture read on every recording thread (3–5× cheaper under contention) #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, resource: check unmapped_only instead of host_idle.is_some() in let_go_idle #3272, GPU: look up slot-eviction candidates by the raw texture key (part of #2778) #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 (List: title copies name a held host copy's levels, and are fitted to the host's textures #3062 and GPU: count one alias-seed subresource per Texture3D mip #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