mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 21:50:52 +01:00
[PR #2010] runtime: remove per-call string allocations in cached_pso and add unit tests (RT-CACHED-PSO-ZERO-ALLOC-CHECK-AND-ENCODE) #2017
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#2017
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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)
7509fbcruntime: 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#7170341146on #1608).Problem
In
runtime/src/cached_pso.h:encode()andcheck()allocated a temporary 64-bytestd::string padded = key; padded.resize(key_bytes, '\0');(exceeding SSO on MSVC/libc++) on every call, adding an unnecessary heap allocation and free perCreate*PipelineState/GetCachedBlobcheck (especially contended across parallel pipeline worker threads).encode()also pushed header fields byte-by-byte throughput32()loops.KeyBuilder::tag(nullptr)passed null tostd::strlen, andKeyBuilder::bytes(nullptr, nonzero)performed null pointer arithmetic.Fix
encode(): allocate the exactblob_bytes(104-byte) output buffer once (zero-initialized), copymin(key.size(), key_bytes)directly into offset 36, and write little-endian 32-bit fields directly withwrite32().check(): comparemin(key.size(), key_bytes)bytes directly againstkey.data()and verify the remaining trailing bytes up tokey_bytesare zero — zero heap allocations.KeyBuilder: guard null pointers intag()andbytes(), batchput32()insertion, and addreserve().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, everyCheckresult, andKeyBuildernull 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 2Mnew/deletepairs)encode():240.20 ms->121.45 ms(1.98x faster, 1 allocation instead of 2)Verification
runtime/test/cached_pso_test.cppcleanly under:clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werrorclang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werrorclang++ -std=c++20 -O2 -Wall -Wextra -Werrorpython3 -I scripts/check_no_game_data.py runtime/src/cached_pso.h runtime/test/cached_pso_test.cpp runtime/CMakeLists.txtandpython3 -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.