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

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

@⁠IlyassRS3 on GitHub · 2026-11-25 15:28 UTC · original
IlyassRS3:perf/rt-buffer-va-index-augmented-tree → main · +356 −98 · 2 files · 1 commits · open

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.
**@⁠IlyassRS3** on GitHub · 2026-11-25 15:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1904) `IlyassRS3:perf/rt-buffer-va-index-augmented-tree` → `main` · +356 −98 · 2 files · 1 commits · open 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:10:41 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 15:28 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 15:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1904#pullrequestreview-6060506118) · **reviewed** 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.
Author
Owner

@⁠lixonik on GitHub · 2026-11-25 19:27 UTC · original · reviewed

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.

**@⁠lixonik** on GitHub · 2026-11-25 19:27 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1904#pullrequestreview-6063712111) · **reviewed** 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.
This pull request has changes conflicting with the target branch.
  • runtime/src/buffer_va_index.h
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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