runtime: remove per-call string allocations in cached_pso and add unit tests (RT-CACHED-PSO-ZERO-ALLOC-CHECK-AND-ENCODE) #2010

Open
github-bot wants to merge 1 commit from github-bot/gh-2010 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 18:03 UTC · original
IlyassRS3:perf/rt-cached-pso-zero-alloc → main · +164 −18 · 3 files · 1 commits · open

Claim: RT-CACHED-PSO-ZERO-ALLOC-CHECK-AND-ENCODE (claim comment #7170341146 on #1608).

Problem

In runtime/src/cached_pso.h:

  1. Both encode() and check() allocated a temporary 64-byte std::string padded = key; padded.resize(key_bytes, '\0'); (exceeding SSO on MSVC/libc++) on every call, adding an unnecessary heap allocation and free per Create*PipelineState / GetCachedBlob check (especially contended across parallel pipeline worker threads).
  2. encode() also pushed header fields byte-by-byte through put32() loops.
  3. KeyBuilder::tag(nullptr) passed null to std::strlen, and KeyBuilder::bytes(nullptr, nonzero) performed null pointer arithmetic.

Fix

  • encode(): allocate the exact blob_bytes (104-byte) output buffer once (zero-initialized), copy min(key.size(), key_bytes) directly into offset 36, and write little-endian 32-bit fields directly with write32().
  • check(): compare min(key.size(), key_bytes) bytes directly against key.data() and verify the remaining trailing bytes up to key_bytes are zero — zero heap allocations.
  • KeyBuilder: guard null pointers in tag() and bytes(), batch put32() insertion, and add reserve().
  • Add portable CTest cached_pso (runtime/test/cached_pso_test.cpp, pure C++20 stdlib, runs on Windows/Linux/macOS) covering round-trips, short/exact/overlong/embedded-NUL keys, single-bit tamper across all 104 bytes, every Check result, and KeyBuilder null guards.

Benchmark (clang++ -std=c++20 -O2, bit-exact A/B, 2,000,000 calls, 64-byte hex key)

  • check(): 156.84 ms -> 120.55 ms (1.30x faster, 0 heap allocations vs 2M new/delete pairs)
  • encode(): 240.20 ms -> 121.45 ms (1.98x faster, 1 allocation instead of 2)

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Ran runtime/test/cached_pso_test.cpp 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
  • Ran python3 -I scripts/check_no_game_data.py runtime/src/cached_pso.h runtime/test/cached_pso_test.cpp runtime/CMakeLists.txt and python3 -I tests/test_text_write_line_endings.py: all passed.
**@⁠IlyassRS3** on GitHub · 2026-11-25 18:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2010) `IlyassRS3:perf/rt-cached-pso-zero-alloc` → `main` · +164 −18 · 3 files · 1 commits · open Claim: `RT-CACHED-PSO-ZERO-ALLOC-CHECK-AND-ENCODE` (claim comment `#7170341146` on #1608). ### Problem In `runtime/src/cached_pso.h`: 1. Both `encode()` and `check()` allocated a temporary 64-byte `std::string padded = key; padded.resize(key_bytes, '\0');` (exceeding SSO on MSVC/libc++) on every call, adding an unnecessary heap allocation and free per `Create*PipelineState` / `GetCachedBlob` check (especially contended across parallel pipeline worker threads). 2. `encode()` also pushed header fields byte-by-byte through `put32()` loops. 3. `KeyBuilder::tag(nullptr)` passed null to `std::strlen`, and `KeyBuilder::bytes(nullptr, nonzero)` performed null pointer arithmetic. ### Fix - `encode()`: allocate the exact `blob_bytes` (104-byte) output buffer once (zero-initialized), copy `min(key.size(), key_bytes)` directly into offset 36, and write little-endian 32-bit fields directly with `write32()`. - `check()`: compare `min(key.size(), key_bytes)` bytes directly against `key.data()` and verify the remaining trailing bytes up to `key_bytes` are zero — zero heap allocations. - `KeyBuilder`: guard null pointers in `tag()` and `bytes()`, batch `put32()` insertion, and add `reserve()`. - Add portable CTest `cached_pso` (`runtime/test/cached_pso_test.cpp`, pure C++20 stdlib, runs on Windows/Linux/macOS) covering round-trips, short/exact/overlong/embedded-NUL keys, single-bit tamper across all 104 bytes, every `Check` result, and `KeyBuilder` null guards. ### Benchmark (`clang++ -std=c++20 -O2`, bit-exact A/B, 2,000,000 calls, 64-byte hex key) - `check()`: `156.84 ms` -> `120.55 ms` (**1.30x faster**, 0 heap allocations vs 2M `new`/`delete` pairs) - `encode()`: `240.20 ms` -> `121.45 ms` (**1.98x faster**, 1 allocation instead of 2) ### Verification - **AI-assisted**: yes (Antigravity agent by Google DeepMind). - **GPUs tested**: none (host is macOS arm64; no retail files). - Ran `runtime/test/cached_pso_test.cpp` 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` - Ran `python3 -I scripts/check_no_game_data.py runtime/src/cached_pso.h runtime/test/cached_pso_test.cpp runtime/CMakeLists.txt` and `python3 -I tests/test_text_write_line_endings.py`: all passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:41 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 18:03 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 18:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2010#pullrequestreview-6062563901) · **reviewed** 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.
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/2010/head:github-bot/gh-2010'
git switch 'github-bot/gh-2010'

Merge

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