Shaders: walk a lane-mask diagram once in vars and given #126

Merged
TG0000 merged 1 commit from upstream-pr/126 into main 2026-11-25 00:16:00 +00:00
TG0000 commented 2026-11-24 00:43:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TG0000 on 2026-11-24T00:43:00Z.


structure (crates/victory-shader/src/structure.rs) keeps lane masks as binary decision diagrams (Bools). Two of its walks over a diagram cost more than the diagram's size:

  • Bools::vars kept the nodes and variables it had already seen in Vecs and ran contains on them for every node it visited, so it was quadratic in the node count. given calls it for every guard and branch condition in finish, and support (which emit::lower and translate call once per guard) calls it too.
  • Bools::given called restrict once for each atom the known condition decides. Each call walked the diagram again with a new memo table, so the cost was the diagram's size times the number of decided atoms.

Change

  • vars records what it has seen in Seen, a small set type that scans a list of up to 32 entries and moves to an Fx hash set when it outgrows that. Most masks have only a few nodes, so they stay on the list path. The walk is the same depth-first walk as before, so the variables come back in the same order.
  • given first gathers every decided atom with its value, then restrict replaces all of them in a single memoised walk. Cofactors over different variables commute, so the result is the same function as before. Equal functions share a node in a reduced diagram, so the result is also the same Bool as before. restrict returns early under the last decided variable, and it skips the memo table while the walk is still a single path through decided nodes (that path cannot revisit a node), so the trivial cases allocate nothing.

Tests (synthetic only, no game data)

  • vars_match_the_scan: vars gives the same list, in the same order, as the old list-scanning walk (kept in the tests as the reference). It runs on the constants, a single atom, an exponential "pairs equal" diagram and 40 seeded mixes of and, or and xor.
  • given_matches_a_walk_per_variable: on 60 seeded functions and conditions, given returns the same node as the old walk per atom. The test also checks every one of the 4,096 assignments: wherever the condition holds, the result agrees with the original function.
  • given_replaces_every_decided_atom: with x0..x5 known true, (x0==y0)&&…&&(x5==y5) becomes y0&&…&&y5. It also checks that nothing decided leaves the function unchanged and that a false condition does not panic.
  • The existing structurisation tests still pass.

Benchmark

cargo test --release -p victory-shader bench_bools -- --ignored --nocapture. The input is (x_i == y_i) for all i, with every x ordered before every y, and the condition sets half the x's true. The times are per call, averaged over at least 50 ms of calls:

n nodes vars before after speedup given before after speedup
2 9 0.55 µs 0.63 µs 0.9× 0.91 µs 1.19 µs 0.8×
4 45 2.48 µs 1.94 µs 1.3× 1.71 µs 2.17 µs 0.8×
8 765 161 µs 17.6 µs 9× 123 µs 19.1 µs 6×
10 3,069 1.83 ms 61 µs 30× 1.73 ms 63 µs 27×
12 12,285 28.6 ms 272 µs 105× 29.7 ms 265 µs 112×
13 24,573 139 ms 743 µs 188× 118 ms 670 µs 176×

Diagrams with a few nodes stay within a fraction of a microsecond of before (the small values list in given). From a few hundred nodes on, the speedup grows with the size: the walks are now linear in the node count instead of quadratic.

