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

Manually merged
github merged 1 commit from gh-pr/126/head into gh-pr/126/base 2026-11-26 03:51:15 +00:00
Owner

@⁠TG0000 on GitHub · 2026-11-24 00:43 UTC · original
TG0000:perf-pr5 → main · +283 −19 · 1 files · 1 commits · merged 2026-11-25 00:16 UTC

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

**@⁠TG0000** on GitHub · 2026-11-24 00:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/126) `TG0000:perf-pr5` → `main` · +283 −19 · 1 files · 1 commits · merged 2026-11-25 00:16 UTC `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)
Bools::vars kept the nodes and variables it had seen in lists and scanned
them for every node, quadratic in the diagram's size; it now uses a set
that scans a short list while it is small and hashes once it grows.
Bools::given cofactored the diagram once per decided atom, each walk with
a fresh table; cofactors of different variables commute, so it now
replaces every decided atom in one walk. Equal functions are equal nodes,
so the results are the same nodes as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:51:13 +00:00
github manually merged commit e5b7159832 into gh-pr/126/base 2026-11-26 03:51:15 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:10 UTC · original

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.
**@⁠DaviTostes** on GitHub · 2026-11-24 01:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/126#issuecomment-7118825144) 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.
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/Marvisak--VIctory!126
No description provided.