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

Merged
OGShotz merged 1 commit from upstream-pr/2116 into main 2026-11-29 22:54:42 +00:00
OGShotz commented 2026-11-29 09:37:19 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by OGShotz on 2026-11-29T09:37:19Z.


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

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5549474210 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2116). Originally posted by [OGShotz](https://github.com/OGShotz) on 2026-11-29T09:37:19Z. --- ## 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)
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!2116
No description provided.