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

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

📋 Pull Request Information

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

Base: main ← Head: perf/rt-arena-blocks-perf-and-reuse


📝 Commits (1)

  • 5735c1c runtime: optimize ArenaBlocks walk, node reuse, and power-of-two align_up

📊 Changes

2 files changed (+118 additions, -37 deletions)

View changed files

📝 runtime/src/arena_blocks.h (+68 -37)
📝 runtime/test/arena_blocks_test.cpp (+50 -0)

📄 Description

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)

🔄 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/1985 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-arena-blocks-perf-and-reuse` --- ### 📝 Commits (1) - [`5735c1c`](https://github.com/ubiquitarianism/leonida/commit/5735c1cf65341adbd57034057391bea6eceff7d5) runtime: optimize ArenaBlocks walk, node reuse, and power-of-two align_up ### 📊 Changes **2 files changed** (+118 additions, -37 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/arena_blocks.h` (+68 -37) 📝 `runtime/test/arena_blocks_test.cpp` (+50 -0) </details> ### 📄 Description 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) --- <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#1992
No description provided.