host: release ready retired objects from behind an out-of-order number #3407

Code

Clone this repository

Checkout

From your project repository, check out a new branch and test the changes.

git fetch -u origin "fix/retired-out-of-order:fix/retired-out-of-order"
git switch "fix/retired-out-of-order"
Open
leonida/mandy
MoonEyes wants to merge 2 commits from fix/retired-out-of-order into main

This pull request has changes conflicting with the target branch.

Merge manually to resolve the conflicts.

Conversation
Reviewers 0 approvals
  • No reviewers
Details

This pull request has changes conflicting with the target branch.

Merge manually to resolve the conflicts.

  • d3d12/docs/findings.md
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'fix/retired-out-of-order:fix/retired-out-of-order'
git switch 'fix/retired-out-of-order'
Member

What changes

retire takes its submission number before it locks its thread's queue of RETIRED, and threads share the 32 queues,
so a lower number can be pushed after a higher one. bury took only each queue's front, as long as the numbers there
were not above the finished submission, so an object that was ready but sat behind a later number waited for that one:
freed late, never early. The comment above bury accepted it.

RetiredQueue (d3d12/crates/leonida-gpu/src/host.rs) keeps the entries and whether their numbers are in order. In
order, the front is taken exactly as before. After an inversion, the next take looks through the whole queue once,
keeps the waiting entries in their order, and the fast path comes back as soon as what is left is in order again.
Nothing is dropped under a queue's lock (the reaper path is unchanged).

To learn whether this happens in game at all, RETIRE_INVERSIONS counts the inversions at push time (one atomic add,
only when one happens). Every fifth second the frame meter writes a line, only when some happened since the last line:

objects let go out of order in their queue (a lower submission number after a higher one): N since the last line, M since the start; entries looked through for them under a queue's lock: S since the last line, T since the start

The second count (RETIRE_SCANNED, second commit, asked for by the independent review) says what an inversion costs:
the whole-queue look runs under the queue's lock, and with LEONIDA_RETIRE_QUEUES=0 every thread shares one queue, so
inversions are expected to be frequent there.

It is not behind the diagnostics feature, so a normal build shows it. No such line in a whole run means no inversion.
findings.md has a short section on it.

Tests

  • cargo test -p leonida-gpu --locked: 733 passed, 0 failed, 59 ignored (Windows 11, no GPU needed).
  • cargo check -p leonida-gpu --features diagnostics --locked: builds (the only warning, in book.rs, is already on main).
  • New unit tests in host::retired_tests:
    • a_queue_in_order_gives_its_front: the ordered case behaves as before, ties included.
    • one_out_of_order_does_not_hold_back_those_behind_it: ready entries behind a later number go at their own
      submission; inversions are reported by push_back; the fast path comes back.
    • what_is_taken_is_dropped_by_the_caller: entries are moved out, not dropped inside the queue.
    • a_queue_takes_what_a_partition_takes: every queue of five pushes from four numbers (ties too), at each
      threshold, against a plain partition.
    • The existing what_is_retired_in_several_queues_goes_after_its_own_submission now expects the object behind a later
      number to go at its own submission.
  • python -I scripts/check_no_game_data.py on the touched files: clean.

In-game run required

Not run in game. Before merging: a run on an 8 GB card (memory is short there, so retired memory freed late matters
most), reading the "objects let go out of order" lines to count the inversions, and 300 s without device loss.

Credit

Ported from Darner commit 412a0ec by ALeCs1080 (https://github.com/tyr0nsur/Darner, GPL-3.0-or-later, like this
repository). The credit is also in the code comment and the commit message.

AI assistance

AI-assisted: written by Claude Opus 5.5 (claude-opus-5-5) in Claude Code, reviewed by a separate Claude Opus 5.5
review agent that did not write it. Submitted by Moon.

🤖 Generated with Claude Code

