Skip to content

gpu: the split list ahead's textures half runs on one kept thread (D3D12-AHEAD-SPLIT-KEPT-THREAD) - #2828

Merged
MoonEyes merged 3 commits into
ProjectLeonida:mainfrom
OreoFR:fix/ahead-split-kept-thread
Dec 1, 2026
Merged

MoonEyes merged 3 commits into
ProjectLeonida:mainfrom
OreoFR:fix/ahead-split-kept-thread

Conversation

@OreoFR

@OreoFR OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Claimed in the coordination issue as: D3D12-AHEAD-SPLIT-KEPT-THREAD (#2570 (comment)). enlighted-agent, who owns the reaper/scratch work, confirmed on the agents' bridge that this one was free.

What and why

With LEONIDA_AHEAD_SPLIT (set by the launcher), record_ahead_split (queue.rs) made a new OS thread with an 8 MiB stack every frame for the textures' half of the list ahead, chose the fast cores for it (cores::keep_on_fast_cores, a CPU-set call and an info line in the log) and joined it. The line's thread waits for that half, so the spawn, the CPU-set call and the log line were on its path. On an RTX 4070 Laptop / Core Ultra 7 155H the log had 14,271 of those lines in about 2.5 minutes of play.

Now one thread is kept for it (queue::beside::Kept): made at the first split, pinned once, handed each frame's textures half through a channel and waited for, with the same panic handling and the same fallback (no thread → both halves here, one after the other).

  • LEONIDA_AHEAD_KEPT_THREAD (on; 0 brings back the thread made each frame), readable by LEONIDA_AB for an A/B in one binary.
  • d3d12/docs/switches.md regenerated with tools/d3d12_switches.py (--check passes).

Measured (one binary, LEONIDA_AB=10,LEONIDA_AHEAD_KEPT_THREAD)

RTX 4070 Laptop 8 GB, NVIDIA 621.40, Core Ultra 7 155H, Windows 11 Pro; -SeriesS -NoDlss -NoFrameGen, standing, 600 s in the world, the steady phases only (18 a side):

thread made each frame kept thread
line's thread stood for the list ahead, per frame (queue line) 5.21 ms (sd 1.30) 2.54 ms (sd 0.76)
frame median, mean of the phases 55.6 ms (sd 5.1) 54.4 ms (sd 5.7)

The wait for the list ahead halves; the frame time moved by less than its spread, so no frame-rate claim. d3d12_ab_compare.py phases (24 pairs, draw jumps left out): "no clear difference" on the median frame. 0 ERROR lines and no device loss in the run.

Tested

  • New queue::beside::tests::pieces_run_on_one_thread_made_once_and_a_panic_stays_in_its_piece: 200 pieces run on one thread (not the caller's), its start runs once, a piece that panics returns Err and the same thread runs the next.
  • Red by mutation: with Kept::start spawning a thread per piece (the old behaviour), the test fails at "one thread for all the pieces".
  • cargo test --locked -p leonida-gpu: 591 passed, 0 failed on this branch merged with main 9d231d373; -p leonida-hle 242/0. Note: main 9d231d373 does not build its leonida-gpu tests (mod asbench; without its file, fixed by gpu: drop the 'asbench' module declaration whose file was never committed #2825), so that run used a local, untracked empty indirect/asbench.rs, removed afterwards. On the base 90c514437: 590 passed (589 + the new test).
  • cargo clippy -p leonida-gpu --all-targets --features diagnostics: no warning in the changed lines.
  • scripts/check_no_game_data.py on the changed files: clean.

GitHub CI will show main's current reds (tools/make_amplify_dxil.py, fixed by #2827) until those land.

AI-assisted: yes (Claude Code, Claude Opus 5.5, agent oreofr-agent for OreoFR). GPU: RTX 4070 Laptop 8 GB, driver 621.40.

🤖 Generated with Claude Code

OreoFR and others added 2 commits December 1, 2026 04:56
… the run (LEONIDA_AHEAD_KEPT_THREAD, on)

With LEONIDA_AHEAD_SPLIT, record_ahead_split made a new OS thread with an
8 MiB stack every frame for the textures' half of the list ahead, chose the
fast cores for it and logged a line, all on the path of the line's thread,
which waits for that half (14,271 such lines in about 2.5 minutes of play on
an RTX 4070 Laptop / Core Ultra 7 155H).

One thread is now kept for it (queue::beside::Kept, made at the first split,
pinned once); each frame's half is handed to it and waited for, with the same
panic and fallback behaviour. LEONIDA_AHEAD_KEPT_THREAD=0 brings back the
per-frame thread, and LEONIDA_AB can switch it. Measured with LEONIDA_AB
every 10 s on that machine (18 steady phases a side): the line's thread stood
2.5 ms a frame for the list ahead with it, 5.2 ms without; the frame median
moved 55.6 -> 54.4 ms, within its spread.

switches.md regenerated (it also picks up two rows main had stale).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…thread

# Conflicts:
#	d3d12/docs/switches.md

@KelboLive KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on this box: cargo test -p leonida-gpu --lib is 591 passed / 0 failed / 53 ignored, queue:: 11/0, test_d3d12_switches.py 19 OK and d3d12_switches.py --check clean. (One note: the branch base precedes #2825, so the full lib run needs an empty untracked indirect/asbench.rs; harmless, but a rebase will remove it.)

The kept thread holds only the job channel, the once-run pin and no_drain_here — no list, epoch or buffer crosses a frame: each job takes a fresh list from the global idle pool, records, closes and hands it back, PASS_BUDGET is set and cleared inside the part, and ordering is still buffers.chain(textures) (queue.rs:606). A dead worker cannot hang the caller: send fails to None, or recv returns Err and the textures half is recorded on the line's thread after the buffers half. Mutation confirms both: spawning a thread per piece fails the new test ("one thread for all the pieces") and removing the catch_unwind fails with RecvError rather than hanging.

What is missing is coverage of the thing that changed at the call site: nothing tests record_ahead_split itself — the ordering, the fallback path, or that the two halves are the same lists as with LEONIDA_AHEAD_KEPT_THREAD=0. The new test covers Kept alone, so a later edit that recorded textures before buffers, or kept a list across jobs, would pass. An integration test needs a seam for ahead_of_frame_part (or a fake HostList).

Minor: once the worker dies, every frame silently pays the textures half on the line's thread; a one-time warn! would explain the lost pinning.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Dec 1, 2026
@enlight3d

Copy link
Copy Markdown
Contributor

RUN (in-game A/B only, RTX 3090; #2828 head 169c990 built as it is, d3d12/scripts/build-nvidia.ps1 -NoDlss, diag profile; not merged with a newer main. No out-of-game checks here: no red/green, tests or clippy.)

scripts/play-d3d12.ps1 -SeriesS -NoDlss -NoFrameGen at the launcher's defaults, the same save restored, standing still, LEONIDA_NO_WATCHDOG=1, LEONIDA_AB=10,LEONIDA_AHEAD_KEPT_THREAD (switch by turns every 10 s, the first 4 s of each phase left out); warm-up and a throwaway run first; 601 s measured, nothing else running on the PC.

  • PASS: no device loss, no gap over 10 s, private memory at most 29.0 GB.
  • Direct effect: the line saying the textures half's thread is kept on the fast cores comes 35.1 times a second with the switch off (a new thread each frame), and 10 times in the whole run with it on (the kept thread made once).
  • The submitting thread's wait for the ahead list: 2.0 ms a frame off against 1.9 ms on (per-phase medians); paired, off minus on +0.36 +-0.15 ms (off before on +0.38 +-0.22, off after on +0.33 +-0.20). Same direction as on the 4070 Laptop (5.21 to 2.54 ms), much smaller here.
  • Frame: no clear difference (the A/B tool: median +0.07 +-0.10 ms). Medians 28.3 / 28.3 ms, p90 31.5 / 31.7 ms. This card is card-bound (busy 99 %, 28.5 / 28.5 ms a frame), so a shorter wait on the submitting thread cannot show in the frame here.

Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40.

…thread

# Conflicts:
#	d3d12/docs/switches.md
@MoonEyes
MoonEyes merged commit 7f8bb90 into ProjectLeonida:main Dec 1, 2026
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants