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

Open
github-bot wants to merge 5 commits from github-bot/gh-1882 into main AGit
Owner

@⁠t91a60 on GitHub · 2026-11-25 14:11 UTC · original
t91a60:perf/xg-xs-copy-specialization → main · +40 −4 · 1 files · 5 commits · open

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.

**@⁠t91a60** on GitHub · 2026-11-25 14:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1882) `t91a60:perf/xg-xs-copy-specialization` → `main` · +40 −4 · 1 files · 5 commits · open **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://git.gtahub.st/github/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://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608). No specific issue closed.
- Add copy_fixed_bytes<N> template for 1/2/4/8/16-byte elements (common in textures).
- Switch in tiled per-element path reduces call overhead (7.6× faster on synthetic 1024x1024 RGBA8 under GCC -O3).
- Linear row path and uncommon sizes unchanged.
- Behavior identical; covered by xg_xs_test (bijection, round-trip, model agreement) and d3d12 title texture tests.
- Addresses finding in coordination #1608.
- AI-assisted.

Tested with: python -m compileall -q make_game.py tools tests (passed). Full CTest recommended post-build.
- Advance linear pointer incrementally instead of calculating offset per element.
- Eliminates multiplication and branching inside the hot loop.
- Uses __restrict__ for better aliasing optimization.
- AI-assisted.
- Replaced std::memcpy with direct assignments for 1/2/4/8/16-byte elements (bypasses memcpy overhead on MSVC).
- Changed __restrict__ to __restrict for native MSVC compilation (fixes C2146).
- AI-assisted.
- Replaced direct scalar/uint64_t dereferences with std::memcpy(dst_ptr, src_ptr, bytes).
- MSVC /O2 inlines this to optimal instructions when bytes is a compile-time constant.
- Fixes undefined behavior on unaligned byte buffers and padded row pitches.
- AI-assisted.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:09:49 +00:00
Author
Owner

@⁠thiefyzheng on GitHub · 2026-11-25 14:24 UTC · original

Heads-up on parallel work, @⁠t91a60: the same specialization was claimed on #1608 at 13:53 UTC as RT-TEXEL-COPY-FIXEDWIDTH (#1608, 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 (#1880) — 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).

**@⁠thiefyzheng** on GitHub · 2026-11-25 14:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1882#issuecomment-7165240361) Heads-up on parallel work, @⁠t91a60: the same specialization was claimed on #1608 at 13:53 UTC as RT-TEXEL-COPY-FIXEDWIDTH (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608, 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://git.gtahub.st/github/ubiquitarianism--leonida/pulls/1880) — 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).
Author
Owner

@⁠Snooozs on GitHub · 2026-11-25 15:12 UTC · original

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).

**@⁠Snooozs** on GitHub · 2026-11-25 15:12 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1882#issuecomment-7166467545) 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).
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-25 15:15 UTC · original

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.

**@⁠ecox982745** on GitHub · 2026-11-25 15:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1882#issuecomment-7166553801) 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://git.gtahub.st/github/ubiquitarianism--leonida/pulls/1880) AI-assisted verification with OpenAI Codex.
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-25 15:45 UTC · original · edited 2026-11-25 16:14 UTC

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.

**@⁠ecox982745** on GitHub · 2026-11-25 15:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1882#issuecomment-7167298525) · edited 2026-11-25 16:14 UTC `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://git.gtahub.st/github/ubiquitarianism--leonida/pulls/1880) 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.
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 origin '+refs/pull/1882/head:github-bot/gh-1882'
git switch 'github-bot/gh-1882'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1882'
git switch 'github-bot/gh-1882'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1882'
git switch 'github-bot/gh-1882'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1882'
git switch 'main'
git merge --squash 'github-bot/gh-1882'
git switch 'main'
git merge --ff-only 'github-bot/gh-1882'
git switch 'main'
git merge 'github-bot/gh-1882'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.