Skip to content

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

Open
IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-cached-pso-zero-alloc
Open

IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-cached-pso-zero-alloc

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

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 AI balanced review requested due to automatic review settings November 25, 2026 18:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

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

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants