d3d12-gpu: map the translated DXIL through a temporary arena, so 270 MiB can leave the working set #3460

Code

Clone this repository

Checkout

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

git fetch -u  "d3d12/shader-map-dxil:KelboLive-d3d12/shader-map-dxil"
git switch "KelboLive-d3d12/shader-map-dxil"
Open
leonida/mandy
KelboLive wants to merge 2 commits from KelboLive/mandy:d3d12/shader-map-dxil into main

This pull request can be merged automatically.

Conversation
Reviewers 0 approvals
  • No reviewers
Details
This pull request can be merged automatically.
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 'd3d12/shader-map-dxil:KelboLive-d3d12/shader-map-dxil'
git switch 'KelboLive-d3d12/shader-map-dxil'
First-time contributor

What/why

#3261 / #3312 (D3D12-SHADER-BYTES-MAPPED): Translated.host.dxil is our translated DXIL, created once per (container, mask) and never let go - 270 MiB in the audited run (4,236 translations), on top of 188 MiB of title containers. It is read at every HostPipeline::new, including pipeline-library hits, because the description and library name are built before library::pipeline.

This maps dxil only through an arena of temporary delete-on-close files, so its pages can leave the working set instead of staying in private commit. Shader.container is untouched: mapping the title's original bytes to a temp file is a separate policy decision.

Design

  • New pipeline/bytes.rs: Bytes::{Memory(Vec<u8>), Mapped(Blob)} with bytes(), len(), trim(); Blob { arena: Arc<Arena>, at, len }; one PID-named arena (FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, explicit read/write/delete sharing), 64 MiB MapViewOfFile chunks, a mutex bump allocator, VirtualUnlock trim, and Drop unmaps then closes (delete-on-close removes the file).
  • Any file/mapping failure returns Bytes::Memory with the original Vec, so the switch is a best-effort optimisation, not a new failure mode.
  • Translated.dxil is a Bytes; Translated::dxil() is the accessor. HostShader is unchanged, and the translator/tests keep their type.
  • host::sweep calls pipeline::trim_shader_map(); trimming is idle/ram_idle_ms gated, runs on leonida-shader-map-trim, and only VirtualUnlocks pages - it never unmaps or remaps a live view (pointer stability is a test).
  • Startup removes only leonida-shader-map-<pid>-<n>.bin leftovers and only when no live process holds them.

Switch

LEONIDA_SHADER_MAP is on only for exactly 1; unset/0/anything else uses Bytes::Memory and is byte-identical to today. Read once per process; the switch-off path never creates a file. Switch page regenerated.

Tests

  • New pipeline::bytes tests: 10 passed - reload after trim, dropped store removes the file, mapping failure falls back, Send + Sync, second chunk keeps the first blob's address, switch-off byte identity, switch-on mapping, stale-name rule, switch parser, From off path.
  • cargo test -p leonida-gpu --lib -- --test-threads=1: 765 passed, 0 failed, 60 ignored.
  • clippy clean; no-game-data, switch page and diff checks clean.

Not verified here (no game run)

Real D3D12 pipeline creation from mapped bytecode; a genuine MapViewOfFile failure after a successful file create (the fallback is tested via file-create failure); actual working-set eviction from VirtualUnlock (the test proves bytes survive, not that pages leave); the full 270 MiB arena at the 64 MiB chunk size (tests use 64 KiB); and concurrent puts from translator workers beyond the compile-time Send + Sync bound.

A run should compare the shader containers / translations and private/mapped lines with the switch on and off, plus hitch p99 and page-input counters around trims. Until then this is a prototype behind an off-by-default switch.

Item: D3D12-SHADER-BYTES-MAPPED, claimed in #3312. Depends on: none.
AI-assisted: DeepSeek V4.1 Flash (OpenCode); an audit and a synthetic 64 MiB prototype preceded it.

## What/why #3261 / #3312 (D3D12-SHADER-BYTES-MAPPED): `Translated.host.dxil` is our translated DXIL, created once per (container, mask) and **never let go** - 270 MiB in the audited run (4,236 translations), on top of 188 MiB of title containers. It is read at every `HostPipeline::new`, including pipeline-library hits, because the description and library name are built before `library::pipeline`. This maps **dxil only** through an arena of temporary delete-on-close files, so its pages can leave the working set instead of staying in private commit. `Shader.container` is untouched: mapping the title's original bytes to a temp file is a separate policy decision. ## Design - New `pipeline/bytes.rs`: `Bytes::{Memory(Vec<u8>), Mapped(Blob)}` with `bytes()`, `len()`, `trim()`; `Blob { arena: Arc<Arena>, at, len }`; one PID-named arena (`FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE`, explicit read/write/delete sharing), 64 MiB `MapViewOfFile` chunks, a mutex bump allocator, `VirtualUnlock` trim, and `Drop` unmaps then closes (delete-on-close removes the file). - Any file/mapping failure returns `Bytes::Memory` with the original `Vec`, so the switch is a best-effort optimisation, not a new failure mode. - `Translated.dxil` is a `Bytes`; `Translated::dxil()` is the accessor. `HostShader` is unchanged, and the translator/tests keep their type. - `host::sweep` calls `pipeline::trim_shader_map()`; trimming is idle/`ram_idle_ms` gated, runs on `leonida-shader-map-trim`, and only `VirtualUnlock`s pages - it never unmaps or remaps a live view (pointer stability is a test). - Startup removes only `leonida-shader-map-<pid>-<n>.bin` leftovers and only when no live process holds them. ## Switch `LEONIDA_SHADER_MAP` is on only for exactly `1`; unset/`0`/anything else uses `Bytes::Memory` and is byte-identical to today. Read once per process; the switch-off path never creates a file. Switch page regenerated. ## Tests - New `pipeline::bytes` tests: **10 passed** - reload after trim, dropped store removes the file, mapping failure falls back, `Send + Sync`, second chunk keeps the first blob's address, switch-off byte identity, switch-on mapping, stale-name rule, switch parser, `From` off path. - `cargo test -p leonida-gpu --lib -- --test-threads=1`: **765 passed, 0 failed, 60 ignored**. - clippy clean; no-game-data, switch page and diff checks clean. ## Not verified here (no game run) Real D3D12 pipeline creation from mapped bytecode; a genuine `MapViewOfFile` failure after a successful file create (the fallback is tested via file-create failure); actual working-set eviction from `VirtualUnlock` (the test proves bytes survive, not that pages leave); the full 270 MiB arena at the 64 MiB chunk size (tests use 64 KiB); and concurrent puts from translator workers beyond the compile-time `Send + Sync` bound. A run should compare the `shader containers / translations` and private/mapped lines with the switch on and off, plus hitch p99 and page-input counters around trims. Until then this is a prototype behind an off-by-default switch. Item: D3D12-SHADER-BYTES-MAPPED, claimed in #3312. Depends on: none. AI-assisted: DeepSeek V4.1 Flash (OpenCode); an audit and a synthetic 64 MiB prototype preceded it.
Keep `Translated.host.dxil` in a temp-file arena instead of the process
heap when `LEONIDA_SHADER_MAP=1`; unset or `0` keeps every byte in a `Vec`
exactly as before. The arena is one PID-named
`FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE` file, mapped in
fixed 64 MiB chunks and bumped into under one mutex. A blob never moves or
remaps while it lives; the sweep lets its pages go on idle or pressure,
never unmapping a live view. Any file or mapping failure falls back to
`Bytes::Memory` with the bytes untouched.

`Shader.container` is unchanged: this is phase 1, host code only.
Refuse a translation larger than one view before it is copied (it stays in memory), so the bump allocator cannot write past a mapped chunk. Take host.dxil out only after the last fallible check, so a refused translation never reaches the arena, and say so in Translated's doc.

Clean same-PID leftovers from a crashed run (the live-handle probe protects a live arena) and only accept <n> digits in a stale name. Correct the mapping/trim comments, warn once when a later chunk cannot be mapped, and snapshot the chunk list before trimming.

Tests: oversize, exact fit, zero length, concurrent puts with a trimmer, stale cleanup, and a mapped translation that reads back the in-memory bytes from a synthetic console pixel shader container.
Author
First-time contributor

