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

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

📋 Pull Request Information

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

Base: main ← Head: perf/rt-descriptor-table-bulk-copy-and-dirty-cap


📝 Commits (1)

  • 5f188a1 perf(descriptor_table): bulk memcpy GPU table serialization and bound dirty-slot tracking

📊 Changes

2 files changed (+75 additions, -20 deletions)

View changed files

📝 runtime/src/descriptor_table.h (+37 -20)
📝 runtime/test/descriptor_table_test.cpp (+38 -0)

📄 Description

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.

🔄 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/1967 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-descriptor-table-bulk-copy-and-dirty-cap` --- ### 📝 Commits (1) - [`5f188a1`](https://github.com/ubiquitarianism/leonida/commit/5f188a19a5fe60f06636cdbf3df76b309c4e17bb) perf(descriptor_table): bulk memcpy GPU table serialization and bound dirty-slot tracking ### 📊 Changes **2 files changed** (+75 additions, -20 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/descriptor_table.h` (+37 -20) 📝 `runtime/test/descriptor_table_test.cpp` (+38 -0) </details> ### 📄 Description 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. --- <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#1974
No description provided.