Skip to content

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

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
TG0000:perf-pr5
Nov 25, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
TG0000:perf-pr5

Conversation

@TG0000

@TG0000 TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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. Translate compute selects with numeric lane masks #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

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 <noreply@anthropic.com>
@DaviTostes

Copy link
Copy Markdown

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 Build: Linux workspace, contributor setup and CI #112 for the victory-gpu E0425.

edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ghost ghost mentioned this pull request Nov 24, 2026

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants