[pr-performance][memory] shadow: 32-byte AVX2 fast-skip in merge_piece #440

Open
Paliton wants to merge 1 commit 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
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 labels
No milestone
No project
No assignees
1 participant
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.