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

Merged
IlyassRS3 merged 1 commit from upstream-pr/2012 into main 2026-11-27 21:42:10 +00:00
IlyassRS3 commented 2026-11-25 18:06:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T18:06:51Z.


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.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5496592118 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2012). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T18:06:51Z. --- 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.
copilot-pull-request-reviewer[bot] commented 2026-11-25 18:07:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T18:07:05Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6062606934 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2012#pullrequestreview-6062606934). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T18:07:05Z. **Review:** COMMENTED --- 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.
Tyrannys commented 2026-11-27 20:48:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-27T20:48:04Z.


Profile M data point with #1705 and #2012 applied together (RTX 4070 SUPER 12 GB, NVIDIA 621.01, Windows 11). Counts only. AI-assisted (Claude Code).

The textures submit phase (Device::refresh_title_textures) after 900 s, means per submission:

  • c3-G1c, without #1705 / #2012: 474.6 ms, 0.22 frames/s (opening sequence)
  • c3-G1d, with #1705 + #2012: 24.0 ms, 0.30 frames/s (opening cutscene, then gameplay control from ~1,200 s)

Not a one-variable A/B: G1d also changed LEONIDA_KEYBOARD_PAD (always -> 1), LEONIDA_CAPTURE_LIMIT (40 -> 80) and ran on caches one run warmer, and the two pull requests are not separated. One run each, PROVISIONAL; refuted if the same tree without the two pull requests also shows tens of ms. Not run: #1705's new case in d3d12_existing_heap_test, because that test file does not compile in this local stack for an unrelated earlier merge; title_texture_refresh_test (#2012) passes.

