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

Closed
leonida/mandy
quietfold wants to merge 0 commits from perf/rt-buffer-va-index-augmented-tree into main

Pull request closed

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

quietfold commented 2026-11-25 15:28:32 +00:00

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.
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] reviewed 2026-11-25 15:28:45 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
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 reviewed 2026-11-25 19:27:55 +00:00
lixonik left a comment
No description provided.
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

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

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

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

**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

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.