Shaders: walk a lane-mask diagram once in vars and given #126
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!126
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/126/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCstructure(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. #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
@DaviTostes on GitHub · 2026-11-24 01:10 UTC · original
Tested
e5b7159on 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.structure::tests::bench_bools(benchmark).victory-gpuE0425.