Review fixes are in at 17de0c292 (a read-only audit found one must-fix).

  • Oversize hole fixed: Arena::put now rejects a blob larger than the chunk (> chunk -> Bytes::Memory), so a 70 MiB translation can never write past a 64 MiB view. Test: chunk+1 returns None and the earlier blob still reads back.
  • Map only after every fallible check: translate() is split into translate() + translate_with_map(...); mem::take(&mut host.dxil)/Bytes::with_mapping now happen after the GROUP_PACK refusal, so a refused translation does not orphan arena bytes.
  • Stale cleanup: the own-PID skip is removed (same-PID crash leftovers are cleaned; the live-handle probe still protects a live arena) and a non-numeric <n> is rejected.
  • Safety/doc: the map_chunk SAFETY note (mapping handle closed immediately, file open for the arena), the trim wording (pressure only shortens the idle threshold; one trim per burst) and the drop wording (dirty pages go to the temp file; the gain is working set/private commit) are corrected. Translated.dxil says it is taken out even with the switch off.
  • Robustness: one-time warn when a later chunk fails to map; trim snapshots the chunk list under the lock before VirtualUnlock.

Tests now include oversize, exact-fit, zero length, 8-thread concurrent puts with a trimmer looping, clean_stale (unopened own-PID removed, share-delete-held and non-matching kept), and an integration test with a synthetic console shader container showing a mapped translation reads back byte-identical to the in-memory one and both host.dxil are empty - no device needed.

16 bytes tests, 772 full serial, clippy -D warnings, switch page, no-game-data and diff clean. Still no game run: the real memory drop and pipeline creation from mapped bytecode remain the run-side check.

Review fixes are in at **`17de0c292`** (a read-only audit found one must-fix). - **Oversize hole fixed:** `Arena::put` now rejects a blob larger than the chunk (`> chunk` -> `Bytes::Memory`), so a 70 MiB translation can never write past a 64 MiB view. Test: chunk+1 returns `None` and the earlier blob still reads back. - **Map only after every fallible check:** `translate()` is split into `translate()` + `translate_with_map(...)`; `mem::take(&mut host.dxil)`/`Bytes::with_mapping` now happen after the `GROUP_PACK` refusal, so a refused translation does not orphan arena bytes. - **Stale cleanup:** the own-PID skip is removed (same-PID crash leftovers are cleaned; the live-handle probe still protects a live arena) and a non-numeric `<n>` is rejected. - **Safety/doc:** the `map_chunk` SAFETY note (mapping handle closed immediately, file open for the arena), the trim wording (pressure only shortens the idle threshold; one trim per burst) and the drop wording (dirty pages go to the temp file; the gain is working set/private commit) are corrected. `Translated.dxil` says it is taken out even with the switch off. - **Robustness:** one-time warn when a later chunk fails to map; trim snapshots the chunk list under the lock before `VirtualUnlock`. Tests now include oversize, exact-fit, zero length, 8-thread concurrent puts with a trimmer looping, `clean_stale` (unopened own-PID removed, share-delete-held and non-matching kept), and an **integration test with a synthetic console shader container** showing a mapped translation reads back byte-identical to the in-memory one and both `host.dxil` are empty - no device needed. **16 bytes tests, 772 full serial**, clippy `-D warnings`, switch page, no-game-data and diff clean. Still no game run: the real memory drop and pipeline creation from mapped bytecode remain the run-side check.
KelboLive force-pushed d3d12/shader-map-dxil from 17de0c2928 to a8bb3b3cb1 2026-12-04 15:10:40 +00:00 Compare
Author
First-time contributor

Rebased onto main 44c480a5f (head a8bb3b3cb). No conflicts. Re-ran: 775 passed, 0 failed, 60 ignored.

Rebased onto `main` `44c480a5f` (head `a8bb3b3cb`). No conflicts. Re-ran: 775 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/shader-map-dxil from a8bb3b3cb1 to 729b137646 2026-12-04 15:57:13 +00:00 Compare
Author
First-time contributor

Replayed onto main bba262b94 (head 729b13764). No conflicts; switch page --check ok. Re-ran: 794 passed, 0 failed, 60 ignored.

