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

Closed
leonida/mandy
quietfold wants to merge 0 commits from perf/rt-native-pso-hashmap-and-plan into main

Pull request closed

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

quietfold commented 2026-11-25 16:16:53 +00:00

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)
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-pull-request-reviewer[bot] reviewed 2026-11-26 04:26:05 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
zmasi commented 2026-11-28 12:17:30 +00:00

#1940 | head b8c59d7a718bf9c9fdf25d0e43d1862b6363f90d (merged cleanly with main d48a957bb9988afce424552a3c0c8d01ef12320f) | 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

#1940 | head b8c59d7a718bf9c9fdf25d0e43d1862b6363f90d (merged cleanly with main d48a957bb9988afce424552a3c0c8d01ef12320f) | `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 commented 2026-11-29 02:40:41 +00:00

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

**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).
aslan2142 commented 2026-12-01 16:39:52 +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.