perf(gpu): use runtime-dispatched AVX2 non-temporal stores for large snapshot copies #1003

Merged
adwen-007 merged 1 commit from upstream-pr/1003 into main 2026-11-29 08:16:58 +00:00
adwen-007 commented 2026-11-28 10:57:45 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by adwen-007 on 2026-11-28T10:57:45Z.


Problem

take_snapshots() copies large write-watched buffer snapshots on worker threads using std::ptr::copy_nonoverlapping. For large ranges (>= 64 KiB), regular stores allocate destination cache lines (read-for-ownership DRAM traffic) and evict hot CPU state from L2/L3 cache.

Fix

  • Add a runtime-dispatched copy_snapshot_avx2 (#[target_feature(enable = "avx2")] gated by std::arch::is_x86_feature_detected!("avx2")) using 4x unrolled _mm256_loadu_si256 + _mm256_stream_si256 (VMOVNTDQ) with 32-byte destination alignment and _mm_sfence() for copies >= 64 KiB.
  • Works on standard generic x86_64-pc-windows-msvc release builds (no -C target-cpu=native required) and falls back to copy_nonoverlapping on non-AVX2 CPUs or copies < 64 KiB.

Verification

  • Tested on Intel Core i7-12700K + 64 GB RAM + RTX 3060 Ti 8 GB (Windows 10 19045): snapshots time in the preparing per frame breakdown dropped from ~28.6 ms spikes to 0.0–0.3 ms across 20-minute gameplay sessions (0 errors).
  • Verified clean cargo check -p victory-gpu without RUSTFLAGS (generic x86-64 target) and zero warnings on v2026.11.28.01.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5539397464 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1003). Originally posted by [adwen-007](https://github.com/adwen-007) on 2026-11-28T10:57:45Z. --- ### Problem `take_snapshots()` copies large write-watched buffer snapshots on worker threads using `std::ptr::copy_nonoverlapping`. For large ranges (>= 64 KiB), regular stores allocate destination cache lines (read-for-ownership DRAM traffic) and evict hot CPU state from L2/L3 cache. ### Fix - Add a runtime-dispatched `copy_snapshot_avx2` (`#[target_feature(enable = "avx2")]` gated by `std::arch::is_x86_feature_detected!("avx2")`) using 4x unrolled `_mm256_loadu_si256` + `_mm256_stream_si256` (`VMOVNTDQ`) with 32-byte destination alignment and `_mm_sfence()` for copies >= 64 KiB. - Works on standard generic `x86_64-pc-windows-msvc` release builds (no `-C target-cpu=native` required) and falls back to `copy_nonoverlapping` on non-AVX2 CPUs or copies < 64 KiB. ### Verification - Tested on Intel Core i7-12700K + 64 GB RAM + RTX 3060 Ti 8 GB (Windows 10 19045): `snapshots` time in the `preparing per frame` breakdown dropped from ~`28.6 ms` spikes to `0.0–0.3 ms` across 20-minute gameplay sessions (`0` errors). - Verified clean `cargo check -p victory-gpu` without `RUSTFLAGS` (generic x86-64 target) and zero warnings on `v2026.11.28.01`.
Dukestar69 commented 2026-11-28 11:49:01 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-11-28T11:49:01Z.

Review: APPROVED


Tested locally merged onto main (63b902f).

  • Merge & Policy: Clean merge in crates/victory-gpu/src/shadow.rs; cargo xtask policy passed (294 files ok).
  • Code & Safety Review: Runtime-dispatched AVX2 path (len >= 64 KiB + is_x86_feature_detected!("avx2")) properly aligns dst to 32 bytes before unrolled _mm256_stream_si256, copies the tail safely, and issues _mm_sfence() before returning.
  • Tests: cargo test -p victory-gpu --lib shadow:: passed (36 passed; 0 failed; 2 ignored, including copies_cut_into_jobs_copy_the_same_bytes) with 0 compiler warnings.

Clean cache-friendly snapshot copy optimization. LGTM.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6097618053 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1003#pullrequestreview-6097618053). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-28T11:49:01Z. **Review:** APPROVED --- Tested locally merged onto `main` (`63b902f`). - **Merge & Policy**: Clean merge in `crates/victory-gpu/src/shadow.rs`; `cargo xtask policy` passed (`294 files ok`). - **Code & Safety Review**: Runtime-dispatched AVX2 path (`len >= 64 KiB` + `is_x86_feature_detected!("avx2")`) properly aligns `dst` to 32 bytes before unrolled `_mm256_stream_si256`, copies the tail safely, and issues `_mm_sfence()` before returning. - **Tests**: `cargo test -p victory-gpu --lib shadow::` passed (`36 passed; 0 failed; 2 ignored`, including `copies_cut_into_jobs_copy_the_same_bytes`) with 0 compiler warnings. Clean cache-friendly snapshot copy optimization. LGTM. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
trustno1-coder/VIctory-NVIDIA!1003
No description provided.