[PR #1940] runtime: hash native_pso HashMap and avoid heap churn in plan() #1948

Open
opened 2026-11-26 21:19:30 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1940
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: perf/rt-native-pso-hashmap-and-plan


📝 Commits (1)

  • b8c59d7 runtime: hash native_pso HashMap and avoid heap churn in plan()

📊 Changes

3 files changed (+259 additions, -10 deletions)

View changed files

📝 runtime/CMakeLists.txt (+2 -0)
📝 runtime/src/native_pso.h (+47 -10)
➕ runtime/test/native_pso_plan_test.cpp (+210 -0)

📄 Description

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1940 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-native-pso-hashmap-and-plan` --- ### 📝 Commits (1) - [`b8c59d7`](https://github.com/ubiquitarianism/leonida/commit/b8c59d7a718bf9c9fdf25d0e43d1862b6363f90d) runtime: hash native_pso HashMap and avoid heap churn in plan() ### 📊 Changes **3 files changed** (+259 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+2 -0) 📝 `runtime/src/native_pso.h` (+47 -10) ➕ `runtime/test/native_pso_plan_test.cpp` (+210 -0) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1948
No description provided.