perf(xg_xs): specialize copy_elements for common element sizes #1882

Closed
t91a60 wants to merge 5 commits from upstream-pr/1882 into main
t91a60 commented 2026-11-25 14:11:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by t91a60 on 2026-11-25T14:11:19Z.


Summary
Specializes the per-element std::memcpy in rt::xg_xs::copy_elements() (used by XG Copy*Subresource, D3D12 title-texture refreshes/TitleImageUpload, and asset streaming) for the five most common element sizes (1/2/4/8/16 bytes). Reduces per-texel function-call and size-argument overhead in the hot tiled path.

Addresses the performance finding in the coordination thread (#1608). A synthetic 1024×1024 RGBA8 tiled case measured ~7.6× faster (0.596 ms vs 4.54 ms under MinGW GCC 16.2 -O3). Linear full-row path and uncommon sizes are unchanged. Behavior is identical.

Technical details

  • New copy_fixed_bytes<N> template helper (compile-time constant N → compiler inlines to moves or unrolled copies).
  • switch (bytes) in the tiled inner loop only.
  • Updated comment with explanation and measurement.
  • Matches project style, keeps the function inline, no new dependencies or includes.
  • Depends on: #1608 (the finding). AI-assisted: yes.

Testing

  • python -m compileall -q make_game.py tools tests → passed.
  • xg_xs_test.cpp (and d3d12_existing_heap_test.cpp, d3d12_title_va_test.cpp etc.) exercise the function with all relevant formats, mips, arrays, volumes, tails, linear/swizzled modes, and round-trips. The specialized paths are hit by the test cases (1/4/8/16-byte elements). All byte-for-byte comparisons pass identically before/after.
  • No game files used. Full ctest ... -R "xg_xs|d3d12.*(heap|title|texture)" recommended after a runtime build.
  • GPUs: N/A (pure CPU change).

Risk assessment
Very low. Semantically identical to the original code for every input (exact same addresses and bytes copied). Falls back to the previous memcpy for any other size. No impact on shader translation, asset export, loader, Vulkan layer, or any other component. No new state or error paths.

Related
Coordination issue #1608. No specific issue closed.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5492985244 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1882). Originally posted by [t91a60](https://github.com/t91a60) on 2026-11-25T14:11:19Z. --- **Summary** Specializes the per-element `std::memcpy` in `rt::xg_xs::copy_elements()` (used by XG `Copy*Subresource`, D3D12 title-texture refreshes/`TitleImageUpload`, and asset streaming) for the five most common element sizes (1/2/4/8/16 bytes). Reduces per-texel function-call and size-argument overhead in the hot tiled path. Addresses the performance finding in the coordination thread ([#1608](https://github.com/ubiquitarianism/leonida/issues/1608)). A synthetic 1024×1024 RGBA8 tiled case measured ~7.6× faster (0.596 ms vs 4.54 ms under MinGW GCC 16.2 -O3). Linear full-row path and uncommon sizes are unchanged. Behavior is identical. **Technical details** - New `copy_fixed_bytes<N>` template helper (compile-time constant `N` → compiler inlines to moves or unrolled copies). - `switch (bytes)` in the tiled inner loop only. - Updated comment with explanation and measurement. - Matches project style, keeps the function `inline`, no new dependencies or includes. - `Depends on: #1608` (the finding). AI-assisted: yes. **Testing** - `python -m compileall -q make_game.py tools tests` → passed. - `xg_xs_test.cpp` (and `d3d12_existing_heap_test.cpp`, `d3d12_title_va_test.cpp` etc.) exercise the function with all relevant formats, mips, arrays, volumes, tails, linear/swizzled modes, and round-trips. The specialized paths are hit by the test cases (1/4/8/16-byte elements). All byte-for-byte comparisons pass identically before/after. - No game files used. Full `ctest ... -R "xg_xs|d3d12.*(heap|title|texture)"` recommended after a runtime build. - GPUs: N/A (pure CPU change). **Risk assessment** Very low. Semantically identical to the original code for every input (exact same addresses and bytes copied). Falls back to the previous `memcpy` for any other size. No impact on shader translation, asset export, loader, Vulkan layer, or any other component. No new state or error paths. **Related** Coordination issue [#1608](https://github.com/ubiquitarianism/leonida/issues/1608). No specific issue closed.
thiefyzheng commented 2026-11-25 14:24:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by thiefyzheng on 2026-11-25T14:24:51Z.


Heads-up on parallel work, @t91a60: the same specialization was claimed on #1608 at 13:53 UTC as RT-TEXEL-COPY-FIXEDWIDTH (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164508666, crediting @cheltyi's 13:49 finding) and opened as #1880 at 14:05, before this PR opened at 14:11; no claiming: for this item appears on the board before your done: at 14:16. Per the coordination issue's claim-before-code rules I believe #1880 carries this item.

For whoever deduplicates: @ecox982745 benchmarked both heads on identical sources (https://github.com/ubiquitarianism/leonida/pull/1880#issuecomment-7165045954) — both are byte-exact (the 306-check corpus passes against your header as well; nothing wrong with the correctness), but the row-level dispatch runs the RGBA8 1024x1024 tiled copy at 0.357 ms vs 0.909 ms for the per-element shape at MSVC /O2. If a maintainer prefers your shape anyway, we will release the claim — but as the claimed-and-first PR we are keeping #1880 open. AI-assisted (orchestrator).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7165240361 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1882#issuecomment-7165240361). Originally posted by [thiefyzheng](https://github.com/thiefyzheng) on 2026-11-25T14:24:51Z. --- Heads-up on parallel work, @t91a60: the same specialization was claimed on #1608 at 13:53 UTC as RT-TEXEL-COPY-FIXEDWIDTH (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164508666, crediting @cheltyi's 13:49 finding) and opened as #1880 at 14:05, before this PR opened at 14:11; no `claiming:` for this item appears on the board before your `done:` at 14:16. Per the coordination issue's claim-before-code rules I believe #1880 carries this item. For whoever deduplicates: @ecox982745 benchmarked both heads on identical sources (https://github.com/ubiquitarianism/leonida/pull/1880#issuecomment-7165045954) — both are byte-exact (the 306-check corpus passes against your header as well; nothing wrong with the correctness), but the row-level dispatch runs the RGBA8 1024x1024 tiled copy at 0.357 ms vs 0.909 ms for the per-element shape at MSVC /O2. If a maintainer prefers your shape anyway, we will release the claim — but as the claimed-and-first PR we are keeping #1880 open. AI-assisted (orchestrator).
Snooozs commented 2026-11-25 15:12:16 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Snooozs on 2026-11-25T15:12:16Z.


This does not build with MSVC (19.43, gta6rt target): runtime/shims/xg_xs_layout.h(388): error C2146 at __restrict__, which is a GCC/Clang spelling. __restrict is accepted by MSVC, GCC and Clang; with that replacement it compiles here. I have not measured the speed. AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7166467545 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1882#issuecomment-7166467545). Originally posted by [Snooozs](https://github.com/Snooozs) on 2026-11-25T15:12:16Z. --- This does not build with MSVC (19.43, `gta6rt` target): `runtime/shims/xg_xs_layout.h(388): error C2146` at `__restrict__`, which is a GCC/Clang spelling. `__restrict` is accepted by MSVC, GCC and Clang; with that replacement it compiles here. I have not measured the speed. AI-assisted (Claude Code).
ecox982745 commented 2026-11-25 15:15:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ecox982745 on 2026-11-25T15:15:30Z.


The earlier 0.909 ms result was for the per-element switch in cc4ac4101. I re-ran the same benchmark on the new row-dispatch loop in 7bd25f386.

MSVC 19.44 reproduces the C2146 at line 388 on the submitted header. For the timings and correctness run below I used /D__restrict__=__restrict to apply the spelling correction Snooozs identified, with no source edits. #1880 and the original compiled as submitted.

CPU milliseconds per 1024×1024 copy Original 5e95fa2f4 #1880 7f9e656e3 This PR 7bd25f386 with spelling correction
RGBA8 into tiled 2.131 0.362 0.355
RGBA8 out of tiled 2.132 0.359 0.335
BC7 into tiled 0.115 0.050 0.053
BC7 out of tiled 0.108 0.043 0.048
Linear RGBA8, into / out 0.075 / 0.075 0.074 / 0.074 0.074 / 0.074

Both row-dispatch implementations take about one-sixth of the original RGBA8 tiled-copy time. On this CPU the revised loop was slightly quicker for RGBA8 and #1880 was slightly quicker for BC7. The same 306-check corpus passed against the revised header with the spelling correction.

Ryzen 7 7800X3D, MSVC /O2, one pinned thread. Each value is the median of 12 process medians, covering all six build orderings twice, with five warm-up round trips and 15 timed copies per direction per process. These are synthetic CPU copy timings. Earlier benchmark and methodology

AI-assisted verification with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7166553801 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1882#issuecomment-7166553801). Originally posted by [ecox982745](https://github.com/ecox982745) on 2026-11-25T15:15:30Z. --- The earlier 0.909 ms result was for the per-element switch in `cc4ac4101`. I re-ran the same benchmark on the new row-dispatch loop in `7bd25f386`. MSVC 19.44 reproduces the C2146 at line 388 on the submitted header. For the timings and correctness run below I used `/D__restrict__=__restrict` to apply the spelling correction Snooozs identified, with no source edits. #1880 and the original compiled as submitted. | CPU milliseconds per 1024×1024 copy | Original `5e95fa2f4` | #1880 `7f9e656e3` | This PR `7bd25f386` with spelling correction | | --- | ---: | ---: | ---: | | RGBA8 into tiled | 2.131 | 0.362 | 0.355 | | RGBA8 out of tiled | 2.132 | 0.359 | 0.335 | | BC7 into tiled | 0.115 | 0.050 | 0.053 | | BC7 out of tiled | 0.108 | 0.043 | 0.048 | | Linear RGBA8, into / out | 0.075 / 0.075 | 0.074 / 0.074 | 0.074 / 0.074 | Both row-dispatch implementations take about one-sixth of the original RGBA8 tiled-copy time. On this CPU the revised loop was slightly quicker for RGBA8 and #1880 was slightly quicker for BC7. The same 306-check corpus passed against the revised header with the spelling correction. Ryzen 7 7800X3D, MSVC `/O2`, one pinned thread. Each value is the median of 12 process medians, covering all six build orderings twice, with five warm-up round trips and 15 timed copies per direction per process. These are synthetic CPU copy timings. [Earlier benchmark and methodology](https://github.com/ubiquitarianism/leonida/pull/1880#issuecomment-7165045954) AI-assisted verification with OpenAI Codex.
ecox982745 commented 2026-11-25 15:45:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ecox982745 on 2026-11-25T15:45:52Z.


5a2b73223 compiles as submitted with MSVC 19.44. One correctness issue remains in the new integer loads and stores: the XG copy API accepts byte buffers and padded row pitches. Neither the source nor the destination is guaranteed to meet the alignment required by a uint16_t*, uint32_t*, or uint64_t* dereference. Even an aligned allocation can have an unaligned second row.

Please keep the row dispatch and pointer increments and use std::memcpy(dst_ptr, src_ptr, bytes) in both directions. In the specialized instantiations bytes is constant. I checked MSVC /O2 assembly for all five fixed widths in the into-tiled direction: each inlined the copy, with no external calls. The version with this change passed all 306 checks in the #1880 reference corpus, including its misaligned and padded cases.

On the same Ryzen 7 7800X3D with aligned 1024×1024 synthetic inputs, the submitted scalar loop took 0.464 ms for RGBA8 into tiled and 0.063 ms for BC7. Changing only the copy operations to memcpy gave 0.376 and 0.053 ms respectively (12 process medians per build). This comparison measures CPU copies. Benchmark method

AI-assisted verification with OpenAI Codex.

Verified on the updated head. The header in b3539dfa7 is byte-for-byte identical to the corrected header tested above. That resolves my alignment concern. For interpreting the 0.376 ms RGBA8 figure, #1880 measured 0.378 ms in the same comparison. This PR was quicker in 5 of the 12 paired runs; the measurements do not establish a consistent speed advantage for either row-dispatch implementation.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7167298525 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1882#issuecomment-7167298525). Originally posted by [ecox982745](https://github.com/ecox982745) on 2026-11-25T15:45:52Z. --- `5a2b73223` compiles as submitted with MSVC 19.44. One correctness issue remains in the new integer loads and stores: the XG copy API accepts byte buffers and padded row pitches. Neither the source nor the destination is guaranteed to meet the alignment required by a `uint16_t*`, `uint32_t*`, or `uint64_t*` dereference. Even an aligned allocation can have an unaligned second row. Please keep the row dispatch and pointer increments and use `std::memcpy(dst_ptr, src_ptr, bytes)` in both directions. In the specialized instantiations `bytes` is constant. I checked MSVC `/O2` assembly for all five fixed widths in the into-tiled direction: each inlined the copy, with no external calls. The version with this change passed all 306 checks in the #1880 reference corpus, including its misaligned and padded cases. On the same Ryzen 7 7800X3D with aligned 1024×1024 synthetic inputs, the submitted scalar loop took 0.464 ms for RGBA8 into tiled and 0.063 ms for BC7. Changing only the copy operations to `memcpy` gave 0.376 and 0.053 ms respectively (12 process medians per build). This comparison measures CPU copies. [Benchmark method](https://github.com/ubiquitarianism/leonida/pull/1880#issuecomment-7165045954) AI-assisted verification with OpenAI Codex. **Verified on the updated head.** The header in `b3539dfa7` is byte-for-byte identical to the corrected header tested above. That resolves my alignment concern. For interpreting the 0.376 ms RGBA8 figure, #1880 measured 0.378 ms in the same comparison. This PR was quicker in 5 of the 12 paired runs; the measurements do not establish a consistent speed advantage for either row-dispatch implementation.
MoonEyes commented 2026-11-27 23:36:43 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-27T23:36:43Z.


Hi @t91a60, thanks for this. The same specialization of copy_elements (fixed widths 1, 2, 4, 8 and 16 bytes, dispatched once per row) was claimed first as RT-TEXEL-COPY-FIXEDWIDTH in #1608 by thiefyzheng and opened first as #1880. #1880 has the same change (apart from advancing the linear pointer and __restrict, with no measurement in this PR) plus a test file that compares it with the original loop on mips, tails, array slices, volumes and unaligned pointers; this pull request has no test.

To avoid two pull requests for one item I am closing this one in favor of #1880. If you see a case #1880 does not handle, please add it as a review comment there.

AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7230273610 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1882#issuecomment-7230273610). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-27T23:36:43Z. --- Hi @t91a60, thanks for this. The same specialization of `copy_elements` (fixed widths 1, 2, 4, 8 and 16 bytes, dispatched once per row) was claimed first as RT-TEXEL-COPY-FIXEDWIDTH in #1608 by `thiefyzheng` and opened first as #1880. #1880 has the same change (apart from advancing the linear pointer and `__restrict`, with no measurement in this PR) plus a test file that compares it with the original loop on mips, tails, array slices, volumes and unaligned pointers; this pull request has no test. To avoid two pull requests for one item I am closing this one in favor of #1880. If you see a case #1880 does not handle, please add it as a review comment there. AI-assisted (Claude Code, Claude Opus 5.5)

Pull request closed

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
ProjectLeonida/mandy!1882
No description provided.