Repository navigation
perf: CPU-side optimizations (BCPack, detiling, inflate, hashing, write-watch journal, DSP) - #623
depaolaluigi123 wants to merge 2 commits into
Conversation
|
Measured on a real Windows PC with a real copy of the game, since the PR says none of this has been tested It does not merge with #614. Both rewrite The rest of this is a single-run observation, not a verdict, and I would not want it read as one:
So: not a claim that these optimizations are wrong — the copyback figure says otherwise — only that on this Filed with an AI assistant's help; the figures are from this PC's own run logs. |
|
Thanks for taking the time to build and measure this on Windows, and for flagging the conflict. On the numbers: as you noted, the build you measured includes the other open performance PRs but not this one, so it doesn't tell us yet how these changes behave on Windows. A warm-cache run of On the conflict with #614: both PRs touch Also a heads-up: this PR currently conflicts with |
0889d0c to
644ed02
Compare
|
Setup
Before vs after (medians)
Reading it: about 2.3 ms/frame (~3%) better, almost all of it in copies and texture refresh; AI Assisted: Open Code. || commit 2: testing |
|
Windows, warm cache: main PC: Ryzen 9 9950X3D, RTX 5080 16 GB (driver 621.10), 32 GB RAM, Windows 11, 3440x1440. 1. A bug after main's #932. Since #932, 2. It doesn't apply cleanly on
3. Results: about 5 minutes each in the opening, world time only, each build after its own warm-up.
The extra hitches came as 300–570 ms freezes in clusters, about once a minute. No pipelines were being made at those moments, so it isn't a warm-up gap. We didn't find the cause and didn't try the per-change switches one by one. DLSS frame generation reset its frame timer 21 times, against 5. An earlier run on an older base, with only 8 of the CPU's 32 processors enabled, was also choppier: 75 against 10 frames over 100 ms in about 2.5 minutes. (Written with Claude; numbers are from this PC's logs.) |
36e5e91 to
ba9ebf1
Compare
|
Thanks @lordnyx0 and @SkylerizMe for the detailed Windows runs. They found real problems, and the branch is now updated (rebased on
About the 300–570 ms freezes, I can't reproduce them here, so I can't say what causes them. One thing I did find: on a sparse journal, the first version of the two-level journal ( If you get the chance, a warm run of the new head (
That would show whether the hitches are still there and whether one of these changes is behind them. |
CPU-side optimizations of the layer, each checked against the original code. IMPORTANT, how this was tested: the author of these changes does not own a copy of the game. Nothing here has been run with the game, and nothing has been run on Windows. Every number below comes from benchmarks on synthetic data that was made up for the purpose (textures, streams, impulse responses and memory journals), on Linux, one core of an Intel i7-13620H (AVX2, VAES, no AVX-512). Before merging, run `cargo test` on Windows. victory-gpu - watch.rs: the ranges asked ahead (`ahead`) were walked linearly by every `writes`, `asked`, `nothing_new` and `ask_more` call. They are now sorted, with a running maximum of the ends, and answered by binary search. 5,000 lookups: 7x faster with 50 ranges, 49x with 500, 173x with 2,000. In `ask_rest`, a range inside one asked just before it is not asked again. main's locking (only the pages asked about are held) is kept as it is. The equivalence test is in the patch. - AddressHash, already in the project, in place of SipHash in per-frame maps (draw.rs, descriptor/host.rs, texture.rs, and memory.rs, which is used only by the ray-tracing builder): lookups 3.7-4.4x faster (8-17 -> 2-5 ns). The frame saving is an estimate: the lookups a frame were not counted. - texture.rs `digest`: AES rounds (VAES on 256 bits, AES-NI fallback, the same value either way; the old code is used without AES): 2.4-3.3x faster on data in cache, 1.5-1.7x from RAM. 4,000 variants (zero runs, swaps, single-bit flips) all distinct; a test checks that both paths agree. victory-hle - dstorage.rs `inflate`: libdeflate (C, MIT, whole-buffer decompressor) in place of main's reused zlib-rs state: 1.33-1.40x faster on meshes, text, already-packed and sparse data, in 64 KiB and 1 MiB chunks (measured against a reused, reset zlib-rs state; zlib-ng was slower than zlib-rs). All cases of the existing test hold, plus trailing bytes and a corrupt checksum. - dsp/convolve.rs, the DSP convolution, reworked: real-input FFTs (realfft) instead of a 2048-point complex FFT (8x faster); the sum over partitions register-blocked with AVX2+FMA (32 bins' sums kept in registers while every partition goes by, 1.3-2.1x faster); spectrum rows 64-byte aligned (misaligned rows measured 1.2-1.4x slower; a [bin][partition] layout measured 6-12x slower). A whole block is 1.6-7.6x faster, depending on the length of the response. Output within about 1e-6 of the original; existing tests plus new tests for the vector sum and against the original. victory-formats - bcpack.rs: BC7 block rebuilding with a compile-time 256-entry nibble table, anchors as a bit mask, and a bit writer on two u64 words instead of variable u128 shifts: 1.6x faster (150 -> 94 ns a block, all 8 BC7 modes, output identical on 80,000 random blocks). - bcpack.rs `lanes`: each round in two passes, every lane's step first (they are independent, so the core overlaps them on several ALUs), then the byte hand-out in lane order. BC7 end to end 1.11x; about 1.3x for all of BC7 together with the item above. - texture.rs: detiling of standard (S) modes as a transpose. A 256-byte micro tile is a column-major matrix of 16-byte cells; each micro tile is transposed in registers (vperm2i128) and written as whole 64-byte rows (two micro tiles side by side for 4-byte elements), with streaming stores where the destination is 64-byte aligned, as D3D12 upload memory is. The cell rule is checked against Layout::offset; everything else, the pipe and bank XOR modes included (their addresses are not a row's part plus a column's), takes the general path. Against main's general path, 2048^2 textures: 64KB_S 1.73x (8-byte elements), 2.20x (16), 2.46x (4); 4KB_S 1.92x; D modes unchanged. Tests compare it with the general path over 6 swizzle modes, 6 formats, odd and even sizes, mips, slices and aligned and unaligned destinations, and over the XOR modes of both consoles into aligned rows. Checks: victory-formats tests pass on Linux (58, every run of 25 in a row). victory-gpu and victory-hle no longer build on Linux, so the whole workspace was checked with cargo clippy --workspace --all-targets [--all-features] --locked --target x86_64-pc-windows-msvc: no errors, and no warnings in the files this changes. libdeflate's C build was stubbed for that Linux check, so it still needs a real MSVC build. Measured and left out: AVX2 16-lane rANS (0.94-1.10x), 4x-unrolled safe_merge assembly (0.85x on realistic change rates), target-cpu=x86-64-v3 and LTO (no gain on these kernels). Changes to the shader translator (FxHash maps, a dominance-based value table in the DXIL optimiser) were dropped: against main at d4b5d51 they measured no gain, with identical output. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… C, f16 reverb Four deeper changes, kept apart from the first commit on purpose: they replace a core data structure (the write-watch journal), add assembly with its own fault-handling labels, add C code to a crate that had none, and add an approximate reverb. That makes them easier to review, test on Windows and revert one by one. Each one can also be switched at run time (see below), except the journal. IMPORTANT, how this was tested: the author of these changes does not own a copy of the game. Nothing here has been run with the game, and nothing has been run on Windows. Every number below comes from benchmarks on synthetic data that was made up for the purpose (random journals and ranges, buffers with made-up contents, random BCPack streams, made-up impulse responses), on Linux, on one core of an Intel i7-13620H (AVX2, VAES, F16C, no AVX-512). victory-gpu: the write-watch journal as a two-level page table - New watch/pages.rs replaces the journal's BTreeMap<page, asking>. Pages are grouped in chunks of 4,096 (256 MiB of title memory) made when a page of theirs is first noted. Each chunk keeps every page's asking (0: never), a bit per written page and a bit per word of those bits in use. Questions go through the words in use only: a word with few written pages visits them by their bits (tzcnt), a word with many is compared whole with AVX2 (4 pages an instruction). A range over more chunks than there are goes through the chunks there are, in order, not through every chunk number it covers. - Measured against the BTreeMap, journals of 2,000 and 200,000 written pages over 64 GiB: ranges of 16 pages 4.3-5.1x faster (list and nothing_new); 4,096 pages 0.95-1.7x; a range over the whole journal 1.05x when it is full; on a sparse journal such a range takes a few microseconds, 2.5-3x the tree's. Notes 19-28x faster. - An earlier version of this commit went through every chunk number of a range: a range over terabytes of addresses took milliseconds (up to 4.7 ms a question). Fixed here. - An equivalence test compares every answer with a BTreeMap over random notes and ranges, across chunk borders. victory-gpu: copy and digest in one pass, in assembly - safecopy.rs: victory_safe_copy_hash copies blocks of 128 bytes from title memory and takes each through the AES digest's round (VAES), reading the source once. The source loads are inside new fault labels in failing_exit; on any fault the call fails and the caller starts again the original way. It uses only ymm0-ymm5, the volatile registers of the Windows calling convention, so nothing needs saving. - texture.rs: copy_digest() uses it where a copy of title memory was followed by digest(): the refresh of textures in view memory, and fingerprint(). It gives the same digest as digest(). It only runs with the safe copy on, no soft views and VAES present; otherwise, or on a fault, it does copy_mapped() then digest(), as before. - Measured 1.16-1.39x faster than the safe copy (rep movsb) followed by the VAES digest, for 64 KiB to 8 MiB. The same bytes and the same digest as copy-then-digest for every length from 0 to 700 and for large ones. - New Windows test: the same bytes and digest as copy-then-digest, and a decommitted page makes the call fail without a crash. - VICTORY_COPY_DIGEST=0 turns it off. victory-formats: BCPack lane decoding in C - csrc/bcpack_lanes.c, built with the cc crate (MSVC on Windows) from a new build.rs: the same two-pass schedule as the Rust lanes(), which stays as the reference and the fallback. - decode() measured 1.17-1.20x faster on top of the first commit's optimizations (BC1 82 -> 98 MB/s, BC4 76 -> 89, BC7 50.5 -> 59.4). - The round-trip test now also checks that the C version gives the same records as the Rust one on every lane layout, and fails on truncated streams. - xtask/src/policy.rs: "c" added to the asset policy's allowed extensions, for this one file. That is the maintainers' call: without it, the C version (and this line) can be dropped and the Rust lanes() kept. - VICTORY_BCPACK_C=0 turns it off. victory-hle: the reverb's impulse response as half floats, opt-in - dsp/convolve.rs can keep the response as half floats (F16C) and widen them as they are loaded. That halves the bytes brought in from memory for every block, which is the limit with long responses. - A whole block, measured: 1.03x faster with a 0.2 s response, 1.15x with 1 s, 1.97x with 3 s, 1.82x with 6 s. - This is an approximation: the output differs from the float version by about -73 dB of its peak. On a_mono_convolution_is_computed's input that is up to 1.6e-4, over the 1e-4 the test allows; so it is off by default, and that test is unchanged. VICTORY_DSP_HALF=1 turns it on; a test bounds its error at -60 dB. Checks: victory-formats tests pass on Linux; the journal's and the convolution's tests pass on Linux from copies of the same files (those crates no longer build there). The whole workspace passes cargo clippy --workspace --all-targets [--all-features] --locked --target x86_64-pc-windows-msvc with no errors and no warnings in the files this changes; the C builds (libdeflate, BCPack) used a stand-in compiler for that, so they still need a real MSVC build. xtask's policy test passes for these files (main's lower.rs is over its size limit on its own). Before merging, run cargo test on Windows: the safecopy fault test and watch.rs's tests only run there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ba9ebf1 to
fbe9a84
Compare
This pull request is a set of performance optimizations across several parts of the layer: BCPack, texture detiling, DirectStorage inflate, texture hashing, the write-watch journal, and the DSP convolution.
How it was made and tested. All of this was written on Linux. None of it has been tested on Windows. I don't own a copy of the game, so every measurement comes from benchmarks on synthetic data made up for the purpose: made-up textures, BCPack streams, memory journals and impulse responses. They ran on one core of an Intel i7-13620H (AVX2, VAES, F16C, no AVX-512). Every change was checked against the original code, with byte-identical output wherever the result has to be exact.
Two commits, split by depth.
perf: faster BCPack, detiling, inflate, hashing, DSP) holds the less invasive changes: local rewrites of hot functions and data structures that keep the same interfaces.perf: two-level write-watch journal, fused copy+digest asm, BCPack in C, f16 reverb) holds the deeper ones. It replaces a core data structure, adds assembly with its own fault-handling labels, adds C code to a crate that had none, and adds an approximate (opt-in) reverb.They were kept as two commits so that they can be reviewed, tested and reverted separately.
These changes need testing by the maintainers on Windows to see whether they actually improve performance there and are worth keeping. The numbers below come from isolated benchmarks, not from the running application.
Changes since the first version of this PR
Rebased on
mainatf9dfb76, and reworked after the reports in this thread:addressable(), and the transposed detiling took them, but their addresses are not "a row's part plus a column's". They now take the general path. The column offsets usechecked_subas well. A new test detiles every XOR mode of both consoles into 64-byte-aligned rows, the case that madexor_modes_tile_where_offset_pointsfail now and then; it fails without the fix.mainthey measured no gain (same output, same time), so FxHash in the translator, the DXIL optimiser's dominance table andspeed_bench.rsare gone. This PR no longer touchesvictory-shader.VICTORY_DSP_HALF=1). Its error is up to 1.6e-4 ona_mono_convolution_is_computed's input, over the 1e-4 that test allows. Off by default, that test passes unchanged."c"added toALLOWED_EXTENSIONSinxtask/src/policy.rs, forcrates/victory-formats/csrc/bcpack_lanes.c. That is your call: if C files should stay out, the C version can be dropped and the Rustlanes()from commit 1 kept.watch.rsis ported onto main's newerask_restlocking (it holds only the pages it asks about) and onto main's owner-told memory (owned()), anddraw.rskeeps main's ownheap_buffer_passrework.memory.rskeeps main's fault budget (Faults), withAddressHashfor its tables.Commit 1: less invasive changes
watch.rs: ranges asked aheadwrites/asked/nothing_new/ask_moreAddressHashinstead of SipHash indraw.rs,descriptor/host.rs,texture.rs,memory.rstexture.rsdigestdstorage.rsinflatedsp/convolve.rsrealfft) instead of a 2048-point complex one (8x). The partition sum register-blocked with AVX2+FMA (1.3-2.1x). Spectrum rows 64-byte alignedbcpack.rstexture.rsdetilingvperm2i128) and written as whole 64-byte rows, with streaming stores when the destination is 64-byte aligned, as D3D12 upload memory is. Checked againstLayout::offset; XOR modes and anything else take the general pathCommit 2: deeper changes
watch/pages.rsreplaces the journal'sBTreeMapwith a two-level page table: chunks of 4,096 pages made on first use, each with dense askings, a bit per written page and a bit per word in use. Only words in use are visited: sparse words withtzcnt, dense words compared with AVX2. A range over more chunks than there are goes through the chunks there areBTreeMap, journals of 2,000 and 200,000 pages: ranges of 16 pages 4.3-5.1x; 4,096 pages 0.95-1.7x; a whole full journal 1.05x; a whole sparse journal a few µs, 2.5-3x slower than the tree. Notes 19-28x. Equivalence test against aBTreeMapsafecopy.rsvictory_safe_copy_hash: copies 128-byte blocks of title memory and runs the VAES digest round on them, reading the source once. The source loads sit inside new fault labels; on any fault the caller falls back tocopy_mapped+digest. Uses only ymm0-ymm5.texture.rscopy_digest()uses it for view-memory texture refreshes andfingerprint()rep movsb) followed by the VAES digest. Same bytes and same digest for every length. New Windows test: a decommitted page fails cleanlyVICTORY_COPY_DIGEST=0csrc/bcpack_lanes.c, built withccfrom a newbuild.rs: the same two-pass schedule, with Rustlanes()kept as reference and fallbackdecode()1.17-1.20x on top of commit 1 (BC1 82 → 98 MB/s, BC4 76 → 89, BC7 50.5 → 59.4). Same records as Rust on every layout and on truncated streamsVICTORY_BCPACK_C=0VICTORY_DSP_HALF=1Expected effect
Estimates from the isolated measurements, to be confirmed on Windows:
Checks done here, and what is left
victory-formatstests pass (59).victory-gpuandvictory-hleno longer build on Linux, so the journal and convolution tests were run from copies of the same files.cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc, with and without--all-features: no errors, and no warnings in the files this PR changes. (With-D warningsthe build currently stops on amanual_checked_divlint in main'scrates/victory-shader/src/emit/lower.rs, which this PR does not touch.)crates/victory-shader/src/emit/lower.rs(over the size limit), independently of this PR.cargo test --workspace: the safecopy fault test andwatch.rs's tests only run there;Benchmarks to reproduce the BCPack numbers:
cargo test --release -p victory-formats bcpack_throughput -- --ignored --nocapture, with and withoutVICTORY_BCPACK_C=0.🤖 Generated with Claude Code