Safe copy: copies under 1 KiB by SSE2 moves, 3-4x faster small copies on Zen 2 (VICTORY_SMALL_COPY) #2116

Manually merged
github merged 1 commit from github-bot/gh-2116 into gh-pr/2116/base 2026-11-29 22:58:53 +00:00 AGit
Owner

@⁠OGShotz on GitHub · 2026-11-29 09:37 UTC · original
OGShotz:small-copy-zen2 → main · +65 −6 · 1 files · 1 commits · merged 2026-11-29 22:54 UTC

What it changes

safecopy::copy (the fault-safe copy of title memory) was one rep movsb at any length. rep movsb starts slowly on Zen 2, which has no fast short rep movsb, so small copies cost several times what a plain copy does.

Copies under VICTORY_SMALL_COPY bytes (1024 by default; 0 keeps rep movsb for every length, as before) now use 16-byte SSE2 moves, then 8-byte moves, then single bytes. All of these sit between victory_safe_copy_at and victory_safe_copy_after, so a fault in them is handled as one in rep movsb is: the watch handler opens a block we guard and the copy goes on, or the copy leaves through victory_safe_copy_failed. The path is SSE2 only, with no AVX state to clear. The limit is passed in r9, so the switch is read once in Rust (small_copy()), not in the assembly.

How it was tested

  • Ryzen 7 3700X (Zen 2), RTX 2070 8 GB, Windows 11, release build with -C target-cpu=znver2.
  • safecopy::tests::copy_costs (ignored test, --nocapture). It now also measures 64 B and 1 KiB pieces, scattered over 128 MiB. These are the column for copy (labelled "rep movsb" in the test output), in GB/s:
piece into VICTORY_SMALL_COPY=0 (before) default (after) memcpy
64 B plain memory 1.4 4.8 4.6-5.0
256 B plain memory 3.1 12.4 15.1-15.6
64 B buffer the card reads 2.7 5.1 4.9-5.0
256 B buffer the card reads 9.4 11.4 14.7-15.0
4 KiB and 64 KiB both 7.1-8.8 7.6-8.4 5.6-9.1

Pieces of 4 KiB and up are memory-bound and unchanged; they still take rep movsb.

  • cargo test -p victory-gpu --lib safecopy: 8 passed.
    • copies_every_length_and_alignment covers more lengths now (7-9, 15-17, 31-33, 63-65, 100, 513, 1023-1025).
    • faults_are_failures adds small copies that run into a forbidden page (a + 4096 - 20, 40 bytes; a + 4096 - 3, 8 bytes), which must fail, and one that ends just before it, which must succeed.
  • The full victory-gpu suite passed (920) on a branch with this change on main 829188e9. On the previous main (e1f2eb60) the gpu, shader and hle suites all passed with it (625 / 395 / 204; the gpu suite has the same rare failures as main without it: watch::many_askers_tests 2 of 1,000 runs either way, and descriptor::host::tests::views_off_their_elements_get_a_copy_of_their_bytes).
  • In-game: played on an earlier main with this change, Series S, DLSS, no frame generation, with no crash. Those runs also had other changes, so it gives no frame-rate figure for this change alone.

Checked for overlap

  • I searched the open pull requests. Related: #1298 makes the same change for safe copy inside a larger pull request: an AVX2 loop for 32..=1024 bytes, with vzeroupper on both exits. This one is that part on its own, against the current main, switch-gated, with Zen 2 numbers and fault tests at the edges. If #1298 lands first, this can be closed.

🤖 Generated with Claude Code

**@⁠OGShotz** on GitHub · 2026-11-29 09:37 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2116) `OGShotz:small-copy-zen2` → `main` · +65 −6 · 1 files · 1 commits · merged 2026-11-29 22:54 UTC ## What it changes `safecopy::copy` (the fault-safe copy of title memory) was one `rep movsb` at any length. `rep movsb` starts slowly on Zen 2, which has no fast short `rep movsb`, so small copies cost several times what a plain copy does. Copies under `VICTORY_SMALL_COPY` bytes (1024 by default; `0` keeps `rep movsb` for every length, as before) now use 16-byte SSE2 moves, then 8-byte moves, then single bytes. All of these sit between `victory_safe_copy_at` and `victory_safe_copy_after`, so a fault in them is handled as one in `rep movsb` is: the watch handler opens a block we guard and the copy goes on, or the copy leaves through `victory_safe_copy_failed`. The path is SSE2 only, with no AVX state to clear. The limit is passed in `r9`, so the switch is read once in Rust (`small_copy()`), not in the assembly. ## How it was tested - Ryzen 7 3700X (Zen 2), RTX 2070 8 GB, Windows 11, release build with `-C target-cpu=znver2`. - `safecopy::tests::copy_costs` (ignored test, `--nocapture`). It now also measures 64 B and 1 KiB pieces, scattered over 128 MiB. These are the column for `copy` (labelled "rep movsb" in the test output), in GB/s: | piece | into | `VICTORY_SMALL_COPY=0` (before) | default (after) | memcpy | |---|---|---|---|---| | 64 B | plain memory | 1.4 | 4.8 | 4.6-5.0 | | 256 B | plain memory | 3.1 | 12.4 | 15.1-15.6 | | 64 B | buffer the card reads | 2.7 | 5.1 | 4.9-5.0 | | 256 B | buffer the card reads | 9.4 | 11.4 | 14.7-15.0 | | 4 KiB and 64 KiB | both | 7.1-8.8 | 7.6-8.4 | 5.6-9.1 | Pieces of 4 KiB and up are memory-bound and unchanged; they still take `rep movsb`. - `cargo test -p victory-gpu --lib safecopy`: 8 passed. - `copies_every_length_and_alignment` covers more lengths now (7-9, 15-17, 31-33, 63-65, 100, 513, 1023-1025). - `faults_are_failures` adds small copies that run into a forbidden page (`a + 4096 - 20`, 40 bytes; `a + 4096 - 3`, 8 bytes), which must fail, and one that ends just before it, which must succeed. - The full `victory-gpu` suite passed (920) on a branch with this change on main 829188e9. On the previous main (e1f2eb60) the gpu, shader and hle suites all passed with it (625 / 395 / 204; the gpu suite has the same rare failures as main without it: `watch::many_askers_tests` 2 of 1,000 runs either way, and `descriptor::host::tests::views_off_their_elements_get_a_copy_of_their_bytes`). - In-game: played on an earlier main with this change, Series S, DLSS, no frame generation, with no crash. Those runs also had other changes, so it gives no frame-rate figure for this change alone. ## Checked for overlap - [x] I searched the open pull requests. Related: #1298 makes the same change for safe copy inside a larger pull request: an AVX2 loop for 32..=1024 bytes, with `vzeroupper` on both exits. This one is that part on its own, against the current main, switch-gated, with Zen 2 numbers and fault tests at the edges. If #1298 lands first, this can be closed. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
rep movsb starts slowly on Zen 2. copy_costs on a Ryzen 7 3700X, into plain
memory: 64 B 1.4 -> 4.8 GB/s, 256 B 3.1 -> 12.4 GB/s; into a buffer the card
reads: 64 B 2.7 -> 5.1 GB/s, 256 B 9.4 -> 11.4 GB/s; 4 KiB and up unchanged.
The every-length test covers the lengths around the limit, and the fault test
small copies that run into a forbidden page.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 09:39:36 +00:00
github-bot changed target branch from main to gh-pr/2116/base 2026-11-29 22:58:26 +00:00
github manually merged commit 36c23d49db into gh-pr/2116/base 2026-11-29 22:58:28 +00:00
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

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