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

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

📋 Pull Request Information

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

Base: main ← Head: perf/rt-buffer-va-index-augmented-tree


📝 Commits (1)

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

📊 Changes

2 files changed (+356 additions, -98 deletions)

View changed files

📝 runtime/src/buffer_va_index.h (+269 -52)
📝 runtime/test/buffer_va_index_test.cpp (+87 -46)

📄 Description

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.

🔄 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/1904 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-buffer-va-index-augmented-tree` --- ### 📝 Commits (1) - [`673f014`](https://github.com/ubiquitarianism/leonida/commit/673f014272420ac02ec9256d969f342fcbc26965) perf(runtime): replace BufferVaIndex std::map+multiset with O(log N) augmented AVL interval tree (RT-BUFFER-VA-INDEX-AUGMENTED-TREE) ### 📊 Changes **2 files changed** (+356 additions, -98 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/buffer_va_index.h` (+269 -52) 📝 `runtime/test/buffer_va_index_test.cpp` (+87 -46) </details> ### 📄 Description 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. --- <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#1913
No description provided.