RUN c3-G1d | 2026-11-27 | Tyrannys | AI-assisted: Claude Code
BASE:        2859d0c9b + #2233 #2275 #2418 #2440 #2020 #2166 #2432 #2224 #2179 #2228 #1851 #1882 #2260 #2267 #1592 (+ local test fix) + #443 + #1705 + #2012 | shaders: library re-translated with w64-one-lane-fold; DirectStorage predictor profile regenerated after 2a491668a
MACHINE:     RTX 4070 SUPER 12 GB, desktop | NVIDIA 621.01 | Windows 11 | 64 GB
RECIPE:      OreoFR's world recipe (#1608, 2026-11-27 08:23) + DEVIATIONS: COMPILE_GATE=0 PIPELINE_PREWARM_ORDER=1 SHADOW_ALIAS_MS=1000 SKIP_COMPUTE=<1,009 ray-hit programs> RT_AS=off DEVICE_FAULT=1 TITLE_HUD=1 MEM_PROBE=60 FRAME_PACING=1 PRESENT_NO_TEAR=1 KEYBOARD_PAD=1 CAPTURE_LIMIT=80 | precompile: yes, library programs except the skip list
OS/DRIVER:   TdrDelay=60
TARGET:      textures phase ms per submission after 900 s; how far | PREDICTED: textures at least halved vs c3-G1c (474.6); < 1 frame/s; autosave prompt or gameplay
MEASURED:    full 1,804 s, PresentX 3,191 ok / 0 failed, 11.0 M D3D12 calls, 0 decompression errors, 0 device stalls, 0 presses besides the script (235)
             world-start pipeline burst ~270 s; letterboxed near-white cutscene from ~600 s; by ~1,200 s the game's own control hint
             ("move around / move the camera") over a screen that is almost all white (mean RGB 252-254) to the end
             after 900 s (means): textures 24.0 ms per submission (c3-G1c 474.6), heaps 12.8, shadows 19.7; 7.7 submissions/s; 0.30 frames/s
             process dedicated / shared (MiB, means): burst+0..180 s 10,777 / 1,446; +180..240 s 10,695 / 1,696; +240 s..end 10,759 / 2,345
             (shared max 7,107 after a +5,120 MiB rise at ~1,664 s); HUD VRAM 10.0-13.6 GB used vs a 10.7 GB budget
             3D engine mean: 8.4% (0..300 s), 9.8%, 59.9%, 29.8%; compute 0.0%, copy 0.0-1.0%
             skip list: 250 of the 1,009 skipped programs were requested, each first by ~600 s, none first requested during gameplay;
             their dispatches are dropped. Loop-guard hits: not observable (the translator's guard exits silently, no counter)
END:         time limit
CACHES:      warm (journal + session/heavy caches after G1b, G1b-ctl, G1c) | RUN 1 of 1
INSTRUMENTS: TITLE_HUD, MEM_PROBE=60, DEVICE_FAULT=1, Windows GPU performance counters every 10 s, gputaskman --json every 30 s
VERDICT:     prediction held (textures down ~95%; 0.30 frames/s; gameplay control reached, picture white)
<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7227852177 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2012#issuecomment-7227852177). Originally posted by [Tyrannys](https://github.com/Tyrannys) on 2026-11-27T20:48:04Z. --- Profile M data point with #1705 and #2012 applied together (RTX 4070 SUPER 12 GB, NVIDIA 621.01, Windows 11). Counts only. AI-assisted (Claude Code). The `textures` submit phase (`Device::refresh_title_textures`) after 900 s, means per submission: - c3-G1c, without #1705 / #2012: **474.6 ms**, 0.22 frames/s (opening sequence) - c3-G1d, with #1705 + #2012: **24.0 ms**, 0.30 frames/s (opening cutscene, then gameplay control from ~1,200 s) Not a one-variable A/B: G1d also changed `LEONIDA_KEYBOARD_PAD` (always -> 1), `LEONIDA_CAPTURE_LIMIT` (40 -> 80) and ran on caches one run warmer, and the two pull requests are not separated. One run each, PROVISIONAL; refuted if the same tree without the two pull requests also shows tens of ms. Not run: #1705's new case in `d3d12_existing_heap_test`, because that test file does not compile in this local stack for an unrelated earlier merge; `title_texture_refresh_test` (#2012) passes. ``` RUN c3-G1d | 2026-11-27 | Tyrannys | AI-assisted: Claude Code BASE: 2859d0c9b + #2233 #2275 #2418 #2440 #2020 #2166 #2432 #2224 #2179 #2228 #1851 #1882 #2260 #2267 #1592 (+ local test fix) + #443 + #1705 + #2012 | shaders: library re-translated with w64-one-lane-fold; DirectStorage predictor profile regenerated after 2a491668a MACHINE: RTX 4070 SUPER 12 GB, desktop | NVIDIA 621.01 | Windows 11 | 64 GB RECIPE: OreoFR's world recipe (#1608, 2026-11-27 08:23) + DEVIATIONS: COMPILE_GATE=0 PIPELINE_PREWARM_ORDER=1 SHADOW_ALIAS_MS=1000 SKIP_COMPUTE=<1,009 ray-hit programs> RT_AS=off DEVICE_FAULT=1 TITLE_HUD=1 MEM_PROBE=60 FRAME_PACING=1 PRESENT_NO_TEAR=1 KEYBOARD_PAD=1 CAPTURE_LIMIT=80 | precompile: yes, library programs except the skip list OS/DRIVER: TdrDelay=60 TARGET: textures phase ms per submission after 900 s; how far | PREDICTED: textures at least halved vs c3-G1c (474.6); < 1 frame/s; autosave prompt or gameplay MEASURED: full 1,804 s, PresentX 3,191 ok / 0 failed, 11.0 M D3D12 calls, 0 decompression errors, 0 device stalls, 0 presses besides the script (235) world-start pipeline burst ~270 s; letterboxed near-white cutscene from ~600 s; by ~1,200 s the game's own control hint ("move around / move the camera") over a screen that is almost all white (mean RGB 252-254) to the end after 900 s (means): textures 24.0 ms per submission (c3-G1c 474.6), heaps 12.8, shadows 19.7; 7.7 submissions/s; 0.30 frames/s process dedicated / shared (MiB, means): burst+0..180 s 10,777 / 1,446; +180..240 s 10,695 / 1,696; +240 s..end 10,759 / 2,345 (shared max 7,107 after a +5,120 MiB rise at ~1,664 s); HUD VRAM 10.0-13.6 GB used vs a 10.7 GB budget 3D engine mean: 8.4% (0..300 s), 9.8%, 59.9%, 29.8%; compute 0.0%, copy 0.0-1.0% skip list: 250 of the 1,009 skipped programs were requested, each first by ~600 s, none first requested during gameplay; their dispatches are dropped. Loop-guard hits: not observable (the translator's guard exits silently, no counter) END: time limit CACHES: warm (journal + session/heavy caches after G1b, G1b-ctl, G1c) | RUN 1 of 1 INSTRUMENTS: TITLE_HUD, MEM_PROBE=60, DEVICE_FAULT=1, Windows GPU performance counters every 10 s, gputaskman --json every 30 s VERDICT: prediction held (textures down ~95%; 0.30 frames/s; gameplay control reached, picture white) ```
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!2012
No description provided.