shadow: 32-byte AVX2 fast-skip in merge_piece #440

Open
Paliton wants to merge 2 commits from Paliton/Darner-fork:pr/avx2-merge-piece into Main
First-time contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

  • The problem on main: shadow::merge_piece (crates/darner-gpu/src/shadow.rs) compared and updated now, held, and title byte-by-byte, writing every byte of title and held even across unchanged stretches during GPU-to-CPU shadow read-back merges.

Ports the 32-byte AVX2 fast-skip path for merge_piece from ProjectLeonida/mandy 653f60d37 (PR #2699, bobcatchris, GPL-3.0), runtime-dispatched via is_x86_feature_detected!("avx2") with a scalar fallback (merge_piece_scalar). Refined so that when a 32-byte chunk has differences, only the differing byte positions (trailing_zeros over the negated equality mask) are stored into title — avoiding writing neighbouring unchanged bytes that a title thread may be updating concurrently. Adds unit test merge_piece_avx2_matches_scalar_across_lengths_and_patterns.

How it was tested

  • Tested revision: 545354f (based on f3840d7)

  • Commands and results:

    • cargo xtask policy -> asset policy: 555 files ok
    • cargo clippy --workspace --all-targets --locked -- -D warnings -> ok (0 warnings)
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings -> ok (0 warnings)
    • cargo test -p darner-gpu --lib --locked merge_piece -- --test-threads=1 -> 1 passed; 0 failed (merge_piece_avx2_matches_scalar_across_lengths_and_patterns)
    • cargo test --workspace --locked -> all non-flaky tests pass (1773/1774 in darner-gpu; the single failure is the pre-existing watch::tests::ask_once_by_res_splits_reservations_without_refusals flake fixed by #262)
  • Changes a default: yes (automatic AVX2 dispatch on x86_64 CPUs that support AVX2; identical observable output, stricter non-overwriting writes to title).

  • A/B result:

    1. Isolated CPU microbenchmark (rustc -O, Intel Core i7-14700KF, 4 MiB buffer, 200 iterations, 2 runs):

      Pattern (4 MiB) A (scalar) B (AVX2)
      0% dirty (unchanged) 117.4–126.2 us/MiB (8.3–8.9 GB/s) 57.3–61.3 us/MiB (17.1–18.3 GB/s, 2.05×–2.06×)
      0.1% dirty (every 1024 B) 120.7–121.3 us/MiB (8.7 GB/s) 75.1–76.1 us/MiB (13.8–14.0 GB/s, 1.59×–1.61×)
      1% dirty (every 100 B) 117.1–119.8 us/MiB (8.8–9.0 GB/s) 98.2–99.9 us/MiB (10.5–10.7 GB/s, 1.19×–1.20×)
    2. In-game A/B (2 runs each, 240 s automated opening sequence, --profile diag --features diagnostics, pure play-defaults, RTX 5070 12 GB + i7-14700KF, Series X, 1440p -> 4K, DLSS off, FG off, RT off). Note this rig is primarily GPU-bound at 1440p -> 4K (run-to-run GPU variance ~1 ms), so average FPS gain is partly within GPU noise, while p99 spikes dropped cleanly and read-backs were spotless:

      Metric (2 runs each) A (off, f3840d7) B (on, 545354f)
      Total frames (240 s) 5,367 / 5,537 (mean 5,452) 5,496 / 5,577 (mean 5,537, +85)
      Window 2 (130-180 s) fps / med ms 21.24 / 21.86 fps (45.77 / 44.46 ms) 22.25 / 22.33 fps (43.72 / 43.63 ms)
      Window 3 (180-235 s) fps (mean) 21.52 / 22.16 (21.84 fps) 22.55 / 22.50 (22.52 fps, +0.69)
      Window 3 median frame time 45.26 / 43.89 (44.58 ms) 43.25 / 43.34 (43.30 ms, -1.28 ms)
      Window 3 p95 / p99 frame time 47.1 / 94.3 ms (mean) 45.5 / 84.0 ms (p99 -10.3 ms)
  • Gates run on that revision: all three AGENTS.md gates + cargo xtask policy on 545354f.

  • Gameplay, when relevant: RTX 5070 12 GB + i7-14700KF, Series X, 1440p -> 4K, DLSS off, FG off, RT off. Zero read-back corruption or regressions.

  • Hardware dependence: x86_64 CPUs with AVX2 (safe scalar fallback elsewhere).

  • Coverage gaps: in-game A/B was run on a GPU-bound system rather than a CPU-bound one; non-AVX2 fallback verified via direct merge_piece_scalar equivalence in unit tests.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #430, #339, #332, #321, #319, #310, #218, #214 (also touch crates/darner-gpu/src/shadow.rs, non-overlapping functions)
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: ## What it changes - The problem on main: `shadow::merge_piece` (`crates/darner-gpu/src/shadow.rs`) compared and updated `now`, `held`, and `title` byte-by-byte, writing every byte of `title` and `held` even across unchanged stretches during GPU-to-CPU shadow read-back merges. Ports the 32-byte AVX2 fast-skip path for `merge_piece` from `ProjectLeonida/mandy` `653f60d37` (PR #2699, `bobcatchris`, GPL-3.0), runtime-dispatched via `is_x86_feature_detected!("avx2")` with a scalar fallback (`merge_piece_scalar`). Refined so that when a 32-byte chunk has differences, only the differing byte positions (`trailing_zeros` over the negated equality mask) are stored into `title` — avoiding writing neighbouring unchanged bytes that a title thread may be updating concurrently. Adds unit test `merge_piece_avx2_matches_scalar_across_lengths_and_patterns`. ## How it was tested - Tested revision: `545354f` (based on `f3840d7`) - Commands and results: - `cargo xtask policy` -> `asset policy: 555 files ok` - `cargo clippy --workspace --all-targets --locked -- -D warnings` -> ok (0 warnings) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` -> ok (0 warnings) - `cargo test -p darner-gpu --lib --locked merge_piece -- --test-threads=1` -> `1 passed; 0 failed` (`merge_piece_avx2_matches_scalar_across_lengths_and_patterns`) - `cargo test --workspace --locked` -> all non-flaky tests pass (`1773/1774` in `darner-gpu`; the single failure is the pre-existing `watch::tests::ask_once_by_res_splits_reservations_without_refusals` flake fixed by #262) - Changes a default: yes (automatic AVX2 dispatch on x86_64 CPUs that support AVX2; identical observable output, stricter non-overwriting writes to `title`). - A/B result: 1. **Isolated CPU microbenchmark** (`rustc -O`, Intel Core i7-14700KF, 4 MiB buffer, 200 iterations, 2 runs): | Pattern (4 MiB) | A (scalar) | B (AVX2) | |---|---|---| | 0% dirty (unchanged) | 117.4–126.2 us/MiB (8.3–8.9 GB/s) | **57.3–61.3 us/MiB (17.1–18.3 GB/s, 2.05×–2.06×)** | | 0.1% dirty (every 1024 B) | 120.7–121.3 us/MiB (8.7 GB/s) | **75.1–76.1 us/MiB (13.8–14.0 GB/s, 1.59×–1.61×)** | | 1% dirty (every 100 B) | 117.1–119.8 us/MiB (8.8–9.0 GB/s) | **98.2–99.9 us/MiB (10.5–10.7 GB/s, 1.19×–1.20×)** | 2. **In-game A/B** (2 runs each, 240 s automated opening sequence, `--profile diag --features diagnostics`, pure `play-defaults`, RTX 5070 12 GB + i7-14700KF, Series X, 1440p -> 4K, DLSS off, FG off, RT off). Note this rig is primarily GPU-bound at 1440p -> 4K (run-to-run GPU variance ~1 ms), so average FPS gain is partly within GPU noise, while p99 spikes dropped cleanly and read-backs were spotless: | Metric (2 runs each) | A (off, `f3840d7`) | B (on, `545354f`) | |---|---|---| | Total frames (240 s) | 5,367 / 5,537 (mean 5,452) | 5,496 / 5,577 (mean 5,537, +85) | | Window 2 (130-180 s) fps / med ms | 21.24 / 21.86 fps (45.77 / 44.46 ms) | 22.25 / 22.33 fps (43.72 / 43.63 ms) | | Window 3 (180-235 s) fps (mean) | 21.52 / 22.16 (21.84 fps) | 22.55 / 22.50 (22.52 fps, +0.69) | | Window 3 median frame time | 45.26 / 43.89 (44.58 ms) | 43.25 / 43.34 (43.30 ms, -1.28 ms) | | Window 3 p95 / p99 frame time | 47.1 / 94.3 ms (mean) | 45.5 / **84.0 ms** (p99 -10.3 ms) | - Gates run on that revision: all three `AGENTS.md` gates + `cargo xtask policy` on `545354f`. - Gameplay, when relevant: RTX 5070 12 GB + i7-14700KF, Series X, 1440p -> 4K, DLSS off, FG off, RT off. Zero read-back corruption or regressions. - Hardware dependence: x86_64 CPUs with AVX2 (safe scalar fallback elsewhere). - Coverage gaps: in-game A/B was run on a GPU-bound system rather than a CPU-bound one; non-AVX2 fallback verified via direct `merge_piece_scalar` equivalence in unit tests. ## Checklist - [x] I ran this change in the game, or say below why it can't be (docs, tests only). - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #430, #339, #332, #321, #319, #310, #218, #214 (also touch `crates/darner-gpu/src/shadow.rs`, non-overlapping functions) - Replaces or overlaps: none found
Contributor

Test report, together with #442 (both merged on A): no measurable change in this scene, and nothing wrong on screen.

PC: RTX 4080 16 GB (driver 621.40), i7-13700K (24 logical processors), 64 GB, Windows 11 26300. Series X profile, 1440p with DLSS Quality, frame generation off, hardware ray tracing on, Reflex off, DARNER_ALIAS_SHARE=1, DARNER_D3D12_BACKGROUND=0.
Method: the opening scene of one save, entered the same way every run (no input after the start screen); medians of per-second values from the host log in three 45-second windows from the moment the world appears; warm pipelines; one run each. A = main 47d21cd + #334, #337, #457, #274, #397, #312, #304, #303, #438, #439.

A A + #440 (f24eeae) + #442 (c971d13)
frame time, ms 25.3 / 25.2 / 24.8 25.3 / 25.1 / 24.9
landing: merging what was fetched, ms a frame (all threads) 1.37 / 1.39 / 1.40 (3.34 / 3.39 / 3.37) 1.34 / 1.35 / 1.34 (3.28 / 3.32 / 3.25)
texture refreshes, ms a frame (all threads) 1.0 / 1.3 / 1.3 1.5 / 1.2 / 1.3

The merge got about 0.05 ms a frame (3%) faster, within the spread; the scene is static, so little is merged or re-hashed.

Not tested: more than one run each, scenes with streaming (driving), other CPUs.

Test report, together with #442 (both merged on A): no measurable change in this scene, and nothing wrong on screen. **PC:** RTX 4080 16 GB (driver 621.40), i7-13700K (24 logical processors), 64 GB, Windows 11 26300. Series X profile, 1440p with DLSS Quality, frame generation off, hardware ray tracing on, Reflex off, `DARNER_ALIAS_SHARE=1`, `DARNER_D3D12_BACKGROUND=0`. **Method:** the opening scene of one save, entered the same way every run (no input after the start screen); medians of per-second values from the host log in three 45-second windows from the moment the world appears; warm pipelines; one run each. A = main 47d21cd + #334, #337, #457, #274, #397, #312, #304, #303, #438, #439. | | A | A + #440 (f24eeae) + #442 (c971d13) | |---|---|---| | frame time, ms | 25.3 / 25.2 / 24.8 | 25.3 / 25.1 / 24.9 | | landing: merging what was fetched, ms a frame (all threads) | 1.37 / 1.39 / 1.40 (3.34 / 3.39 / 3.37) | 1.34 / 1.35 / 1.34 (3.28 / 3.32 / 3.25) | | texture refreshes, ms a frame (all threads) | 1.0 / 1.3 / 1.3 | 1.5 / 1.2 / 1.3 | The merge got about 0.05 ms a frame (3%) faster, within the spread; the scene is static, so little is merged or re-hashed. Not tested: more than one run each, scenes with streaming (driving), other CPUs.
First-time contributor

Tested merged on c8258f9 on Intel Core Ultra 7 265KF (AVX2) across 3 full Series X sessions (30,000+ frames, up to 537,076 texture uploads / 26,087 MB uploaded per run): zero corruption or crashes, land merge cost measured 0.00 ms per frame in the steady state, and policy / clippy --all-features -D warnings / 1,819 darner-gpu tests passed cleanly.

Tested merged on `c8258f9` on Intel Core Ultra 7 265KF (AVX2) across 3 full Series X sessions (30,000+ frames, up to `537,076` texture uploads / `26,087 MB` uploaded per run): zero corruption or crashes, `land` merge cost measured `0.00 ms` per frame in the steady state, and `policy` / `clippy --all-features -D warnings` / `1,819 darner-gpu` tests passed cleanly.
Contributor

I tested f24eeae on a Core Ultra 5 225F. The merge output matched main (d91d8e4) across 1,026 synthetic lengths, including unaligned starts. For reused 64 KiB buffers, the median went from 0.032 to 0.017 ns/byte at 0% dirty, to 0.187 at 10% dirty, and to 0.719 at 100% dirty. Five alternating samples per side, compiled with rustc -C opt-level=3.

A small follow-up restored the fully dirty case to 0.033 ns/byte. When the held comparison mask is zero and all 32 title bytes also differ from the new values, it uses one vector store. Other masks keep your per-byte stores. The 97 dense merge checks and the repository's focused merge_piece test passed. The scattered 10% case is still slower in this test. I haven't measured how often dense merges occur during play, so this is complementary to the in-game results above.

I tested `f24eeae` on a Core Ultra 5 225F. The merge output matched `main` (`d91d8e4`) across 1,026 synthetic lengths, including unaligned starts. For reused 64 KiB buffers, the median went from 0.032 to 0.017 ns/byte at 0% dirty, to 0.187 at 10% dirty, and to 0.719 at 100% dirty. Five alternating samples per side, compiled with `rustc -C opt-level=3`. A small follow-up restored the fully dirty case to 0.033 ns/byte. When the held comparison mask is zero and all 32 title bytes also differ from the new values, it uses one vector store. Other masks keep your per-byte stores. The 97 dense merge checks and the repository's focused `merge_piece` test passed. The scattered 10% case is still slower in this test. I haven't measured how often dense merges occur during play, so this is complementary to the in-game results above.
TrustNo1 changed title from [pr-performance][memory] shadow: 32-byte AVX2 fast-skip in merge_piece to shadow: 32-byte AVX2 fast-skip in merge_piece 2026-12-06 03:29:01 +00:00
Owner

Test needed — commit f24eeae6; what to check:

  • The problem on main: shadow::merge_piece (crates/darner-gpu/src/shadow.rs) compared and updated now, held, and title byte-by-byte, writing every byte of title and held even across unchanged stretches during GPU-to-CPU shadow read-back merges.

Ports the 32-byte AVX2 fast-skip path for merge_piece from ProjectLeonida/mandy 653f60d37 (PR #2699, bobcatchris, GPL-3.0), runtime-dispatched via is_x86_feature_detected!("avx2") with a scalar fallback (merge_piece_scalar). Refined so that when a 32-byte chunk has differences, only the differing byte positions (trailing_zeros over the negated equality mask) are stored into title — avoiding writing neighbouring unchanged bytes that a title thread may be updating concurrently. Adds unit test merge_piece_avx2_matches_scalar_across_lengths_and_patterns.

Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture.

Reply testing to take this run; it's free again after 24 h with no results.

**Test needed** — commit `f24eeae6`; what to check: - The problem on main: `shadow::merge_piece` (`crates/darner-gpu/src/shadow.rs`) compared and updated `now`, `held`, and `title` byte-by-byte, writing every byte of `title` and `held` even across unchanged stretches during GPU-to-CPU shadow read-back merges. Ports the 32-byte AVX2 fast-skip path for `merge_piece` from `ProjectLeonida/mandy` `653f60d37` (PR #2699, `bobcatchris`, GPL-3.0), runtime-dispatched via `is_x86_feature_detected!("avx2")` with a scalar fallback (`merge_piece_scalar`). Refined so that when a 32-byte chunk has differences, only the differing byte positions (`trailing_zeros` over the negated equality mask) are stored into `title` — avoiding writing neighbouring unchanged bytes that a title thread may be updating concurrently. Adds unit test `merge_piece_avx2_matches_scalar_across_lengths_and_patterns`. Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture. Reply `testing` to take this run; it's free again after 24 h with no results.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'pr/avx2-merge-piece:Paliton-pr/avx2-merge-piece'
git switch 'Paliton-pr/avx2-merge-piece'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'Main'
git merge --no-ff 'Paliton-pr/avx2-merge-piece'
git switch 'Paliton-pr/avx2-merge-piece'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'Paliton-pr/avx2-merge-piece'
git switch 'Paliton-pr/avx2-merge-piece'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'Paliton-pr/avx2-merge-piece'
git switch 'Main'
git merge --squash 'Paliton-pr/avx2-merge-piece'
git switch 'Main'
git merge --ff-only 'Paliton-pr/avx2-merge-piece'
git switch 'Main'
git merge 'Paliton-pr/avx2-merge-piece'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
5 participants
Notifications
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
TrustNo1/Darner!440
No description provided.