Skip to content

miniBDD: use a hash table for the unique table - #9182

Merged
tautschnig merged 3 commits into
diffblue:developfrom
kroening:kroening/minibdd-unordered-unique-table
Dec 4, 2026
Merged

tautschnig merged 3 commits into
diffblue:developfrom
kroening:kroening/minibdd-unordered-unique-table

Conversation

@kroening

Copy link
Copy Markdown
Collaborator

Changes

mini_bdd_mgrt::mk consults the unique table (reverse_map) for every
candidate node before creating it, so this lookup is on the hot path of every
BDD operation. It was a std::map (a balanced tree), i.e. O(log n)
pointer-chasing comparisons per node.

This switches the unique table to std::unordered_map, with a hash over
(var, low, high). reverse_keyt gains operator== and a hash functor; its
now-unused operator< is removed.

Motivation

Profiling the ebmc --bdd model checker (on top of the early variable
quantification of diffblue/hw-cbmc#1801) showed mini_bdd_mgrt::mk accounting
for roughly a third of the run time, dominated by the std::map tree
traversal. The unique table is a hash table in essentially every production
BDD package for this reason.

Performance

ebmc --bdd on the 50 smallest HWMCC08 AIGER instances, on top of
diffblue/hw-cbmc#1801, 20s timeout per instance, Apple Silicon. Two builds
that differ only by this commit in the cbmc submodule:

Baseline This PR
Time on the 29 instances solved by both 22.83s 15.79s (1.45×)
Solved (of 50) 29 30
Answers disagreeing with ABC reference 0 0

All answers agree with the ABC reference column in hwmcc08results.csv.
Representative per-instance improvements: visarbiter 5.34s → 3.34s,
eijkS208o 4.95s → 3.70s, pdtvisminmax0 2.75s → 1.87s.

Testing

unit "[miniBDD]" and unit "[solver]" pass. The change does not alter BDD
semantics (the unique table is a lookup structure only); the HWMCC08
cross-check against the ABC reference shows no change in results.

@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.35%. Comparing base (cce28f5) to head (1a44d84).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #9182   +/-   ##
========================================
  Coverage    81.34%   81.35%           
========================================
  Files         1727     1727           
  Lines       191715   191752   +37     
  Branches        73       73           
========================================
+ Hits        155950   155997   +47     
+ Misses       35765    35755   -10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kroening
kroening force-pushed the kroening/minibdd-unordered-unique-table branch from a94f8b2 to a955fb8 Compare November 28, 2026 20:30
kroening and others added 3 commits December 3, 2026 22:59
mini_bdd_mgrt::mk looks up every candidate node in the unique table
(reverse_map) before creating it, so this lookup is on the hot path of
every BDD operation. It was a std::map (a balanced tree), giving
O(log n) pointer-chasing comparisons per node.

Switch it to std::unordered_map with a hash over (var, low, high).
Profiling the ebmc BDD model checker showed mini_bdd_mgrt::mk accounting
for about a third of the run time, dominated by the tree traversal.

On the 50 smallest HWMCC08 instances (ebmc --bdd, on top of the early
variable quantification of diffblue/hw-cbmc#1801, 20s timeout), this is
a 1.45x speedup on the instances solved by both, with no change in
results (all still agree with the ABC reference).
The comment said that 0x9e3779b1 is the 32-bit approximation of
2^32 / phi and that the constant and the scheme are those of Boost's
hash_combine and of Knuth's multiplicative hashing. 2^32 / phi rounds to
0x9e3779b9 (2654435769); 0x9e3779b1 (2654435761) is the prime closest to
it. Boost's hash_combine (of which src/goto-programs/graphml_witness.h has
a copy) uses 0x9e3779b9 with shifts and exclusive or, and Knuth's
multiplicative hashing keeps the high bits of the product, while here
std::unordered_map gets the whole value. The constant itself is fine.

Also document reverse_key_hasht with a Doxygen comment.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
The existing miniBDD scenarios reach the unique table only through the
Boolean operators, and none checks that released nodes are freed:
removing the push onto the free list in mini_bdd_nodet::remove_reference
leaves them passing. The new scenario checks that equivalent functions
share a node over several rounds that reuse released nodes, that the
number of nodes returns to its baseline once all results are released,
that mk returns the existing node for the same arguments, and that a
reused node is found under its new arguments only, and evaluates the
results. It fails when remove_reference does not free the node, does not
erase its key, or when mk does not enter a reused node, and passes both
with the hash table and with the std::map that it replaced.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig
tautschnig force-pushed the kroening/minibdd-unordered-unique-table branch from a955fb8 to 1a44d84 Compare December 3, 2026 23:14
@tautschnig
tautschnig enabled auto-merge December 4, 2026 00:10
@tautschnig
tautschnig merged commit aef1c2b into diffblue:develop Dec 4, 2026
92 of 100 checks passed
kroening pushed a commit that referenced this pull request Dec 4, 2026
mini_bdd_applyt consults its computed table, a std::map from pairs of
node numbers to results, at every step. Once the unique table became a
hash table (#9182), the lookups in this tree were the largest part of
the run time of random BDD operations (about a quarter, with perf). Make
it a std::unordered_map, hashing the pair with combine_hash.

Measured with a stand-alone driver that applies random and, or, xor,
not, equivalence, exists and restrict operations to a pool of BDDs,
three seeds each (x86-64, g++ 13.3 -O2), before -> after:
- 8 variables, 1,000,000 operations: 4.03-4.08 s -> 3.59-3.64 s (1.12x)
- 12 variables, 200,000 operations: 26.4-29.2 s -> 21.5-23.5 s (1.24x)
- 16 variables, 20,000 operations: 43.7-47.7 s -> 37.2-40.8 s (1.17x)
- 4 variables, 2,000,000 operations: 1.23 s -> 1.27-1.28 s (3% slower):
  each operation creates its own computed table, and allocating the
  buckets of a hash table costs more than it saves for a handful of
  entries.
The node numbers of all results and the node counts are the same before
and after, and the driver is clean under ASan and UBSan.

The computed table of restrict, a std::map keyed by node number, is
consulted far less often: a hash table there made no measurable
difference on these workloads, and was 3% slower on the smallest one,
so it stays a tree.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants