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

Closed
leonida/mandy
quietfold wants to merge 0 commits from perf/rt-cached-pso-zero-alloc into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

quietfold commented 2026-11-25 18:03:40 +00:00

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.
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.
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 18:03:54 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
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.
zmasi commented 2026-11-28 12:38:47 +00:00

Verified on Windows 11 (10.0.26200.9974) with MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) against main d48a957bb + 7509fbce05 (claim #2570#issuecomment-7239309422):

  • Line-by-line diff audit: Clean (runtime/CMakeLists.txt, runtime/src/cached_pso.h, runtime/test/cached_pso_test.cpp); eliminates temporary std::string allocations in cached_pso::encode and cached_pso::check while preserving exact zero-padding and 64-byte truncation semantics, adds KeyBuilder::reserve, and guards KeyBuilder::tag(nullptr) / KeyBuilder::bytes(nullptr, n). check_no_game_data.py clean.
  • Red (d48a957bb + PR test): cached_pso_test.cpp fails to build (rc=2, write32 and KeyBuilder::reserve absent on main, plus unguarded std::strlen(nullptr) in KeyBuilder::tag).
  • Green (d48a957bb + 7509fbce05 merge): Auto-merges cleanly (rc=0), compiles with 0 warnings under /W4 /permissive-, and passes (cached_pso_test: all checks passed, rc=0).

AI-assisted.

Verified on Windows 11 (`10.0.26200.9974`) with MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2`) against `main` `d48a957bb` + `7509fbce05` (claim `#2570#issuecomment-7239309422`): - **Line-by-line diff audit**: Clean (`runtime/CMakeLists.txt`, `runtime/src/cached_pso.h`, `runtime/test/cached_pso_test.cpp`); eliminates temporary `std::string` allocations in `cached_pso::encode` and `cached_pso::check` while preserving exact zero-padding and 64-byte truncation semantics, adds `KeyBuilder::reserve`, and guards `KeyBuilder::tag(nullptr)` / `KeyBuilder::bytes(nullptr, n)`. `check_no_game_data.py` clean. - **Red (`d48a957bb` + PR test)**: `cached_pso_test.cpp` fails to build (`rc=2`, `write32` and `KeyBuilder::reserve` absent on `main`, plus unguarded `std::strlen(nullptr)` in `KeyBuilder::tag`). - **Green (`d48a957bb` + `7509fbce05` merge)**: Auto-merges cleanly (`rc=0`), compiles with `0` warnings under `/W4 /permissive-`, and passes (`cached_pso_test: all checks passed`, `rc=0`). AI-assisted.
OreoFR commented 2026-11-29 03:14:33 +00:00

Verified by OreoFR

  • Base: main aa918e5d9 + PR head 7509fbce0
  • Test: ctest -R ^cached_pso$
  • Red on main: the new test does not compile on main (write32 and KeyBuilder::reserve missing)
  • Green with the PR: passes; package builds, full ctest shows no new failure (xmem_no_write_watch failed once under load and passes on a serial rerun)
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

Note: shares runtime/CMakeLists.txt with the open #2566; a rebase may be needed if #2566 lands first.

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

**Verified by OreoFR** - Base: main `aa918e5d9` + PR head `7509fbce0` - Test: `ctest -R ^cached_pso$` - Red on main: the new test does not compile on main (`write32` and `KeyBuilder::reserve` missing) - Green with the PR: passes; package builds, full ctest shows no new failure (`xmem_no_write_watch` failed once under load and passes on a serial rerun) - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 Note: shares `runtime/CMakeLists.txt` with the open #2566; a rebase may be needed if #2566 lands first. AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:41:03 +00:00

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.

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.
Sign in to join this conversation.
No description provided.