perf(runtime): replace BufferVaIndex std::map+multiset with O(log N) augmented AVL interval tree (RT-BUFFER-VA-INDEX-AUGMENTED-TREE) #1904

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/1904 into main
IlyassRS3 commented 2026-11-25 15:28:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T15:28:32Z.


Item ID: RT-BUFFER-VA-INDEX-AUGMENTED-TREE (claimed in #1608).

Context

VulkanContext::buffer_at() calls BufferVaIndex::find() on every vertex-buffer and index-buffer binding during draw replay (vulkan_replay.h:2704, 2738) — including before falling back to vk.title_memory() on title-memory VBs/IBs.

Previously, BufferVaIndex stored ranges in a std::map plus a std::multiset<uint64_t> of sizes (longest_), walking backwards from upper_bound until va - start >= longest_. Whenever a large buffer (e.g. a 1 GiB arena/placed heap) is live anywhere lower in the address space, every miss or aliased spill degraded to an O(N) red-black tree walk across thousands of buffers, and every insert() allocated two heap nodes.

Changes

  • runtime/src/buffer_va_index.h:
    • Replaced std::map + std::multiset with a self-balancing 64-byte-node AVL interval tree keyed by (start, id) and augmented with per-subtree max_end_inc = max(min(start + size - 1, UINT64_MAX)) and max_size.
    • find() rejects VAs past root_->max_end_inc in O(1), and uses an iterative O(log N) pruned search (find_rightmost_fast) that skips any subtree whose max_end_inc < target_end_inc, with an overflow-safe fallback (find_rightmost_wrap) when va + (bytes - 1) wraps UINT64_MAX.
    • Exact same tie-breaking rule (greatest (start, id) holding [va, va + bytes), else greatest holding va), O(1) longest() via root_->max_size, zero allocations on in-place replacement, single-node strong-exception-safe allocation on new inserts, plus full copy/move semantics.
  • runtime/test/buffer_va_index_test.cpp:
    • Added near-UINT64_MAX wrap/overflow tests, copy/move checks, zero-allocation replacement + single-allocation failure injection checks, and a mid-gap miss check under a live 1 GiB buffer.

A/B Benchmark (-O2, macOS Apple Silicon, 20,000 buffers + live 1 GiB buffer)

Lookup scenario (20,000 lookups avg) main (std::map + multiset) This PR (augmented AVL) Speedup
Random hit (20,000 buffers) 75.2 ns 47.7 ns 1.58x
Mid-table gap miss under live 1 GiB buffer 25,363.4 ns 42.7 ns 594x
Aliased spill across 10,000 inner buffers 23,793.3 ns 55.2 ns 431x
Miss above live ranges (title-memory VA) 55,123.3 ns 0.6 ns >90,000x

Verification

  • AI-assisted: yes.
  • GPUs tested: none (macOS host, pure C++20 stdlib CPU test; no game files used).
  • runtime/test/buffer_va_index_test.cpp (Clang 20, C++20, -Wall -Wextra -Werror):
    • UBSan (with injected operator new std::bad_alloc tests): PASS
    • -O2 × 3: PASS
  • Standalone 25,000-step randomized differential fuzz harness against linear scan (including overlapping ranges, replacements, erasures, copy/move, and near-UINT64_MAX VAs):
    • ASan + UBSan: PASS
    • TSan: PASS
  • Hygiene: python3 -I scripts/check_no_game_data.py and python3 -I tests/test_text_write_line_endings.py passed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5494293687 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1904). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T15:28:32Z. --- Item ID: `RT-BUFFER-VA-INDEX-AUGMENTED-TREE` (claimed in #1608). ### Context `VulkanContext::buffer_at()` calls `BufferVaIndex::find()` on every vertex-buffer and index-buffer binding during draw replay (`vulkan_replay.h:2704, 2738`) — including before falling back to `vk.title_memory()` on title-memory VBs/IBs. Previously, `BufferVaIndex` stored ranges in a `std::map` plus a `std::multiset<uint64_t>` of sizes (`longest_`), walking backwards from `upper_bound` until `va - start >= longest_`. Whenever a large buffer (e.g. a 1 GiB arena/placed heap) is live anywhere lower in the address space, every miss or aliased spill degraded to an O(N) red-black tree walk across thousands of buffers, and every `insert()` allocated two heap nodes. ### Changes - **`runtime/src/buffer_va_index.h`**: - Replaced `std::map` + `std::multiset` with a self-balancing 64-byte-node AVL interval tree keyed by `(start, id)` and augmented with per-subtree `max_end_inc = max(min(start + size - 1, UINT64_MAX))` and `max_size`. - `find()` rejects VAs past `root_->max_end_inc` in O(1), and uses an iterative O(log N) pruned search (`find_rightmost_fast`) that skips any subtree whose `max_end_inc < target_end_inc`, with an overflow-safe fallback (`find_rightmost_wrap`) when `va + (bytes - 1)` wraps `UINT64_MAX`. - Exact same tie-breaking rule (greatest `(start, id)` holding `[va, va + bytes)`, else greatest holding `va`), O(1) `longest()` via `root_->max_size`, zero allocations on in-place replacement, single-node strong-exception-safe allocation on new inserts, plus full copy/move semantics. - **`runtime/test/buffer_va_index_test.cpp`**: - Added near-`UINT64_MAX` wrap/overflow tests, copy/move checks, zero-allocation replacement + single-allocation failure injection checks, and a mid-gap miss check under a live 1 GiB buffer. ### A/B Benchmark (`-O2`, macOS Apple Silicon, 20,000 buffers + live 1 GiB buffer) | Lookup scenario (20,000 lookups avg) | `main` (`std::map` + `multiset`) | This PR (augmented AVL) | Speedup | |---|---:|---:|---:| | Random hit (20,000 buffers) | `75.2 ns` | **`47.7 ns`** | **1.58x** | | Mid-table gap miss under live 1 GiB buffer | `25,363.4 ns` | **`42.7 ns`** | **594x** | | Aliased spill across 10,000 inner buffers | `23,793.3 ns` | **`55.2 ns`** | **431x** | | Miss above live ranges (title-memory VA) | `55,123.3 ns` | **`0.6 ns`** | **>90,000x** | ### Verification - **AI-assisted**: yes. - **GPUs tested**: none (macOS host, pure C++20 stdlib CPU test; no game files used). - `runtime/test/buffer_va_index_test.cpp` (Clang 20, C++20, `-Wall -Wextra -Werror`): - UBSan (with injected `operator new` `std::bad_alloc` tests): PASS - `-O2` × 3: PASS - Standalone 25,000-step randomized differential fuzz harness against linear scan (including overlapping ranges, replacements, erasures, copy/move, and near-`UINT64_MAX` VAs): - ASan + UBSan: PASS - TSan: PASS - Hygiene: `python3 -I scripts/check_no_game_data.py` and `python3 -I tests/test_text_write_line_endings.py` passed.
copilot-pull-request-reviewer[bot] commented 2026-11-25 15:28:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T15:28:45Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6060506118 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1904#pullrequestreview-6060506118). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T15:28:45Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
lixonik commented 2026-11-25 19:27:55 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-25T19:27:55Z.

Review: COMMENTED


Independent review and CPU testing at 673f01427.

  • buffer_va_index_test passed with GCC/UBSan under WSL.
  • A separate ASan/UBSan harness checked one million mixed insert, replace, erase and lookup steps against a linear oracle. It included overlapping ranges, tied starts, copy/move and cases near UINT64_MAX. All lookups matched.
  • An identical optimized harness on b0f3d3998 and this head measured a median of 39.9 µs versus 70.4 ns for a synthetic gap miss across 20,000 short buffers with a live 1 GiB range below them. Both revisions returned 100,000 misses. This measures index lookups only.

I found no correctness issue in the cases tested. No GPU or game files.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6063712111 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1904#pullrequestreview-6063712111). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-25T19:27:55Z. **Review:** COMMENTED --- Independent review and CPU testing at `673f01427`. - `buffer_va_index_test` passed with GCC/UBSan under WSL. - A separate ASan/UBSan harness checked one million mixed insert, replace, erase and lookup steps against a linear oracle. It included overlapping ranges, tied starts, copy/move and cases near `UINT64_MAX`. All lookups matched. - An identical optimized harness on `b0f3d3998` and this head measured a median of 39.9 µs versus 70.4 ns for a synthetic gap miss across 20,000 short buffers with a live 1 GiB range below them. Both revisions returned 100,000 misses. This measures index lookups only. I found no correctness issue in the cases tested. No GPU or game files. AI-assisted.
MoonEyes commented 2026-11-28 02:51:16 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T02:51:16Z.


Hi @IlyassRS3, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/src/buffer_va_index.h), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7232636256 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1904#issuecomment-7232636256). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T02:51:16Z. --- Hi @IlyassRS3, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `runtime/src/buffer_va_index.h`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
OreoFR commented 2026-11-28 21:55:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:55:59Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head 673f01427
  • Test: not run (merge conflict with main)
  • Red on main: not run
  • Green with the PR: not run
  • Verdict: CONFLICT (runtime/src/buffer_va_index.h conflicts with main)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246377222 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1904#issuecomment-7246377222). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:55:59Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `673f01427` - Test: not run (merge conflict with main) - Red on main: not run - Green with the PR: not run - Verdict: CONFLICT (runtime/src/buffer_va_index.h conflicts with main) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:38:55 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:38:55Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317959715 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1904#issuecomment-7317959715). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:38:55Z. --- 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.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!1904
No description provided.