D3D12-SHADER-ITE-LOSSY-MEMO: keep the results of Bools::ite in a table that forgets (-12 % of full-archive translate time) #2773

Manually merged
github merged 1 commit from github-bot/gh-2773 into gh-pr/2773/base 2026-11-30 20:16:49 +00:00 AGit
Owner

@⁠bobcatchris on GitHub · 2026-11-30 18:01 UTC · original
bobcatchris:perf/d3d12-shader-ite-lossy-memo → main · +274 −15 · 1 files · 1 commits · merged 2026-11-30 20:12 UTC

Claim: D3D12-SHADER-ITE-LOSSY-MEMO (claim in #2570).

What

Bools::ite in leonida-shader/src/structure.rs, the core of the lane-mask structuriser, kept every result in a hash map. A result depends on its triple alone, and the nodes made on the way to it stay in the diagram, so a result that is asked for again after it was dropped finds those nodes and makes none: the table can forget without the diagram, its node numbers or anything built from them changing.

The results now live in IteCache, a set-associative table of four 16-byte entries to a 64-byte line (newest of a set first). It doubles, keeping what it holds, while the diagram has more nodes than it has entries, up to one entry per possible node (MAX_NODES). A lookup touches one line (a hash map touches its control bytes and then the entry), and nothing is rehashed one entry at a time as it grows.

Bools::mk also makes a new node with one probe of unique (entry) where it looked the key up and then inserted it.

Safe Rust, no new dependency, no change to what any caller sees.

Measured

leonida translate of the whole archive, 16 threads on a Ryzen 7 7800X3D, built with --features diagnostics,mimalloc (the build the game uses), four interleaved pairs of this branch against main (08e42a516), final code:

wall CPU (user + kernel)
main 46.5, 46.5, 47.1, 48.3 s (mean 47.1) 653.5, 652.9, 653.5, 657.0 s (mean 654.2)
this branch 40.9, 41.3, 40.6, 41.9 s (mean 41.2) 572.1, 575.5, 572.3, 576.4 s (mean 574.1)

That is -12.5 % wall and -12.2 % CPU.

Sizing the table (measured with the first version of the index function, two runs of each): as many entries as nodes was the best of six settings; twice and four times as many entries as nodes, and a half, a quarter and an eighth as many, used 3 % to 19 % more user CPU.

DXIL is unchanged. Every emitted DXIL was hashed (blake3) and compared with a main binary built the same way: 25,689 programs, 15,799 distinct, the same multiset in all four runs of this branch.

Where it does not help: with the system heap (cargo build --release -p leonida, the build of README-fxceless.md) and 16 threads I measured no change: six interleaved pairs, CPU 908 s on main and 913 s here (+0.5 %, inside the run-to-run spread of the machine, which was busy: walls from 66 to 94 s). The translator on that build waits on the allocator, so time taken out of the diagram work does not show. I did not find out more than that.

Tests

cargo test -p leonida-shader --release: 388 passed, 5 ignored. New:

  • the_memo_keeps_the_newest_results_of_a_set_and_forgets_the_rest: of ten results put into one set, the last four are found, no other triple is.
  • the_memo_grows_to_its_limit_and_keeps_what_it_holds.
  • a_memo_that_forgets_gives_the_nodes_one_that_keeps_all_gives: 400 random and/or/xor/not/ite over 8 variables on three diagrams (default memo, a memo of four entries that cannot grow, one with room for all): the same node at every step, the same node list at the end, and every function checked against its truth table over all 256 assignments. Asking the small one the whole script again makes no node.

Negative controls: a lookup that ignores the third key of the triple, an insert that does not push the older entries down, a growth that drops its entries and a growth that ignores its limit each make at least one of them fail (I made each change, ran them, and reverted it).

Not in this pull request

Nothing from the game: no archive, shader, hash or log is in it, and scripts/check_no_game_data.py is clean on the one file. It does not touch my open #2726 (Bools::given), which edits other lines of the same file; whichever merges second takes a merge.

AI-assisted: Claude Sonnet 5.5. Measured on an RX 7900 XTX machine (no GPU is used by translate).

**@⁠bobcatchris** on GitHub · 2026-11-30 18:01 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2773) `bobcatchris:perf/d3d12-shader-ite-lossy-memo` → `main` · +274 −15 · 1 files · 1 commits · merged 2026-11-30 20:12 UTC Claim: D3D12-SHADER-ITE-LOSSY-MEMO ([claim](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/2570) in #2570). ## What `Bools::ite` in `leonida-shader/src/structure.rs`, the core of the lane-mask structuriser, kept every result in a hash map. A result depends on its triple alone, and the nodes made on the way to it stay in the diagram, so a result that is asked for again after it was dropped finds those nodes and makes none: the table can forget without the diagram, its node numbers or anything built from them changing. The results now live in `IteCache`, a set-associative table of four 16-byte entries to a 64-byte line (newest of a set first). It doubles, keeping what it holds, while the diagram has more nodes than it has entries, up to one entry per possible node (`MAX_NODES`). A lookup touches one line (a hash map touches its control bytes and then the entry), and nothing is rehashed one entry at a time as it grows. `Bools::mk` also makes a new node with one probe of `unique` (`entry`) where it looked the key up and then inserted it. Safe Rust, no new dependency, no change to what any caller sees. ## Measured `leonida translate` of the whole archive, 16 threads on a Ryzen 7 7800X3D, built with `--features diagnostics,mimalloc` (the build the game uses), four interleaved pairs of this branch against main (`08e42a516`), final code: | | wall | CPU (user + kernel) | |---|---|---| | main | 46.5, 46.5, 47.1, 48.3 s (mean 47.1) | 653.5, 652.9, 653.5, 657.0 s (mean 654.2) | | this branch | 40.9, 41.3, 40.6, 41.9 s (mean 41.2) | 572.1, 575.5, 572.3, 576.4 s (mean 574.1) | That is -12.5 % wall and -12.2 % CPU. Sizing the table (measured with the first version of the index function, two runs of each): as many entries as nodes was the best of six settings; twice and four times as many entries as nodes, and a half, a quarter and an eighth as many, used 3 % to 19 % more user CPU. **DXIL is unchanged.** Every emitted DXIL was hashed (blake3) and compared with a main binary built the same way: 25,689 programs, 15,799 distinct, the same multiset in all four runs of this branch. **Where it does not help:** with the system heap (`cargo build --release -p leonida`, the build of `README-fxceless.md`) and 16 threads I measured no change: six interleaved pairs, CPU 908 s on main and 913 s here (+0.5 %, inside the run-to-run spread of the machine, which was busy: walls from 66 to 94 s). The translator on that build waits on the allocator, so time taken out of the diagram work does not show. I did not find out more than that. ## Tests `cargo test -p leonida-shader --release`: 388 passed, 5 ignored. New: - `the_memo_keeps_the_newest_results_of_a_set_and_forgets_the_rest`: of ten results put into one set, the last four are found, no other triple is. - `the_memo_grows_to_its_limit_and_keeps_what_it_holds`. - `a_memo_that_forgets_gives_the_nodes_one_that_keeps_all_gives`: 400 random `and`/`or`/`xor`/`not`/`ite` over 8 variables on three diagrams (default memo, a memo of four entries that cannot grow, one with room for all): the same node at every step, the same node list at the end, and every function checked against its truth table over all 256 assignments. Asking the small one the whole script again makes no node. Negative controls: a lookup that ignores the third key of the triple, an insert that does not push the older entries down, a growth that drops its entries and a growth that ignores its limit each make at least one of them fail (I made each change, ran them, and reverted it). ## Not in this pull request Nothing from the game: no archive, shader, hash or log is in it, and `scripts/check_no_game_data.py` is clean on the one file. It does not touch my open #2726 (`Bools::given`), which edits other lines of the same file; whichever merges second takes a merge. AI-assisted: Claude Sonnet 5.5. Measured on an RX 7900 XTX machine (no GPU is used by `translate`).
`Bools::ite` kept every result in a hash map. A result depends on its
triple alone, and the nodes made on the way to it stay in the diagram, so
a result that is asked for again after it was dropped finds those nodes and
makes none: the table can forget without the diagram changing. It is now a
set-associative table of four 16-byte entries to a cache line (the newest
of a set first), which doubles, keeping what it holds, while the diagram
has more nodes than it has entries. A lookup touches one line, and nothing
is rehashed as the table grows.

`Bools::mk` also makes a new node with one probe of its table (`entry`)
where it looked the key up and then inserted it.

Measured on a full-archive `leonida translate` (16 threads, mimalloc
build), four interleaved pairs against main: wall 47.1 s -> 41.2 s, CPU
(user + kernel) 654 s -> 574 s. The blake3 of every emitted DXIL (25,689
programs, 15,799 distinct) is the same as main's in all four runs. On the
system heap the same pairs show no change (six pairs, CPU +0.5 %).

Tests: the table keeps the newest of a set and forgets the rest, grows to
its limit and keeps what it holds, and a diagram whose table holds four
entries gives the same nodes, in the same places, as one with room for
all, and every function holds where the one it stands for does.

AI-assisted: Claude Sonnet 5.5

Co-authored-by: Cursor <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 18:03:07 +00:00
Author
Owner

@⁠OreoFR on GitHub · 2026-11-30 18:06 UTC · original

Verified by OreoFR on macOS 27.2.1, Apple M5, Rust (rustup), main ad36782c6 merged with head 6bbaeed11.

  • MEASURED, cargo test --release -p leonida-shader: main 229 passed, 0 failed, 2 ignored; main + this PR 232 passed, 0 failed, 2 ignored. The 3 new tests are the_memo_keeps_the_newest_results_of_a_set_and_forgets_the_rest, the_memo_grows_to_its_limit_and_keeps_what_it_holds and a_memo_that_forgets_gives_the_nodes_one_that_keeps_all_gives (the last one checks the forgetting memo against one that keeps everything).
  • No red side: the new tests exercise the new memo type, so they cannot run against main's code.
  • Not covered here: the Windows-only test modules of the crate (#[cfg(all(test, windows))]), the output identity over the archive and the -12 % translate time (they need the game's shaders).

AI-assisted: Claude Code

**@⁠OreoFR** on GitHub · 2026-11-30 18:06 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2773#issuecomment-7287349240) Verified by OreoFR on macOS 27.2.1, Apple M5, Rust (rustup), `main` `ad36782c6` merged with head `6bbaeed11`. - MEASURED, `cargo test --release -p leonida-shader`: `main` 229 passed, 0 failed, 2 ignored; `main` + this PR 232 passed, 0 failed, 2 ignored. The 3 new tests are `the_memo_keeps_the_newest_results_of_a_set_and_forgets_the_rest`, `the_memo_grows_to_its_limit_and_keeps_what_it_holds` and `a_memo_that_forgets_gives_the_nodes_one_that_keeps_all_gives` (the last one checks the forgetting memo against one that keeps everything). - No red side: the new tests exercise the new memo type, so they cannot run against `main`'s code. - Not covered here: the Windows-only test modules of the crate (`#[cfg(all(test, windows))]`), the output identity over the archive and the -12 % translate time (they need the game's shaders). AI-assisted: Claude Code
Author
Owner

@⁠KelboLive on GitHub · 2026-11-30 18:19 UTC · original

Independent check on Windows (not the author), at 6bbaeed11.

  • cargo test --locked -p leonida-shader --lib (shared warm target): 388 passed, 0 failed, 5 ignored in 273.7 s.

The -12% translate-time claim needs a corpus run, which I did not do; the crate's structural tests are green with the memo table.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned.

AI-assisted: yes (opencode).

**@⁠KelboLive** on GitHub · 2026-11-30 18:19 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2773#issuecomment-7287668068) Independent check on Windows (not the author), at `6bbaeed11`. - `cargo test --locked -p leonida-shader --lib` (shared warm target): **388 passed, 0 failed, 5 ignored** in 273.7 s. The -12% translate-time claim needs a corpus run, which I did not do; the crate's structural tests are green with the memo table. Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned. AI-assisted: yes (opencode).
github-bot changed target branch from main to gh-pr/2773/base 2026-11-30 20:16:43 +00:00
github manually merged commit 6bbaeed111 into gh-pr/2773/base 2026-11-30 20:16:44 +00:00
Author
Owner

@⁠MoonEyes on GitHub · 2026-11-30 20:11 UTC · original

RUN · VERIFY-2773-RTX4060 · by MoonEyes (another person than the author) on head 6bbaeed11, with #2745 (88d0dbf9c), #2773 (6bbaeed11), #2726 (986771506) and #2780 (6c4474cfd) merged into main 51dbc5b58. Windows 11 Pro 26300, RTX 4060 8 GB, -SeriesS -FrameGen 2, diag build.

  • cargo test -p leonida-shader --locked: 400 passed; -p leonida-gpu: 580 passed; 0 failed.
  • Game: two runs of 480 s, the person walking in the world from 345 s. First (shaders translated again: 132 s, pipelines warmed: 533 s): steady after 98 s, about 500 s of it. Second, warm: steady after 107 s, about 495 s of it, about 51 m walked. In both no device loss, no unhandled exception, the process alive at the end. PASS for the gate of 300 s. The picture was not looked at.
  • Translation at a first start 132 s here, 177 s on main 8f41d7d01 with two other pull requests: n=1 on each side and not the same tree, so no speed-up is claimed. The four pull requests were in one build.

AI-assisted (Claude Code).

**@⁠MoonEyes** on GitHub · 2026-11-30 20:11 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2773#issuecomment-7290547696) RUN · VERIFY-2773-RTX4060 · by MoonEyes (another person than the author) on head `6bbaeed11`, with #2745 (`88d0dbf9c`), #2773 (`6bbaeed11`), #2726 (`986771506`) and #2780 (`6c4474cfd`) merged into `main` `51dbc5b58`. Windows 11 Pro 26300, RTX 4060 8 GB, `-SeriesS -FrameGen 2`, diag build. - `cargo test -p leonida-shader --locked`: 400 passed; `-p leonida-gpu`: 580 passed; 0 failed. - Game: two runs of 480 s, the person walking in the world from 345 s. First (shaders translated again: 132 s, pipelines warmed: 533 s): steady after 98 s, about 500 s of it. Second, warm: steady after 107 s, about 495 s of it, about 51 m walked. In both no device loss, no unhandled exception, the process alive at the end. PASS for the gate of 300 s. The picture was not looked at. - Translation at a first start 132 s here, 177 s on `main` `8f41d7d01` with two other pull requests: n=1 on each side and not the same tree, so no speed-up is claimed. The four pull requests were in one build. AI-assisted (Claude Code).
Author
Owner

@⁠MoonEyes on GitHub · 2026-11-30 20:12 UTC · original

Merging. Head 6bbaeed11 merges cleanly with main b7934269d and does not force a rebase of #2566. Two runs in the game (RTX 4060, about 500 s steady each, no device loss) with #2745, #2773, #2726 and #2780 together on this head; cargo test green; checked by other people. The shaders are translated again at the first start. Merged by MoonEyes.

AI-assisted (Claude Code).

**@⁠MoonEyes** on GitHub · 2026-11-30 20:12 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2773#issuecomment-7290567688) Merging. Head `6bbaeed11` merges cleanly with `main` `b7934269d` and does not force a rebase of #2566. Two runs in the game (RTX 4060, about 500 s steady each, no device loss) with #2745, #2773, #2726 and #2780 together on this head; cargo test green; checked by other people. The shaders are translated again at the first start. Merged by MoonEyes. AI-assisted (Claude Code).
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/ubiquitarianism--leonida!2773
No description provided.