[PR #1882] perf(xg_xs): specialize copy_elements for common element sizes #1895

Open
opened 2026-11-26 21:19:19 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1882
Author: @t91a60
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: perf/xg-xs-copy-specialization


📝 Commits (5)

  • cc4ac41 perf(xg_xs): specialize copy_elements for common element sizes
  • 7bd25f3 perf(xg_xs): optimize row-level dispatch with pointer advancement
  • 5a2b732 perf(xg_xs): replace memcpy with direct scalar moves and fix MSVC restrict keyword
  • b3539df perf(xg_xs): revert to std::memcpy for correctness and performance
  • d52880b Merge branch 'main' into perf/xg-xs-copy-specialization

📊 Changes

1 file changed (+40 additions, -4 deletions)

View changed files

📝 runtime/shims/xg_xs_layout.h (+40 -4)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1882 **Author:** [@t91a60](https://github.com/t91a60) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/xg-xs-copy-specialization` --- ### 📝 Commits (5) - [`cc4ac41`](https://github.com/ubiquitarianism/leonida/commit/cc4ac4101b3201a771b94a1b5303c83d01e629ad) perf(xg_xs): specialize copy_elements for common element sizes - [`7bd25f3`](https://github.com/ubiquitarianism/leonida/commit/7bd25f386111727a1db3aaab5227e2c5d3218726) perf(xg_xs): optimize row-level dispatch with pointer advancement - [`5a2b732`](https://github.com/ubiquitarianism/leonida/commit/5a2b73223f30d69297f340771633ef72272bd32e) perf(xg_xs): replace memcpy with direct scalar moves and fix MSVC restrict keyword - [`b3539df`](https://github.com/ubiquitarianism/leonida/commit/b3539dfa70d0f2746c261608c7c8ade658378688) perf(xg_xs): revert to std::memcpy for correctness and performance - [`d52880b`](https://github.com/ubiquitarianism/leonida/commit/d52880bdca7a43861843c7ceaa510fca82ed8b41) Merge branch 'main' into perf/xg-xs-copy-specialization ### 📊 Changes **1 file changed** (+40 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `runtime/shims/xg_xs_layout.h` (+40 -4) </details> ### 📄 Description **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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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
starred/leonida#1895
No description provided.