runtime: optimize ArenaBlocks walk, node reuse, and power-of-two align_up (RT-ARENA-BLOCKS-PERF-AND-REUSE) #1985

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

@⁠IlyassRS3 on GitHub · 2026-11-25 17:26 UTC · original
IlyassRS3:perf/rt-arena-blocks-perf-and-reuse → main · +118 −37 · 2 files · 1 commits · open

Item ID: RT-ARENA-BLOCKS-PERF-AND-REUSE (claimed in #1608)

Summary

Optimizes leonida::ArenaBlocks (runtime/src/arena_blocks.h), the GPU address arena's free-block allocator used on resource creation and destruction:

  1. Sequential walk without repeated O(log N) tree descents: allocate() previously restarted blocks_.lower_bound(cursor) from the RB-tree root on every single free block and called blocks_.find(block) before cut(). Undersized or unaligned blocks never invoke keep_out(), and when keep_out() runs without mutating blocks_ (checked via a monotonic mutation_ counter), the existing iterator remains valid — turning the first-fit walk from O(k log N) into O(k) sequential iterator increments and cutting directly without a redundant lookup.
  2. Re-entrant reset() detection: tracks a separate monotonic mutation_ counter incremented in reset(), cut(), and release() (leaving cuts() unchanged), so a re-entrant reset() inside keep_out() when cuts() == 0 is still caught cleanly.
  3. O(1) total-free-space early rejection: returns false immediately when bytes > free_bytes_ (when no block can satisfy bytes and keep_out() could never be invoked).
  4. Power-of-two align_up() fast path: replaces 64-bit modulo/division with overflow-safe bitmask arithmetic for power-of-two alignments while keeping the exact fallback for arbitrary alignments.
  5. Zero-allocation RB-tree node reuse in cut() and release():
    • cut() shrinks the node in place on suffix/middle cuts, uses std::map::extract() + hinted insert() on prefix cuts (reusing the node with zero heap allocation), and uses emplace_hint() on middle splits.
    • release() extends an adjacent left neighbor in place, recycles an absorbed right node via extract() + hinted insert(), and uses emplace_hint() for isolated inserts.
  6. Expanded unit tests in runtime/test/arena_blocks_test.cpp covering 2^63 power-of-two overflow, non-power-of-two alignment, re-entrant reset() when cuts() == 0, bytes > free_bytes() fast reject, and all prefix/middle/suffix/left/right/3-way coalesce paths.

Benchmarks (-O2, bit-exact A/B vs upstream/main)

  • Fragmented 2,000-hole scan × 5,000 alloc/release: 94.926 ms → 16.776 ms (5.66× faster)
  • Mixed alloc/release churn (200k ops, 256 slots): 20.393 ms → 11.721 ms (1.74× faster)
  • Oversized reject (1,000 holes × 20,000 calls): 0.079 ms → 0.036 ms (2.18× faster)

Testing & Disclosure

  • AI-assisted: yes
  • GPUs tested: none (macOS host, CPU-only portable unit test and A/B benchmark)
  • clang++ -std=c++20 -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -Wall -Wextra -Werror runtime/test/arena_blocks_test.cpp: PASS
  • clang++ -std=c++20 -g -O1 -fsanitize=thread -fno-omit-frame-pointer -Wall -Wextra -Werror runtime/test/arena_blocks_test.cpp: PASS
  • clang++ -std=c++20 -O2 -Wall -Wextra -Werror runtime/test/arena_blocks_test.cpp: PASS
  • python3 -I scripts/check_no_game_data.py runtime/src/arena_blocks.h runtime/test/arena_blocks_test.cpp: PASS
  • python3 -I tests/test_text_write_line_endings.py: PASS (3/3)
**@⁠IlyassRS3** on GitHub · 2026-11-25 17:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1985) `IlyassRS3:perf/rt-arena-blocks-perf-and-reuse` → `main` · +118 −37 · 2 files · 1 commits · open Item ID: `RT-ARENA-BLOCKS-PERF-AND-REUSE` (claimed in #1608) ## Summary Optimizes `leonida::ArenaBlocks` (`runtime/src/arena_blocks.h`), the GPU address arena's free-block allocator used on resource creation and destruction: 1. **Sequential walk without repeated O(log N) tree descents**: `allocate()` previously restarted `blocks_.lower_bound(cursor)` from the RB-tree root on every single free block and called `blocks_.find(block)` before `cut()`. Undersized or unaligned blocks never invoke `keep_out()`, and when `keep_out()` runs without mutating `blocks_` (checked via a monotonic `mutation_` counter), the existing iterator remains valid — turning the first-fit walk from O(k log N) into O(k) sequential iterator increments and cutting directly without a redundant lookup. 2. **Re-entrant `reset()` detection**: tracks a separate monotonic `mutation_` counter incremented in `reset()`, `cut()`, and `release()` (leaving `cuts()` unchanged), so a re-entrant `reset()` inside `keep_out()` when `cuts() == 0` is still caught cleanly. 3. **O(1) total-free-space early rejection**: returns `false` immediately when `bytes > free_bytes_` (when no block can satisfy `bytes` and `keep_out()` could never be invoked). 4. **Power-of-two `align_up()` fast path**: replaces 64-bit modulo/division with overflow-safe bitmask arithmetic for power-of-two alignments while keeping the exact fallback for arbitrary alignments. 5. **Zero-allocation RB-tree node reuse in `cut()` and `release()`**: - `cut()` shrinks the node in place on suffix/middle cuts, uses `std::map::extract()` + hinted `insert()` on prefix cuts (reusing the node with zero heap allocation), and uses `emplace_hint()` on middle splits. - `release()` extends an adjacent left neighbor in place, recycles an absorbed right node via `extract()` + hinted `insert()`, and uses `emplace_hint()` for isolated inserts. 6. **Expanded unit tests** in `runtime/test/arena_blocks_test.cpp` covering 2^63 power-of-two overflow, non-power-of-two alignment, re-entrant `reset()` when `cuts() == 0`, `bytes > free_bytes()` fast reject, and all prefix/middle/suffix/left/right/3-way coalesce paths. ## Benchmarks (`-O2`, bit-exact A/B vs `upstream/main`) - Fragmented 2,000-hole scan × 5,000 alloc/release: 94.926 ms → **16.776 ms** (**5.66×** faster) - Mixed alloc/release churn (200k ops, 256 slots): 20.393 ms → **11.721 ms** (**1.74×** faster) - Oversized reject (1,000 holes × 20,000 calls): 0.079 ms → **0.036 ms** (**2.18×** faster) ## Testing & Disclosure - AI-assisted: yes - GPUs tested: none (macOS host, CPU-only portable unit test and A/B benchmark) - `clang++ -std=c++20 -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -Wall -Wextra -Werror runtime/test/arena_blocks_test.cpp`: PASS - `clang++ -std=c++20 -g -O1 -fsanitize=thread -fno-omit-frame-pointer -Wall -Wextra -Werror runtime/test/arena_blocks_test.cpp`: PASS - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror runtime/test/arena_blocks_test.cpp`: PASS - `python3 -I scripts/check_no_game_data.py runtime/src/arena_blocks.h runtime/test/arena_blocks_test.cpp`: PASS - `python3 -I tests/test_text_write_line_endings.py`: PASS (3/3)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:13:53 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 17:26 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 17:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1985#pullrequestreview-6062039616) · **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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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