Skip to content

runtime: hash native_pso HashMap and avoid heap churn in plan() - #1940

Open
IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-native-pso-hashmap-and-plan
Open

IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-native-pso-hashmap-and-plan

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

Item ID: RT-NATIVE-PSO-HASHMAP-AND-PLAN-PERF (claimed in #1608 comment #7167689403).

Summary

Optimises native_pso::HashMap parsing/lookup and removes per-stage heap churn in native_pso::plan(), while adding overflow-safe bounds checks in decode_state_b() and a portable unit test suite (native_pso_plan_test).

With native_hashes.txt now carrying ~25,321 shader digests and plan() running on every native PSO creation across retail world loading, replacing the red-black tree and per-block heap allocations directly speeds up pipeline lookup and planning:

  • runtime/src/native_pso.h:
    • Added DigestHash (mixing both 64-bit halves of the 16-byte digest via a splitmix64 finaliser) and switched HashMap::map_ from std::map<Digest, ShaderRef> to std::unordered_map<Digest, ShaderRef, DigestHash> (retaining #include <map> because runtime/shims/d3d12_xs_native_pso.h relies on it transitively).
    • Pre-reserved map_ in HashMap::parse() using a newline count (std::count) and checked map_.find(*digest) before constructing ShaderRef strings on duplicate digests.
    • Replaced the per-call std::set<Digest> in record_digests() with a small-size linear deduplication path (kLinearDedupLimit = 16) that spills to std::unordered_set<Digest, DigestHash> only for large candidate lists.
    • Scanned bytes(block.records) directly in plan() using a stack Digest instead of allocating a temporary std::vector<Digest> + std::set<Digest> per stage block (identical semantics: duplicate digests hit the same const ShaderRef*, while two distinct matched digests still return PlanStatus::multiple_matches).
    • Pre-reserved hex() (32 bytes) and decode_state_b() (s.elements.reserve(count)), and rewrote the remaining-bytes checks in decode_state_b() as subtraction (b.size() - at < ...) to prevent size_t addition wrap.
  • runtime/test/native_pso_plan_test.cpp & runtime/CMakeLists.txt:
    • Added portable native_pso_plan_test / CTest native_pso_plan covering hex/parse_digest, record_digests (including >16-digest hash spill and duplicates), HashMap::parse validation/duplicates/conflicts, all plan() status paths (including repeated matching digest vs two distinct matching digests), and decode_state_b().

A/B Benchmark (-O2, 25,321 synthetic catalog digests)

Operation Base (main) Head (this PR) Speedup
HashMap::parse (25,321 lines, 2 runs) 7.46 ms / 5.37 ms 5.12 ms / 2.91 ms 1.46× – 1.85×
HashMap::find (per lookup, 2 runs) 79.8 ns / 72.3 ns 11.3 ns / 8.4 ns 7.1× – 8.6×
plan() (VS+PS, per call, 2 runs) 307.0 ns / 281.7 ns 137.2 ns / 108.8 ns 2.24× – 2.59×

Verification

  • AI-assisted: yes
  • GPUs tested: none (host-only portable C++ tests on macOS arm64; no game files used)
  • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror:
    • native_pso_plan_test: PASS
    • native_pso_blend_test: PASS
    • native_pso_depth_test: PASS
  • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror:
    • native_pso_plan_test: PASS
  • clang++ -std=c++20 -O2 -Wall -Wextra -Werror:
    • native_pso_plan_test × 3 runs: PASS
  • Hygiene:
    • python3 -I scripts/check_no_game_data.py runtime/src/native_pso.h runtime/test/native_pso_plan_test.cpp runtime/CMakeLists.txt: PASS
    • python3 -I tests/test_text_write_line_endings.py: PASS (3/3)

Copilot AI balanced review requested due to automatic review settings November 25, 2026 16:17

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 encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#1940 | head b8c59d7 (merged cleanly with main d48a957) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(native_pso_plan|native_pso_blend|native_pso_depth|native_pso_fallback|d3d12_mesh)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) + 25k-digest A/B timing | baseline on main: native_pso_plan absent on main; with test added on main, parse = 6.97 ms, plan = 450.9 ns/call | green on PR+main: 5/5 CTests pass (native_pso_plan at parse = 5.79 ms, plan = 117.7 ns/call — 3.83x faster plan() on MSVC RelWithDebInfo, plus native_pso_blend, native_pso_depth, native_pso_fallback, and d3d12_mesh 4096/4096 pixels matched) on RTX 3090 with 0 Khronos validation errors; check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR

  • Base: main aa918e5d9 + PR head b8c59d7a7
  • Test: ctest -R ^native_pso_plan$
  • Red on main: passes (the new test covers behaviour already on main)
  • Green with the PR: passes; package builds, full ctest shows no new failure
  • Verdict: NO RED (optimization; the new test also passes on main)
  • 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