Replayed onto `main` `bba262b94` (head `729b13764`). No conflicts; switch page `--check` ok. Re-ran: 794 passed, 0 failed, 60 ignored.
First-time contributor

RUN — In-game & Unit Test Gate (NVIDIA RTX 5070 12 GB, Windows 11)

Tested PR head a8bb3b3cb merged cleanly onto main (bba262b94), diagnostic release build (-SeriesS, no frame generation, LEONIDA_SHADER_BYTES_MAPPED=1, 315 s automated run from the same save), compared against our recent main (30c683055) baseline:

Metric main baseline (30c683055) PR #3460 (a8bb3b3cb, LEONIDA_SHADER_BYTES_MAPPED=1)
World samples (>=1000 draws) 36 (max_draws=5187) 32 (max_draws=5371)
Median FPS / frame time 23.0 fps (43.5 ms) 22.9 fps (43.7 ms)
Slowest 10% / fastest 10% 13.7 / 25.0 fps 15.5 / 24.5 fps
3000-4999 draws/frame median 23.2 fps (28 samples) 23.2 fps (21 samples)
Errors / warnings / device lost 0 / 0 / 0 0 / 0 / 0

Unit & Repo Checks

  • cargo test -p leonida-gpu --lib --locked pipeline::bytes:: — 16/16 PASS
  • cargo test -p leonida-gpu --lib --locked shader_cache — 6/6 PASS
  • cargo clippy -p leonida-gpu --all-targets --locked -- -D warnings — 0 warnings
  • python -I tools/d3d12_switches.py --check d3d12/docs/switches.md — PASS
  • python -I scripts/check_no_game_data.py — PASS

Verdict

PASS — clean merge onto bba262b94, all 16/16 arena tests and clippy/switch checks pass, and the 315 s live run with LEONIDA_SHADER_BYTES_MAPPED=1 completed with zero errors and no FPS regression. Ready for merge.

AI-assisted.

### RUN — In-game & Unit Test Gate (NVIDIA RTX 5070 12 GB, Windows 11) Tested PR head `a8bb3b3cb` merged cleanly onto `main` (`bba262b94`), diagnostic release build (`-SeriesS`, no frame generation, `LEONIDA_SHADER_BYTES_MAPPED=1`, 315 s automated run from the same save), compared against our recent `main` (`30c683055`) baseline: | Metric | `main` baseline (`30c683055`) | PR `#3460` (`a8bb3b3cb`, `LEONIDA_SHADER_BYTES_MAPPED=1`) | | --- | --- | --- | | World samples (`>=1000` draws) | `36` (`max_draws=5187`) | `32` (`max_draws=5371`) | | Median FPS / frame time | `23.0 fps` (`43.5 ms`) | `22.9 fps` (`43.7 ms`) | | Slowest 10% / fastest 10% | `13.7` / `25.0 fps` | `15.5` / `24.5 fps` | | `3000-4999` draws/frame median | `23.2 fps` (`28` samples) | `23.2 fps` (`21` samples) | | Errors / warnings / device lost | `0` / `0` / `0` | `0` / `0` / `0` | ### Unit & Repo Checks - `cargo test -p leonida-gpu --lib --locked pipeline::bytes::` — `16/16` PASS - `cargo test -p leonida-gpu --lib --locked shader_cache` — `6/6` PASS - `cargo clippy -p leonida-gpu --all-targets --locked -- -D warnings` — `0` warnings - `python -I tools/d3d12_switches.py --check d3d12/docs/switches.md` — PASS - `python -I scripts/check_no_game_data.py` — PASS ### Verdict **PASS** — clean merge onto `bba262b94`, all `16/16` arena tests and `clippy`/switch checks pass, and the 315 s live run with `LEONIDA_SHADER_BYTES_MAPPED=1` completed with zero errors and no FPS regression. Ready for merge. AI-assisted.
KelboLive force-pushed d3d12/shader-map-dxil from 729b137646 to aea13b29a4 2026-12-04 16:16:02 +00:00 Compare
Author
First-time contributor

Replayed onto main e1be896b2 (head aea13b29a). No conflicts; switch page --check passes. 794 passed, 0 failed, 60 ignored.

