runtime: early-exit pending_through and cache semaphore completions in RetireTickets #2025

Merged
leonida/mandy
quietfold merged 1 commit from perf/rt-retire-tickets-early-break-and-sem-cache into main 2026-11-28 17:54:21 +00:00

Pull request successfully merged and closed

quietfold commented 2026-11-25 18:28:20 +00:00

Coordination item: RT-RETIRE-TICKETS-EARLY-BREAK-AND-SEM-CACHE (claimed on #1608).

Why

  1. RetireTickets::pending_through(ticket) scanned the entire pending_ deque even though normal record() calls arrive in non-decreasing ticket order (as documented at the top of retire_tickets.h). Under deletion-queue backpressure (VulkanBackend::wait_retire_ticket(old_ticket)), scanning thousands of newer in-flight submissions wastes mutex-held CPU time under submit_mutex_.
  2. RetireTickets::completed(query) invoked the caller's Vulkan semaphore query (vkGetSemaphoreCounterValue) for every non-idle pending entry, even when a previous entry in the same pass already confirmed an equal or higher timeline value on that semaphore.

Changes

  • runtime/src/retire_tickets.h:
    • Track monotonic_ (automatically reset to true when pending_ drains, cleared if an out-of-order ticket < pending_.back().ticket is recorded) so pending_through(ticket) returns immediately when ticket < pending_.front().ticket and breaks as soon as entry.ticket > ticket in the monotonic case, while preserving full-scan exactness for out-of-order tickets.
    • Cache up to 8 (semaphore, highest_confirmed_value) entries on the stack inside completed(query) so subsequent entries in the same pass with front.value <= confirmed skip redundant query() calls.
  • runtime/test/retire_tickets_test.cpp: add tests for per-pass confirmed-value caching, >8-semaphore cache eviction, non-monotonic pending_through() fallback, monotonic reset after drain, and cutoff-before-front early return.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (CPU-only unit tests & benchmark on macOS arm64).
  • Compiled and ran runtime/test/retire_tickets_test.cpp with clang++ -std=c++20 -Wall -Wextra -Werror:
    • ASan+UBSan (-O1 -g -fsanitize=address,undefined): PASS.
    • TSan (-O1 -g -fsanitize=thread): PASS.
    • Release (-O2): PASS.
  • A/B -O2 benchmark (50,000 calls to pending_through(16) on 4,096 pending submissions across 3 queues):
    • main: 137.223 ms (acc=2400000)
    • this branch: 8.417 ms (acc=2400000, 16.30× faster, bit-exact).
  • python3 -I scripts/check_no_game_data.py runtime/src/retire_tickets.h runtime/test/retire_tickets_test.cpp: clean.
  • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.
Coordination item: `RT-RETIRE-TICKETS-EARLY-BREAK-AND-SEM-CACHE` (claimed on #1608). ### Why 1. `RetireTickets::pending_through(ticket)` scanned the entire `pending_` deque even though normal `record()` calls arrive in non-decreasing ticket order (as documented at the top of `retire_tickets.h`). Under deletion-queue backpressure (`VulkanBackend::wait_retire_ticket(old_ticket)`), scanning thousands of newer in-flight submissions wastes mutex-held CPU time under `submit_mutex_`. 2. `RetireTickets::completed(query)` invoked the caller's Vulkan semaphore query (`vkGetSemaphoreCounterValue`) for every non-idle pending entry, even when a previous entry in the same pass already confirmed an equal or higher timeline value on that semaphore. ### Changes - `runtime/src/retire_tickets.h`: - Track `monotonic_` (automatically reset to `true` when `pending_` drains, cleared if an out-of-order ticket `< pending_.back().ticket` is recorded) so `pending_through(ticket)` returns immediately when `ticket < pending_.front().ticket` and breaks as soon as `entry.ticket > ticket` in the monotonic case, while preserving full-scan exactness for out-of-order tickets. - Cache up to 8 `(semaphore, highest_confirmed_value)` entries on the stack inside `completed(query)` so subsequent entries in the same pass with `front.value <= confirmed` skip redundant `query()` calls. - `runtime/test/retire_tickets_test.cpp`: add tests for per-pass confirmed-value caching, >8-semaphore cache eviction, non-monotonic `pending_through()` fallback, monotonic reset after drain, and cutoff-before-front early return. ### Verification - AI-assisted: yes (Antigravity agent by Google DeepMind). - GPUs tested: none (CPU-only unit tests & benchmark on macOS arm64). - Compiled and ran `runtime/test/retire_tickets_test.cpp` with `clang++ -std=c++20 -Wall -Wextra -Werror`: - ASan+UBSan (`-O1 -g -fsanitize=address,undefined`): PASS. - TSan (`-O1 -g -fsanitize=thread`): PASS. - Release (`-O2`): PASS. - A/B `-O2` benchmark (50,000 calls to `pending_through(16)` on 4,096 pending submissions across 3 queues): - `main`: `137.223 ms` (`acc=2400000`) - this branch: `8.417 ms` (`acc=2400000`, **16.30× faster**, bit-exact). - `python3 -I scripts/check_no_game_data.py runtime/src/retire_tickets.h runtime/test/retire_tickets_test.cpp`: clean. - `python3 -I tests/test_text_write_line_endings.py`: 3/3 OK.
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 18:28:32 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
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.
zmasi commented 2026-11-28 11:51:50 +00:00

Verified on Windows 11 (10.0.26200.9974) with MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) against main 24549bdb1 + cf5127e81 (claim #2570#issuecomment-7238799031):

  • Line-by-line diff audit: Clean (runtime/src/retire_tickets.h, runtime/test/retire_tickets_test.cpp); caches confirmed timeline values per semaphore (8-slot ring) within completed() to skip redundant counter queries on repeated/lower values, and tracks monotonic_ ticket order so pending_through() early-exits when monotonic while remaining exact when out-of-order tickets are recorded. check_no_game_data.py clean.
  • Red (24549bdb1 + PR test): retire_tickets_test.exe fails (rc=8, redundant semaphore queries on repeated/lower timeline values).
  • Green (24549bdb1 + cf5127e81 merge): Merges cleanly (rc=0), compiles with 0 /W4 warnings, and retire_tickets_test.exe passes (out-of-order queue completion kept the retirement prefix safe, rc=0).

AI-assisted.

Verified on Windows 11 (`10.0.26200.9974`) with MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2`) against `main` `24549bdb1` + `cf5127e81` (claim `#2570#issuecomment-7238799031`): - **Line-by-line diff audit**: Clean (`runtime/src/retire_tickets.h`, `runtime/test/retire_tickets_test.cpp`); caches confirmed timeline values per semaphore (8-slot ring) within `completed()` to skip redundant counter queries on repeated/lower values, and tracks `monotonic_` ticket order so `pending_through()` early-exits when monotonic while remaining exact when out-of-order tickets are recorded. `check_no_game_data.py` clean. - **Red (`24549bdb1` + PR test)**: `retire_tickets_test.exe` fails (`rc=8`, redundant semaphore queries on repeated/lower timeline values). - **Green (`24549bdb1` + `cf5127e81` merge)**: Merges cleanly (`rc=0`), compiles with `0` `/W4` warnings, and `retire_tickets_test.exe` passes (`out-of-order queue completion kept the retirement prefix safe`, `rc=0`). AI-assisted.
MoonEyes commented 2026-11-28 17:54:18 +00:00

Red/green: On main retire_tickets_test exits with code 8 (completions are not cached, so retired/query counts are wrong), and with the PR ctest -R "^retire_tickets$" passes.
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.

Red/green: On main retire_tickets_test exits with code 8 (completions are not cached, so retired/query counts are wrong), and with the PR ctest -R "^retire_tickets$" passes. Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
OreoFR commented 2026-11-28 21:58:13 +00:00

Verified by OreoFR

  • Base: main c1ecf04ed + PR head cf5127e81; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R ^retire_tickets$
  • Red on main: exit code 8 (retired != 14 or queries != 3: completions not cached)
  • Green with the PR: OK
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

**Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `cf5127e81`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R ^retire_tickets$` - Red on main: exit code 8 (retired != 14 or queries != 3: completions not cached) - Green with the PR: OK - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
Sign in to join this conversation.
No description provided.