From b03c9789536597ded536d347b1c7d50b57e71f8d Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Fri, 4 Dec 2026 09:29:45 +0000 Subject: [PATCH 1/2] miniBDD: a helper that combines hashes The hash of the unique table's keys mixes var, low and high by multiplying the value so far by 0x9e3779b1 and adding the next component. Move this step into a helper, so that further hash tables can use the same scheme and the explanation of the constant lives in one place. The hash values are unchanged. Co-authored-by: Kiro --- src/solvers/bdd/miniBDD/miniBDD.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/solvers/bdd/miniBDD/miniBDD.cpp b/src/solvers/bdd/miniBDD/miniBDD.cpp index bd7c3faf268..ea211c5a9ab 100644 --- a/src/solvers/bdd/miniBDD/miniBDD.cpp +++ b/src/solvers/bdd/miniBDD/miniBDD.cpp @@ -17,6 +17,16 @@ Author: Daniel Kroening, kroening@kroening.com #include +/// Combine \p value into the hash \p seed of the values so far: multiply +/// the seed by an odd constant, then add the next value. 0x9e3779b1 +/// (2654435761) is the prime closest to 2^32 / phi, a common multiplier in +/// multiplicative hashing (Knuth, TAOCP vol. 3, section 6.4). +/// \return The combined hash +static std::size_t combine_hash(std::size_t seed, unsigned value) +{ + return seed * 0x9e3779b1 + value; +} + void mini_bdd_nodet::remove_reference() { PRECONDITION_WITH_DIAGNOSTICS( @@ -475,14 +485,7 @@ bool mini_bdd_mgrt::reverse_keyt::operator==( std::size_t mini_bdd_mgrt::reverse_key_hasht::operator()( const mini_bdd_mgrt::reverse_keyt &key) const { - // Mix var, low and high into one value: multiply the value so far by an - // odd constant, then add the next component. 0x9e3779b1 (2654435761) is - // the prime closest to 2^32 / phi, a common multiplier in multiplicative - // hashing (Knuth, TAOCP vol. 3, section 6.4). - std::size_t h = key.var; - h = h * 0x9e3779b1 + key.low; - h = h * 0x9e3779b1 + key.high; - return h; + return combine_hash(combine_hash(key.var, key.low), key.high); } void mini_bdd_mgrt::DumpTable(std::ostream &out) const From a101973d4b9048dafdd438877ffa3f978b4e7846 Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Fri, 4 Dec 2026 09:33:19 +0000 Subject: [PATCH 2/2] miniBDD: a hash table for the computed table of apply 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 --- src/solvers/bdd/miniBDD/miniBDD.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/solvers/bdd/miniBDD/miniBDD.cpp b/src/solvers/bdd/miniBDD/miniBDD.cpp index ea211c5a9ab..7557edd84bc 100644 --- a/src/solvers/bdd/miniBDD/miniBDD.cpp +++ b/src/solvers/bdd/miniBDD/miniBDD.cpp @@ -208,7 +208,20 @@ class mini_bdd_applyt mini_bddt APP_rec(const mini_bddt &x, const mini_bddt &y); mini_bddt APP_non_rec(const mini_bddt &x, const mini_bddt &y); - typedef std::map, mini_bddt> Gt; + /// Hash of a pair of node numbers, the key of the computed table + struct node_pair_hasht + { + std::size_t operator()(const std::pair &key) const + { + return combine_hash(key.first, key.second); + } + }; + + // Computed table: the result for each pair of node numbers. It is + // consulted at every step, so it is a hash table rather than a tree. + typedef std:: + unordered_map, mini_bddt, node_pair_hasht> + Gt; Gt G; };