Repository navigation
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
Conversation
Contributor
|
Verified on Windows 11 (
AI-assisted. |
Contributor
|
Verified by OreoFR
Note: shares AI-assisted (Claude Code, Claude Opus 5.5). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.