perf(descriptor_table): bulk memcpy table serialization/readback and bound changed_slots_ growth #1967

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/1967 into main
IlyassRS3 commented 2026-11-25 16:58:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T16:58:07Z.


Claim: RT-DESCRIPTOR-TABLE-BULK-COPY-AND-DIRTY-CAP (claim comment #7168739564 on #1608).

Optimizations (runtime/src/descriptor_table.h)

  1. Bulk std::memcpy for write_to_buffer(), build_gpu_buffer(), and read_slots():
    • Added compile-time layout static_asserts proving DescriptorEntry is trivially copyable and tightly packed (sizeof == 40, key @ 0, index @ 32, pad @ 36).
    • For !clear_pad (all runtime BindlessSet GPU flushes), replaced per-entry loops (including the double-copy through a stack slot[10] array in write_to_buffer) with a single contiguous std::memcpy.
    • For clear_pad == true, writes the 36-byte (key, index) prefix and trailing 4-byte zero in a single sequential pass (WC-friendly, no stack double-copy).
  2. Bound changed_slots_ memory and skip redundant tracking when all_changed_ is already set:
    • Once all_changed_ is true (after clear() or grow_locked()), mark() returns immediately instead of pushing every subsequent insert/erase onto changed_slots_.
    • When !all_changed_ and changed_slots_.size() > entries_.size() (repeated updates/erases between flushes), compacts changed_slots_ in place with std::sort + std::unique without flipping all_changed_ (preserving the in-place non-relocating flush contract so untouched live slots are never rewritten).
  3. Hoist hash_descriptor_key out of insert_locked() retry loop: avoids recomputing the hash on retries after grow_locked().

Verification

  • AI-assisted: yes.
  • GPUs tested: none (host is macOS arm64; no retail files; 0 CMakeLists.txt changes).
  • Expanded runtime/test/descriptor_table_test.cpp to test write_to_buffer(nullptr), multi-slot read_slots across full and mid-table ranges, all_changed_ suppressing changed_slots_ accumulation, and repeated-mutation compaction without spuriously setting changes.all.
  • Ran descriptor_table_test cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Bit-exact A/B benchmark at -O2 vs main on a 65,536-slot (2.62 MB) table with 24,000 live entries (best of 5 runs, Apple Silicon):
write_to_buffer(false) 64k: base=   55.8 us  new=  38.5 us  speedup= 1.45x
write_to_buffer(true)  64k: base=   41.6 us  new=  33.5 us  speedup= 1.24x
build_gpu_buffer(false)64k: base=   92.7 us  new=  44.0 us  speedup= 2.11x
build_gpu_buffer(true) 64k: base=   57.6 us  new=  46.9 us  speedup= 1.23x
read_slots(64k)        64k: base=   60.6 us  new=  28.7 us  speedup= 2.11x
  • Ran python3 -I scripts/check_no_game_data.py on both files and python3 -I tests/test_text_write_line_endings.py: all passed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5495573266 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1967). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T16:58:07Z. --- Claim: `RT-DESCRIPTOR-TABLE-BULK-COPY-AND-DIRTY-CAP` (claim comment `#7168739564` on #1608). ### Optimizations (`runtime/src/descriptor_table.h`) 1. **Bulk `std::memcpy` for `write_to_buffer()`, `build_gpu_buffer()`, and `read_slots()`**: - Added compile-time layout `static_assert`s proving `DescriptorEntry` is trivially copyable and tightly packed (`sizeof == 40`, `key @ 0`, `index @ 32`, `pad @ 36`). - For `!clear_pad` (all runtime `BindlessSet` GPU flushes), replaced per-entry loops (including the double-copy through a stack `slot[10]` array in `write_to_buffer`) with a single contiguous `std::memcpy`. - For `clear_pad == true`, writes the 36-byte `(key, index)` prefix and trailing 4-byte zero in a single sequential pass (WC-friendly, no stack double-copy). 2. **Bound `changed_slots_` memory and skip redundant tracking when `all_changed_` is already set**: - Once `all_changed_` is true (after `clear()` or `grow_locked()`), `mark()` returns immediately instead of pushing every subsequent insert/erase onto `changed_slots_`. - When `!all_changed_` and `changed_slots_.size() > entries_.size()` (repeated updates/erases between flushes), compacts `changed_slots_` in place with `std::sort` + `std::unique` without flipping `all_changed_` (preserving the in-place non-relocating flush contract so untouched live slots are never rewritten). 3. **Hoist `hash_descriptor_key` out of `insert_locked()` retry loop**: avoids recomputing the hash on retries after `grow_locked()`. ### Verification - **AI-assisted**: yes. - **GPUs tested**: none (host is macOS arm64; no retail files; 0 `CMakeLists.txt` changes). - Expanded `runtime/test/descriptor_table_test.cpp` to test `write_to_buffer(nullptr)`, multi-slot `read_slots` across full and mid-table ranges, `all_changed_` suppressing `changed_slots_` accumulation, and repeated-mutation compaction without spuriously setting `changes.all`. - Ran `descriptor_table_test` cleanly under: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror` - Bit-exact A/B benchmark at `-O2` vs `main` on a 65,536-slot (2.62 MB) table with 24,000 live entries (best of 5 runs, Apple Silicon): ```text write_to_buffer(false) 64k: base= 55.8 us new= 38.5 us speedup= 1.45x write_to_buffer(true) 64k: base= 41.6 us new= 33.5 us speedup= 1.24x build_gpu_buffer(false)64k: base= 92.7 us new= 44.0 us speedup= 2.11x build_gpu_buffer(true) 64k: base= 57.6 us new= 46.9 us speedup= 1.23x read_slots(64k) 64k: base= 60.6 us new= 28.7 us speedup= 2.11x ``` - Ran `python3 -I scripts/check_no_game_data.py` on both files and `python3 -I tests/test_text_write_line_endings.py`: all passed.
copilot-pull-request-reviewer[bot] commented 2026-11-25 16:58:25 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T16:58:25Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6061633916 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1967#pullrequestreview-6061633916). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T16:58:25Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
MoonEyes commented 2026-11-28 02:51:35 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T02:51:35Z.


Hi @IlyassRS3, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/src/descriptor_table.h), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7232639349 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1967#issuecomment-7232639349). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T02:51:35Z. --- Hi @IlyassRS3, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `runtime/src/descriptor_table.h`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
OreoFR commented 2026-11-28 21:57:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:57:11Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head 5f188a19a
  • Test: not run (merge conflict with main)
  • Red on main: not run
  • Green with the PR: not run
  • Verdict: CONFLICT (conflicts with main in runtime/src/descriptor_table.h (main renamed pad to kind with slot_kind()); build_gpu_buffer must be rewritten, non-trivial)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246391086 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1967#issuecomment-7246391086). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:57:11Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `5f188a19a` - Test: not run (merge conflict with main) - Red on main: not run - Green with the PR: not run - Verdict: CONFLICT (conflicts with main in runtime/src/descriptor_table.h (main renamed pad to kind with slot_kind()); build_gpu_buffer must be rewritten, non-trivial) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:40:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:40:14Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7318002556 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1967#issuecomment-7318002556). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:40:14Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

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!1967
No description provided.