Replayed onto `main` `e1be896b2` (head `aea13b29a`). No conflicts; switch page `--check` passes. 794 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/shader-map-dxil from aea13b29a4 to 4f557700fa 2026-12-04 16:32:39 +00:00 Compare
Author
First-time contributor

Replayed onto main 00e245be5 (head 4f557700f). No conflicts; switch page --check ok. 804 passed, 0 failed, 60 ignored.

Replayed onto `main` `00e245be5` (head `4f557700f`). No conflicts; switch page `--check` ok. 804 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/shader-map-dxil from 4f557700fa to 73e1b0d151 2026-12-04 17:15:19 +00:00 Compare
Author
First-time contributor

Replayed onto main f22b9fa64 (head 73e1b0d15). No conflicts; switch page ok. 807 passed, 0 failed, 60 ignored.

Replayed onto `main` `f22b9fa64` (head `73e1b0d15`). No conflicts; switch page ok. 807 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/shader-map-dxil from 73e1b0d151 to 590dd625d3 2026-12-04 18:30:27 +00:00 Compare
Author
First-time contributor

Replayed onto main 6491bc1a1 (head 590dd625d). No conflicts; switch page ok. 807 passed, 0 failed, 60 ignored.

Replayed onto `main` `6491bc1a1` (head `590dd625d`). No conflicts; switch page ok. 807 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/shader-map-dxil from 590dd625d3 to 3ea5744a2c 2026-12-04 18:44:20 +00:00 Compare
Author
First-time contributor

Replayed onto main 6b0498d37 (head 3ea5744a2). No conflicts. 807 passed, 0 failed, 60 ignored.

Replayed onto `main` `6b0498d37` (head `3ea5744a2`). No conflicts. 807 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/shader-map-dxil from 3ea5744a2c to 59d297cb09 2026-12-04 20:53:03 +00:00 Compare
Author
First-time contributor

Replayed onto main 0036195a7 (head 59d297cb0). No conflicts. 807 passed, 0 failed, 60 ignored.

Replayed onto `main` `0036195a7` (head `59d297cb0`). No conflicts. 807 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/shader-map-dxil from 59d297cb09 to 5266bf7137 2026-12-04 23:20:27 +00:00 Compare
Author
First-time contributor

Replayed onto main f4b0404e9 (head 5266bf713). No conflicts. 807 passed, 0 failed, 60 ignored.

Replayed onto `main` `f4b0404e9` (head `5266bf713`). No conflicts. 807 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/shader-map-dxil from 5266bf7137 to 13d6acf939 2026-12-05 05:34:50 +00:00 Compare
Author
First-time contributor

Replayed onto main 93b9d90a2 (head 13d6acf93). No conflicts. 807 passed, 0 failed, 60 ignored.

Replayed onto `main` `93b9d90a2` (head `13d6acf93`). No conflicts. 807 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/shader-map-dxil from 13d6acf939 to 6c157d7fa6 2026-12-05 08:44:29 +00:00 Compare
Author
First-time contributor

Replayed onto main c5355dd25 (head 6c157d7fa). No conflicts. 808 passed, 0 failed, 60 ignored.

Replayed onto `main` `c5355dd25` (head `6c157d7fa`). No conflicts. 808 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/shader-map-dxil from 6c157d7fa6 to d811825d64 2026-12-05 08:53:24 +00:00 Compare
Author
First-time contributor

Replayed onto main e1b0248c1 (head d811825d6). No conflicts. 808 passed, 0 failed, 60 ignored.

Replayed onto `main` `e1b0248c1` (head `d811825d6`). No conflicts. 808 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/shader-map-dxil from d811825d64 to 4ec7c1f570 2026-12-05 09:59:44 +00:00 Compare
Author
First-time contributor

Replayed onto main d99bb4d7a (head 4ec7c1f57). No conflicts. 811 passed, 0 failed, 60 ignored.

Replayed onto `main` `d99bb4d7a` (head `4ec7c1f57`). No conflicts. 811 passed, 0 failed, 60 ignored.
Sign in to join this conversation.
No description provided.