Checked

  • cargo +stable-x86_64-pc-windows-msvc test --workspace: all pass.
  • cargo +stable-x86_64-pc-windows-msvc clippy --workspace --all-targets: no warnings in structure.rs. The warnings that remain are in files this PR does not touch.
  • cargo +stable-x86_64-pc-windows-msvc xtask policy: ok.
  • Only structure.rs changed, and rustfmt ran on that file alone. #69 also edits structure.rs, but in other places (the Structured/Lanes fields and step), and this PR does not touch those lines.
  • Not run against the retail game, so the time saved per shader in the title is not measured. The benchmark above is synthetic. Tested on Windows 11 with the MSVC stable toolchain; the change runs on the CPU only.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6571960285 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/126). Originally posted by [TG0000](https://github.com/TG0000) on 2026-11-24T00:43:00Z. --- `structure` (`crates/victory-shader/src/structure.rs`) keeps lane masks as binary decision diagrams (`Bools`). Two of its walks over a diagram cost more than the diagram's size: - `Bools::vars` kept the nodes and variables it had already seen in `Vec`s and ran `contains` on them for every node it visited, so it was quadratic in the node count. `given` calls it for every guard and branch condition in `finish`, and `support` (which `emit::lower` and `translate` call once per guard) calls it too. - `Bools::given` called `restrict` once for each atom the known condition decides. Each call walked the diagram again with a new memo table, so the cost was the diagram's size times the number of decided atoms. ## Change - `vars` records what it has seen in `Seen`, a small set type that scans a list of up to 32 entries and moves to an Fx hash set when it outgrows that. Most masks have only a few nodes, so they stay on the list path. The walk is the same depth-first walk as before, so the variables come back in the same order. - `given` first gathers every decided atom with its value, then `restrict` replaces all of them in a single memoised walk. Cofactors over different variables commute, so the result is the same function as before. Equal functions share a node in a reduced diagram, so the result is also the same `Bool` as before. `restrict` returns early under the last decided variable, and it skips the memo table while the walk is still a single path through decided nodes (that path cannot revisit a node), so the trivial cases allocate nothing. ## Tests (synthetic only, no game data) - `vars_match_the_scan`: `vars` gives the same list, in the same order, as the old list-scanning walk (kept in the tests as the reference). It runs on the constants, a single atom, an exponential "pairs equal" diagram and 40 seeded mixes of and, or and xor. - `given_matches_a_walk_per_variable`: on 60 seeded functions and conditions, `given` returns the same node as the old walk per atom. The test also checks every one of the 4,096 assignments: wherever the condition holds, the result agrees with the original function. - `given_replaces_every_decided_atom`: with x0..x5 known true, `(x0==y0)&&…&&(x5==y5)` becomes `y0&&…&&y5`. It also checks that nothing decided leaves the function unchanged and that a false condition does not panic. - The existing structurisation tests still pass. ## Benchmark `cargo test --release -p victory-shader bench_bools -- --ignored --nocapture`. The input is `(x_i == y_i)` for all i, with every x ordered before every y, and the condition sets half the x's true. The times are per call, averaged over at least 50 ms of calls: | n | nodes | `vars` before | after | speedup | `given` before | after | speedup | |---:|---:|---:|---:|---:|---:|---:|---:| | 2 | 9 | 0.55 µs | 0.63 µs | 0.9× | 0.91 µs | 1.19 µs | 0.8× | | 4 | 45 | 2.48 µs | 1.94 µs | 1.3× | 1.71 µs | 2.17 µs | 0.8× | | 8 | 765 | 161 µs | 17.6 µs | 9× | 123 µs | 19.1 µs | 6× | | 10 | 3,069 | 1.83 ms | 61 µs | 30× | 1.73 ms | 63 µs | 27× | | 12 | 12,285 | 28.6 ms | 272 µs | 105× | 29.7 ms | 265 µs | 112× | | 13 | 24,573 | 139 ms | 743 µs | 188× | 118 ms | 670 µs | 176× | Diagrams with a few nodes stay within a fraction of a microsecond of before (the small `values` list in `given`). From a few hundred nodes on, the speedup grows with the size: the walks are now linear in the node count instead of quadratic. ## Checked - `cargo +stable-x86_64-pc-windows-msvc test --workspace`: all pass. - `cargo +stable-x86_64-pc-windows-msvc clippy --workspace --all-targets`: no warnings in `structure.rs`. The warnings that remain are in files this PR does not touch. - `cargo +stable-x86_64-pc-windows-msvc xtask policy`: ok. - Only `structure.rs` changed, and rustfmt ran on that file alone. #69 also edits `structure.rs`, but in other places (the `Structured`/`Lanes` fields and `step`), and this PR does not touch those lines. - Not run against the retail game, so the time saved per shader in the title is not measured. The benchmark above is synthetic. Tested on Windows 11 with the MSVC stable toolchain; the change runs on the CPU only. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DaviTostes commented 2026-11-24 06:54:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:10:05Z.


Tested e5b7159 on Arch Linux x86_64, rustc 1.98.1: pass.

  • cargo test -p victory-shader: 54 passed, 0 failed, 1 ignored (2 test binaries), including the 4 tests this PR adds.
  • The ignored test is structure::tests::bench_bools (benchmark).
  • The full workspace still needs #112 for the victory-gpu E0425.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7118825144 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/126#issuecomment-7118825144). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T01:10:05Z. --- Tested `e5b7159` on **Arch Linux x86_64, rustc 1.98.1**: **pass**. - `cargo test -p victory-shader`: **54 passed, 0 failed, 1 ignored** (2 test binaries), including the 4 tests this PR adds. - The ignored test is `structure::tests::bench_bools` (benchmark). - The full workspace still needs #112 for the `victory-gpu` E0425.
catminou4 commented 2026-11-24 09:13:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T09:13:22Z.

Review: APPROVED


Merges cleanly with current main (8e79c80); cargo test -p victory-shader --locked on macOS arm64 / rustc 1.98.1 → 73 passed / 0 failed (2 ignored: the benchmark and the Windows execution suite).

This is a careful BDD refactor: vars and given/restrict go from quadratic list scans / one-cofactor-walk-per-atom to single walks with a Seen set and one multi-variable restrict. The tests keep the old implementations (vars_by_scan, restrict_one, given_by_walks) as reference and assert equality on randomized diagrams, plus exhaustive 2^12 evaluation checks and canonical-form id equality — exactly the right way to prove a refactor. The memo-skip on pure decision paths is sound (memo is per-restrict-call, fixed values).

<!-- greyfinch-sync:Marvisak/VIctory:review:6036480664 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/126#pullrequestreview-6036480664). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T09:13:22Z. **Review:** APPROVED --- Merges cleanly with current main (`8e79c80`); `cargo test -p victory-shader --locked` on macOS arm64 / rustc 1.98.1 → **73 passed / 0 failed** (2 ignored: the benchmark and the Windows execution suite). This is a careful BDD refactor: `vars` and `given`/`restrict` go from quadratic list scans / one-cofactor-walk-per-atom to single walks with a `Seen` set and one multi-variable `restrict`. The tests keep the old implementations (`vars_by_scan`, `restrict_one`, `given_by_walks`) as reference and assert equality on randomized diagrams, plus exhaustive 2^12 evaluation checks and canonical-form id equality — exactly the right way to prove a refactor. The memo-skip on pure decision paths is sound (memo is per-`restrict`-call, fixed `values`).
upstream-sync closed this pull request 2026-11-25 01:02:44 +00:00
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!126
No description provided.