Repository navigation
D3D12-SHADER-BDD-UNIQUE-COMPACT: open-addressing unique table for the BDD nodes (-3 % wall, DXIL identical) - #2788
Conversation
`Bools::unique` was a HashMap<(u32, Bool, Bool), Bool>: 17 bytes a slot at 44-87 % load and 19-39 bytes per node, and 11 % of the CPU time of the 500 slowest programs (the diagrams there hold about 1 M nodes and do not fit the cache). Replace it with open addressing over node numbers: a 4-byte slot (21-bit node number, 11-bit tag), load at most 1/2, linear probing, equality checked on the node itself. Nodes are made in the same order, so the structuriser's output is the same. The new test makes 400 000 random nodes over 40 variables, with the table growing from 1 024 slots to at least 262 144, and checks every one against a HashMap: the same triple gives the same node, a new triple the next number, and a second pass asks for them all again and gets no new node. Skipping the equality check on the node makes it fail. AI-assisted: Claude Sonnet 5.5 Co-authored-by: Cursor <cursoragent@cursor.com>
|
Checked by OreoFR on macOS 27.2.1, Apple M5, Rust (rustup),
AI-assisted: Claude Code |
KelboLive
left a comment
There was a problem hiding this comment.
The table's invariants are exactly right. The key is the full triple (nodes[node] == triple, structure.rs:172), the 11-bit tag only filters probes, growth recomputes the hash and tag from the nodes (:185-195) so no stale entry can survive a resize, there is no deletion (no tombstones), and the <= 1/2 load with one grow per insert guarantees an empty slot, so find's probe terminates. assert!(MAX_NODES == 1 << 21) ties the slot layout to the node range.
Differential against origin/main's HashMap canonicaliser: 400,000 triples (354,189 fresh, 43,269 repeats, 10 growths from 1,024 to 1,048,576 slots) produced identical ids and an identical nodes vector; re-asking all 354,189 triples after the growth made no new node; and a forced same-slot + same-tag pair (v = 64 / v = 1590) stayed distinct and survived another resize.
The committed test has teeth for every truncation I tried: comparing only the variable fails at once (a false hit returned 11893 where 28021 was due), returning on the tag alone returns the wrong node, and a growth that re-inserts under a different hash loses an existing triple and re-makes it. So "a tag collision cannot return a wrong node" is pinned rather than asserted.
One test suggestion: the random sequence only hits a tag-plus-slot collision by chance. Searching Unique::hash for a pair with equal (hash & mask, hash >> 53) (a few thousand hashes) and forcing it makes that path deterministic, as I did above.
No bug found; the only cost is the fixed 4 KB START table per Bools, which is negligible.
(Build note: the shared CARGO_TARGET_DIR was held by another leonida-shader test binary — two agents, one of them mine — so this was built in a separate target dir; not a problem with the PR.)
|
Verified by MoonEyes (another person than the author) on head In-game run with the six others of a batch on AI-assisted (Claude Code). |
|
I tested the full production-sized unique table in the actual Bools::mk on a0066d4 + 0ff8a53, native Linux and Rust 1.98.1. A local fixture inserted distinct triples up to the exact 2,097,152-node limit, then looked up every real node in a permuted order. All node IDs matched main, with the same checksum 083735d185e46f6c. An attempt to insert one more distinct node set overflow, while the last existing node still resolved to ID 2,097,151. The focused structure suite also passed (20 tests including the local fixture). Five alternating solo invocations per arm gave median peak child RSS of about 128.1 MiB on main and 44.2 MiB with the PR. Median insertion was 55 ms versus 37 ms, and permuted lookup 21 ms versus 22 ms. Those are process-level measurements for this synthetic shape, separate from the end-to-end shader corpus figures. MEASURED with synthetic BDD inputs. AI-assisted (Codex). |
VERIFY-2788 — Linux, Fedora 45, Rust 1.98.1 (the pinned toolchain), no GPU device, no game filesVerdict: PASS. The new table gives the same answers as the Head 1. What it changes
/// A slot is four bytes: the node number in its low 21 bits and 11 bits of its triple's hash
/// above them, so that a probe past a slot of another triple does not read that node; 0 is an
/// empty slot (node 0 is a constant, in no triple). Never more than half full.
struct Unique {
slots: Vec<u32>,
mask: usize, // one less than the number of slots, a power of two
}The lookup is open addressing with linear probing; the tag is only a filter and the full triple comparison against the node's own entry is the truth: if slot >> SLOT_NODE_BITS == tag {
let node = slot & SLOT_NODE_MASK;
if nodes[node as usize] == triple {
return Ok(Bool(node));
}
}The table doubles when it is half full ( 2. The repository's own red/green verifier: "red does not build"The new test reads one field of the new structure: assert!(b.unique.slots.len() >= 1 << 18, "{}", b.unique.slots.len());so the red side (the base with only the pull request's tests) cannot compile — there is no That is a limitation of the test, not of the change: a test that cannot exist without the change cannot show that the change makes it pass. So I made the equivalent check myself. 3. The equivalence check I did instead: the same test on the old implementationI took the pull request's test, dropped the one line that reads the new internals, and ran it against the old The test makes 400,000 random triples and checks each against a That is the decisive result for a data-structure swap: the ground truth is the old implementation, not the game's data — and the two agree on the node number of every one of the 400,000 triples, including after the table has grown. The pull request's "DXIL identical" claim rides on exactly this: the node numbers are the same, so the diagram the structuriser builds is the same. 4. What the test does and does not discriminateI broke the table five ways and ran the test each time:
The last one is the only gap, and it is a performance invariant, not a correctness one: the table's cost is its probe length, and the test only pins the size from below ( The other four are the ones that matter, and the model catches each of them. 5. What I did not check
AI-assisted: MiniMax M3.1 Flash. |
|
Merging (added to the merge queue). Head a0066d4 merges cleanly with main c4ca576. Claim: D3D12-SHADER-BDD-UNIQUE-COMPACT (coordination issue #2570). Red/green: merzleckeier (full-size table, same node IDs and checksum as main), imShangLi (Linux, PASS, catches four of five mutations). I ran: #2858, #2882, #2788, #2876 and #2884 merged together onto main c4ca576: cargo tests leonida-shader 420 passed / 0 failed, leonida-loader 85/0, leonida-gpu 644/0. In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, -SeriesS -NoDlss -NoFrameGen, a cold start to the world (135 s) then a 330 s warm walking run: median 35.4 ms (28.2 fps), private memory max 24.4 GB, 0 device lost. CI: green on head and main; the queue runs the full CI on the group before merging. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR. |
|
Confirmed on the title's own archive, and isolated to this change. Method: The twenty other commits in that batch are worth about a second (48.1/47.4 -> 46.9/46.9); this one is about two (46.9 -> 44.85), 4.3%, and it is the only commit between those two builds that touches the translator. The spread inside a build is 0.7 s at most, so the difference is outside it. Two things it does not say:
What it means for a start: a smaller share of the one CPU-bound phase (the shader cache build measured 1.2 min with 16 workers on this machine), not a frame time. AI-assisted: yes - DeepSeek v4.1 (flash). Written with an AI assistant. |
What
Bools(the BDD behind the lane-mask structuriser inleonida-shader/src/structure.rs) keeps its unique table in aHashMap<(u32, Bool, Bool), Bool>. That is about 17 bytes a slot at 44-87 % load, 19-39 bytes per node, and it was 11 % of the CPU time of the 500 slowest programs (insert_no_grow8.8 % plusreserve_rehash2.3 % of their samples). Those programs are the ones whose CPU time scales worst with threads (2.38x CPU time at 16 threads against 1.48x for the median program), because their diagrams (about 1 M nodes, 46 % live at the end) do not fit the cache.This replaces the map with open addressing over node numbers: a 4-byte slot (21-bit node number plus an 11-bit tag), load at most 1/2, linear probing, and equality checked on the node itself (
nodes[node] == triple), so a tag collision cannot return a wrong node. Nodes are made in the same order as before, so nothing downstream sees a difference.MAX_NODESstays1 << 21and a compile-time assert ties it to the slot layout.Claim: D3D12-SHADER-BDD-UNIQUE-COMPACT (coordination issue #2570). Touches
Bools::new,Bools::mkand one test; no overlap with an open pull request (#2750 is in other files of the same crate).Measured
Full-archive
leonida translateon the retail shader archive, release build withdiagnostics,mimalloc, 16 threads, Ryzen 7 7800X3D, 3 alternating runs of each,mainat d8ca182 as the baseline:That is -3.1 % wall and -1.9 % CPU on mean values. On the 500 slowest programs alone (same method, 3 runs each): wall 15.2 / 14.8 / 14.8 s -> 13.9 / 13.8 / 13.8 s (-7.4 %), user CPU 201.5 / 199.9 / 200.8 s -> 189.4 / 189.4 / 188.1 s (-5.8 %).
DXIL output is identical. The blake3 of the DXIL of all 25689 programs (15799 distinct) is the same as
main's in every run, full archive and heavy set (a scratch patch that writes the hashes; it is not part of this pull request).Tests
the_unique_table_gives_a_triple_its_node_again_through_every_growth: 400 000 random nodes over 40 variables while the table grows from 1 024 slots to at least 262 144, each checked against aHashMap: the same triple gives the same node, a new triple the next number,nodes[got] == triple, then every triple is asked again and no node is made, and a triple over a variable nobody used is new. Negative control: replacing thenodes[node] == triplecomparison withtruemakes it fail.cargo test -p leonida-shader --release: 401 passed, 0 failed, 5 ignored.rustfmt --edition 2024 --checkonstructure.rs: the nine diffs it reports were there before and are not in this change; clippy reports nothing in it.check_no_game_data.pyon the changed file: clean.AI-assisted: Claude Sonnet 5.5