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

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

📋 Pull Request Information

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

Base: main ← Head: perf/rt-cached-pso-zero-alloc


📝 Commits (1)

  • 7509fbc runtime: remove per-call string allocations in cached_pso and add unit tests

📊 Changes

3 files changed (+164 additions, -18 deletions)

View changed files

📝 runtime/CMakeLists.txt (+5 -1)
📝 runtime/src/cached_pso.h (+31 -17)
➕ runtime/test/cached_pso_test.cpp (+128 -0)

📄 Description

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.

🔄 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/2010 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-cached-pso-zero-alloc` --- ### 📝 Commits (1) - [`7509fbc`](https://github.com/ubiquitarianism/leonida/commit/7509fbce057a725251a2faa4c8c8774b06a9837a) runtime: remove per-call string allocations in cached_pso and add unit tests ### 📊 Changes **3 files changed** (+164 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+5 -1) 📝 `runtime/src/cached_pso.h` (+31 -17) ➕ `runtime/test/cached_pso_test.cpp` (+128 -0) </details> ### 📄 Description 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. --- <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#2017
No description provided.