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

Open
github-bot wants to merge 1 commit from github-bot/gh-1940 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 16:16 UTC · original
IlyassRS3:perf/rt-native-pso-hashmap-and-plan → main · +259 −10 · 3 files · 1 commits · open

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)
**@⁠IlyassRS3** on GitHub · 2026-11-25 16:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1940) `IlyassRS3:perf/rt-native-pso-hashmap-and-plan` → `main` · +259 −10 · 3 files · 1 commits · open 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:12:01 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-26 04:26 UTC · original · reviewed

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-26 04:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1940#pullrequestreview-6068541976) · **reviewed** Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1940/head:github-bot/gh-1940'
git switch 'github-bot/gh-1940'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1940'
git switch 'github-bot/gh-1940'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1940'
git switch 'github-bot/gh-1940'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1940'
git switch 'main'
git merge --squash 'github-bot/gh-1940'
git switch 'main'
git merge --ff-only 'github-bot/gh-1940'
git switch 'main'
git merge 'github-bot/gh-1940'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.