runtime: add O(1) envelope fast path and explicit wrap to title_texture_refresh (RT-TITLE-TEXTURE-REFRESH-ENVELOPE-AND-WRAP) #2012

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

@⁠IlyassRS3 on GitHub · 2026-11-25 18:06 UTC · original
IlyassRS3:perf/rt-title-texture-refresh-envelope → main · +141 −3 · 3 files · 1 commits · open

Claim: RT-TITLE-TEXTURE-REFRESH-ENVELOPE-AND-WRAP (claim comment #7170422103 on #1608).

Problem

In runtime/src/title_texture_refresh.h:

  1. title_write_overlaps() always executed std::upper_bound before checking begin >= end, writes.empty(), or whether [begin, end) falls outside the merged dirty envelope [writes.front().first, writes.back().second) — paying binary search overhead for thousands of unaffected waiting textures on every refresh pass.
  2. merge_title_writes() unconditionally invoked std::sort even on empty/single-entry or already-sorted write-range lists.
  3. poll_title_textures() incremented cursor = address + 1, relying on implicit unsigned wrap if address == UINT64_MAX.

Fix

  • Short-circuit title_write_overlaps() on empty/inverted queries, outside-envelope ranges (end <= writes.front().first || begin >= writes.back().second), and single-range merges before falling back to std::upper_bound.
  • Short-circuit merge_title_writes() on 0/1-element inputs and skip std::sort when already ordered.
  • Make poll_title_textures() cursor wrap at UINT64_MAX explicit (address == UINT64_MAX ? 0u : address + 1u).
  • Add portable CPU-only CTest title_texture_refresh (runtime/test/title_texture_refresh_test.cpp).

Benchmark (clang++ -std=c++20 -O2, bit-exact A/B, 20,000 waiting textures x 2,000 passes)

  • 64 localized dirty ranges: 154.67 ms -> 18.72 ms (8.26x faster)
  • 1 dirty block (64 KiB): 43.08 ms -> 21.48 ms (2.01x faster)
  • merge_title_writes() on 256 already-sorted ranges (50,000 calls): 17.72 ms -> 12.48 ms (1.42x faster)

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Ran runtime/test/title_texture_refresh_test.cpp cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Ran python3 -I scripts/check_no_game_data.py runtime/src/title_texture_refresh.h runtime/test/title_texture_refresh_test.cpp runtime/CMakeLists.txt and python3 -I tests/test_text_write_line_endings.py: all passed.
**@⁠IlyassRS3** on GitHub · 2026-11-25 18:06 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2012) `IlyassRS3:perf/rt-title-texture-refresh-envelope` → `main` · +141 −3 · 3 files · 1 commits · open Claim: `RT-TITLE-TEXTURE-REFRESH-ENVELOPE-AND-WRAP` (claim comment `#7170422103` on #1608). ### Problem In `runtime/src/title_texture_refresh.h`: 1. `title_write_overlaps()` always executed `std::upper_bound` before checking `begin >= end`, `writes.empty()`, or whether `[begin, end)` falls outside the merged dirty envelope `[writes.front().first, writes.back().second)` — paying binary search overhead for thousands of unaffected waiting textures on every refresh pass. 2. `merge_title_writes()` unconditionally invoked `std::sort` even on empty/single-entry or already-sorted write-range lists. 3. `poll_title_textures()` incremented `cursor = address + 1`, relying on implicit unsigned wrap if `address == UINT64_MAX`. ### Fix - Short-circuit `title_write_overlaps()` on empty/inverted queries, outside-envelope ranges (`end <= writes.front().first || begin >= writes.back().second`), and single-range merges before falling back to `std::upper_bound`. - Short-circuit `merge_title_writes()` on 0/1-element inputs and skip `std::sort` when already ordered. - Make `poll_title_textures()` cursor wrap at `UINT64_MAX` explicit (`address == UINT64_MAX ? 0u : address + 1u`). - Add portable CPU-only CTest `title_texture_refresh` (`runtime/test/title_texture_refresh_test.cpp`). ### Benchmark (`clang++ -std=c++20 -O2`, bit-exact A/B, 20,000 waiting textures x 2,000 passes) - 64 localized dirty ranges: `154.67 ms` -> `18.72 ms` (**8.26x faster**) - 1 dirty block (64 KiB): `43.08 ms` -> `21.48 ms` (**2.01x faster**) - `merge_title_writes()` on 256 already-sorted ranges (50,000 calls): `17.72 ms` -> `12.48 ms` (**1.42x faster**) ### Verification - **AI-assisted**: yes (Antigravity agent by Google DeepMind). - **GPUs tested**: none (host is macOS arm64; no retail files). - Ran `runtime/test/title_texture_refresh_test.cpp` cleanly under: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror` - Ran `python3 -I scripts/check_no_game_data.py runtime/src/title_texture_refresh.h runtime/test/title_texture_refresh_test.cpp runtime/CMakeLists.txt` and `python3 -I tests/test_text_write_line_endings.py`: all passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:44 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 18:07 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 18:07 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2012#pullrequestreview-6062606934) · **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/2012/head:github-bot/gh-2012'
git switch 'github-bot/gh-2012'

Merge

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