### What changes `retire` takes its submission number before it locks its thread's queue of `RETIRED`, and threads share the 32 queues, so a lower number can be pushed after a higher one. `bury` took only each queue's front, as long as the numbers there were not above the finished submission, so an object that was ready but sat behind a later number waited for that one: freed late, never early. The comment above `bury` accepted it. `RetiredQueue` (`d3d12/crates/leonida-gpu/src/host.rs`) keeps the entries and whether their numbers are in order. In order, the front is taken exactly as before. After an inversion, the next take looks through the whole queue once, keeps the waiting entries in their order, and the fast path comes back as soon as what is left is in order again. Nothing is dropped under a queue's lock (the reaper path is unchanged). To learn whether this happens in game at all, `RETIRE_INVERSIONS` counts the inversions at push time (one atomic add, only when one happens). Every fifth second the frame meter writes a line, only when some happened since the last line: `objects let go out of order in their queue (a lower submission number after a higher one): N since the last line, M since the start; entries looked through for them under a queue's lock: S since the last line, T since the start` The second count (`RETIRE_SCANNED`, second commit, asked for by the independent review) says what an inversion costs: the whole-queue look runs under the queue's lock, and with `LEONIDA_RETIRE_QUEUES=0` every thread shares one queue, so inversions are expected to be frequent there. It is not behind the `diagnostics` feature, so a normal build shows it. No such line in a whole run means no inversion. `findings.md` has a short section on it. ### Tests - `cargo test -p leonida-gpu --locked`: 733 passed, 0 failed, 59 ignored (Windows 11, no GPU needed). - `cargo check -p leonida-gpu --features diagnostics --locked`: builds (the only warning, in `book.rs`, is already on main). - New unit tests in `host::retired_tests`: - `a_queue_in_order_gives_its_front`: the ordered case behaves as before, ties included. - `one_out_of_order_does_not_hold_back_those_behind_it`: ready entries behind a later number go at their own submission; inversions are reported by `push_back`; the fast path comes back. - `what_is_taken_is_dropped_by_the_caller`: entries are moved out, not dropped inside the queue. - `a_queue_takes_what_a_partition_takes`: every queue of five pushes from four numbers (ties too), at each threshold, against a plain partition. - The existing `what_is_retired_in_several_queues_goes_after_its_own_submission` now expects the object behind a later number to go at its own submission. - `python -I scripts/check_no_game_data.py` on the touched files: clean. ### In-game run required Not run in game. Before merging: a run on an 8 GB card (memory is short there, so retired memory freed late matters most), reading the "objects let go out of order" lines to count the inversions, and 300 s without device loss. ### Credit Ported from Darner commit `412a0ec` by ALeCs1080 (https://github.com/tyr0nsur/Darner, GPL-3.0-or-later, like this repository). The credit is also in the code comment and the commit message. ### AI assistance AI-assisted: written by Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, reviewed by a separate Claude Opus 5.5 review agent that did not write it. Submitted by Moon. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
retire() takes the submission number before the lock of its thread's
queue, and threads share the 32 queues, so a lower number can be pushed
after a higher one. bury() took only each queue's front while the numbers
were not above the finished submission: what was ready behind a later
number waited for it (freed late, never early).

RetiredQueue keeps the entries and whether their numbers are in order. In
order the front is taken as before; after an inversion the next take looks
through the whole queue once, keeps the waiting entries in their order, and
the fast path comes back once what is left is in order. Nothing is dropped
under a queue's lock.

RETIRE_INVERSIONS counts the inversions; every fifth second of the frame
meter a line says how many came since the last line, only when some did,
so a run tells whether it happens in game.

Tests (retired_tests): the front in order, one out of order no longer
holding back those behind it, what is taken dropped by the caller, and
every queue of five pushes from four numbers against a plain partition;
the existing several-queues test now expects the one behind a later
number to go at its own submission.

Ported from Darner 412a0ec (ALeCs1080, GPL-3.0-or-later).
Not run in game: count the inversions on an 8 GB card.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After an inversion, select_ready looks through the whole queue under its
lock until the queue is in order again. RETIRE_SCANNED counts the entries
it went through, and the inversions line (every fifth second of the frame
meter, only when something changed) now also says how many, so an in-game
run shows what the inversions cost and not only how many there were.
Asked for by the independent review of the previous commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
OreoFR referenced this pull request from a commit 2026-12-04 02:26:46 +00:00
Sign in to join this conversation.
No description provided.