mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 23:05:53 +01:00
[PR #1985] runtime: optimize ArenaBlocks walk, node reuse, and power-of-two align_up (RT-ARENA-BLOCKS-PERF-AND-REUSE) #1992
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#1992
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/1985
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open
Base:
main← Head:perf/rt-arena-blocks-perf-and-reuse📝 Commits (1)
5735c1cruntime: 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:allocate()previously restartedblocks_.lower_bound(cursor)from the RB-tree root on every single free block and calledblocks_.find(block)beforecut(). Undersized or unaligned blocks never invokekeep_out(), and whenkeep_out()runs without mutatingblocks_(checked via a monotonicmutation_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.reset()detection: tracks a separate monotonicmutation_counter incremented inreset(),cut(), andrelease()(leavingcuts()unchanged), so a re-entrantreset()insidekeep_out()whencuts() == 0is still caught cleanly.falseimmediately whenbytes > free_bytes_(when no block can satisfybytesandkeep_out()could never be invoked).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.cut()andrelease():cut()shrinks the node in place on suffix/middle cuts, usesstd::map::extract()+ hintedinsert()on prefix cuts (reusing the node with zero heap allocation), and usesemplace_hint()on middle splits.release()extends an adjacent left neighbor in place, recycles an absorbed right node viaextract()+ hintedinsert(), and usesemplace_hint()for isolated inserts.runtime/test/arena_blocks_test.cppcovering 2^63 power-of-two overflow, non-power-of-two alignment, re-entrantreset()whencuts() == 0,bytes > free_bytes()fast reject, and all prefix/middle/suffix/left/right/3-way coalesce paths.Benchmarks (
-O2, bit-exact A/B vsupstream/main)Testing & Disclosure
clang++ -std=c++20 -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -Wall -Wextra -Werror runtime/test/arena_blocks_test.cpp: PASSclang++ -std=c++20 -g -O1 -fsanitize=thread -fno-omit-frame-pointer -Wall -Wextra -Werror runtime/test/arena_blocks_test.cpp: PASSclang++ -std=c++20 -O2 -Wall -Wextra -Werror runtime/test/arena_blocks_test.cpp: PASSpython3 -I scripts/check_no_game_data.py runtime/src/arena_blocks.h runtime/test/arena_blocks_test.cpp: PASSpython3 -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.