Repository navigation
Shaders: walk a lane-mask diagram once in vars and given - #126
Conversation
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>
|
Tested
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
catminou4
left a comment
There was a problem hiding this comment.
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).
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::varskept the nodes and variables it had already seen inVecs and rancontainson them for every node it visited, so it was quadratic in the node count.givencalls it for every guard and branch condition infinish, andsupport(whichemit::lowerandtranslatecall once per guard) calls it too.Bools::givencalledrestrictonce 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
varsrecords what it has seen inSeen, 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.givenfirst gathers every decided atom with its value, thenrestrictreplaces 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 sameBoolas before.restrictreturns 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:varsgives 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,givenreturns 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)becomesy0&&…&&y5. It also checks that nothing decided leaves the function unchanged and that a false condition does not panic.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:varsbeforegivenbeforeDiagrams with a few nodes stay within a fraction of a microsecond of before (the small
valueslist ingiven). 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 instructure.rs. The warnings that remain are in files this PR does not touch.cargo +stable-x86_64-pc-windows-msvc xtask policy: ok.structure.rschanged, and rustfmt ran on that file alone. Translate compute selects with numeric lane masks #69 also editsstructure.rs, but in other places (theStructured/Lanesfields andstep), and this PR does not touch those lines.AI-assisted (Claude Code).
🤖 Generated with Claude Code