Skip to content

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
trustno1-coder:mainfrom
Lyrnc:perf/per-draw-cost
Open

Lyrnc wants to merge 2 commits into
trustno1-coder:mainfrom
Lyrnc:perf/per-draw-cost

Conversation

@Lyrnc

@Lyrnc Lyrnc commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

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 SEEDS mutex 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. a VICTORY_THIN line with >= 20,000 calls a frame; scripts C:\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):

Build Runs µs a call
v2026.11.28.03 t2803_warm, t2803_r1, t2803_s1 3.61, 3.79, 4.52
829188e (.01), compile admission on t2901_r3, t2901_warm, t2901_r2, t2901_nofold_r1/_w (folding off) 15.5, 71.0, 128.2, 109.2 / 128.2
829188e, admission off (VICTORY_COMPILE_MEMORY=0, or + 299c4aa = our #3347) t2901_cm0_r1, tfix_s1, tfix_r1, m_tfix_2, c_tfix_1, m_tfix_1, tfix_r2 3.73, 3.78, 3.80, 3.68, 3.72, 4.09, 4.81

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):

ms a frame .28.03 (t2803_s1) .01 (t2901_s1) .01 + #3347 (tfix_s1)
in its submissions 52.3 (5.30 µs a draw) 56.2 (5.68) 50.5 (5.38)
copies in the card's memory 7.1 11.7 11.4
heaps' records + sweep 1.3 3.5 3.4
refreshes of host copies 18.5 17.6 14.7
asking the write watch 4.1 3.1 3.1

(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):

  • every shader-resource read of a texture: seed_pending takes the global SEEDS mutex to learn that nothing waits (in 9 world runs: 0 or 1 view ever seeded);
  • every read of a view the title placed: texture_at takes the global HOST_WRITTEN_AT mutex (host_written_like) before its own PLACED_TEXTURES one;
  • every read, as on .28.03: upload_from takes HOST_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 and SEEDS RwLocks).
    The other draw-path changes (finish_depth_writes, the frame-generation camera check for 3-vertex draws, barstat::on(), fixed arrays instead of Vecs in bind/tables) are flag tests or remove allocations.

Root cause

Two separate things were counted as one:

  1. The doubling is the compile memory admission (60a08ef, 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). Compile memory admission off by default (VICTORY_COMPILE_MEMORY=1 turns it on) #3347 (perf/regression-compilemem) turns it off by default; with it off, a call costs 3.7-4.8 µs, as on .28.03.
  2. What is left on the draw path is a global lock or two per texture read, from e530c00. Of these tables, a run seeds 0 or 1 view, so SEEDS is empty for nearly every read; the host writes some of the textures (copies and resolves; release logs do not count them), so HOST_WRITTEN and HOST_WRITTEN_AT say "no" for most reads too. Still, a Mutex acquisition 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's RwLock::read writes the lock word twice (count up, count down), so the line still moves between cores on every read. HOST_WRITTEN_AT stays a Mutex in texture: SEEDS and HOST_WRITTEN behind a RwLock — asked of every texture read on every recording thread (3–5× cheaper under contention) #2226, and texture_at asks it on every read of a placed view.

The same host_wrote is 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.rs only:

  • KeyFilter<WORDS>: a bit per hash of each key of a table, in AtomicU64 words. 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_KEYS and HOST_WRITTEN_AT_KEYS (1,024 words, 65,536 bits each: some thousands of textures, a few % false "maybe").
  • seed_pending's lookup is seed_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_AT is a RwLock too; host_written_like and host_written_block_like ask 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_written and host_wrote share one path (filter, then the read lock).
  • forget (every resource the title releases): each table's lock only for its own change, and HOST_WRITTEN_AT is walked only when the released resource was host-written (its entries are made only by host_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 --nocapture

One "texture read" is the three lookups a read can make: seed_take (seed_pending), host_wrote (upload_from) and host_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's texture.rs with the same bench (C:\lx\notes\agents\L\variants.py puts it in; only the bench and a seed_take wrapper 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):

Tables 1 thread 8 threads 16 threads 1 thread, work between 8 threads, work between
main 41adb55 (Mutex x3) 36.5 / 41.5 1,235 / 1,222 2,580 / 2,577 46.3 / 51.2 108.9 / 88.2
+ #2226 (RwLock for SEEDS, HOST_WRITTEN) 36.3 / 40.6 1,334 / 1,239 2,646 / 2,606 46.9 / 51.8 76.1 / 114.9
+ this branch (filters, RwLock x3) 7.7 / 7.8 34.3 / 34.7 66.4 / 66.5 10.4 / 10.2 10.7 / 13.5

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:

  • texture: SEEDS and HOST_WRITTEN behind a RwLock — asked of every texture read on every recording thread (3–5× cheaper under contention) #2226 alone halves the seed and host-wrote lookups back to back, but leaves the placed-view mutex, and with work between it is within noise of main (76-115 against 88-109 ns): a RwLock read still writes the lock's word.
  • With this branch a read costs 10-14 ns with work between on 8 threads, against 88-109 on main: 75-100 ns less a texture read. With 4-16 texture reads a draw (assumed, not counted), that is 0.3-1.6 µs of recording-thread time a draw, against ~29 µs of recording-thread time a draw in m_tfix_2 (178 ms over 6,115 draws): 1-5 % of the recording threads. They are not the frame's bottleneck (the submitting thread is, perf30 CAUSES cause 5), so the frame gains less.
  • .28.03 asked only HOST_WRITTEN on 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.
  • The sweep's scan asks host_wrote once 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 in texture::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; forget of 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 after forget (and host_wrote false).
  • Negative control: KeyFilter::only made to clear every bit (words[word] |= bit & 0): a_bit_stays_while_its_view_waits fails ("the twin still waits"), the other three pass. Log C:\lx\notes\agents\L3\negative.log.
  • The modules around the tables' callers, on a73bb40: 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 in texture.rs). cargo clippy -p victory-gpu --all-targets: no warning on the touched lines (one, manual_is_multiple_of in 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

  • In-game A/B. No game run was made for this branch: not .04 vs .04 + texture: SEEDS and HOST_WRITTEN behind a RwLock — asked of every texture read on every recording thread (3–5× cheaper under contention) #2226 vs this branch, not VICTORY_UPLOAD_GUARD=0 as 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.
  • How many of the world's placed-view reads land on a host-written texture's memory (the bench assumes 1 in 16, the same as for host_wrote); more hits move those reads back to the read lock.
  • The guard's other per-upload work from e530c00 (two SetPredication calls and a deferred closure per upload from title memory, a WRITE_SUBMITTED mutex 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, in texture_at) is as it was on .28.03 and untouched.
  • Linux/Wine, other CPUs (the 8-core 9800X3D has one CCD; a two-CCD part moves the lock line between CCDs, which costs more).
  • The rest of the victory-gpu test suite (only texture::, resource::, list::, draw::, descriptor:: were run) and the other crates (only victory-gpu changes).

Overlap

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 clippy and the benchmark, and wrote this description. The RwLocks are birdneststream's #2226, carried unchanged.

🤖 Generated with Claude Code

Your Name and others added 2 commits November 30, 2026 07:12
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant