mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-27 00:20:53 +01:00
[PR #1904] perf(runtime): replace BufferVaIndex std::map+multiset with O(log N) augmented AVL interval tree (RT-BUFFER-VA-INDEX-AUGMENTED-TREE) #1913
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#1913
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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)
673f014perf(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()callsBufferVaIndex::find()on every vertex-buffer and index-buffer binding during draw replay (vulkan_replay.h:2704, 2738) — including before falling back tovk.title_memory()on title-memory VBs/IBs.Previously,
BufferVaIndexstored ranges in astd::mapplus astd::multiset<uint64_t>of sizes (longest_), walking backwards fromupper_bounduntilva - 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 everyinsert()allocated two heap nodes.Changes
runtime/src/buffer_va_index.h:std::map+std::multisetwith a self-balancing 64-byte-node AVL interval tree keyed by(start, id)and augmented with per-subtreemax_end_inc = max(min(start + size - 1, UINT64_MAX))andmax_size.find()rejects VAs pastroot_->max_end_incin O(1), and uses an iterative O(log N) pruned search (find_rightmost_fast) that skips any subtree whosemax_end_inc < target_end_inc, with an overflow-safe fallback (find_rightmost_wrap) whenva + (bytes - 1)wrapsUINT64_MAX.(start, id)holding[va, va + bytes), else greatest holdingva), O(1)longest()viaroot_->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:UINT64_MAXwrap/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)main(std::map+multiset)75.2 ns47.7 ns25,363.4 ns42.7 ns23,793.3 ns55.2 ns55,123.3 ns0.6 nsVerification
runtime/test/buffer_va_index_test.cpp(Clang 20, C++20,-Wall -Wextra -Werror):operator newstd::bad_alloctests): PASS-O2× 3: PASSUINT64_MAXVAs):python3 -I scripts/check_no_game_data.pyandpython3 -I tests/test_text_write_line_endings.pypassed.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.