Commit 0f6d8a3
committed
Merge bitcoin/bitcoin#30442: precalculate SipHash constant salt XORs
6eb5ba5 refactor: extract shared `SipHash` state into `SipHashState` (Lőrinc)
118d22d optimization: cache `PresaltedSipHasher` in `CBlockHeaderAndShortTxIDs` (Lőrinc)
9ca52a4 optimization: migrate `SipHashUint256` to `PresaltedSipHasher` (Lőrinc)
ec11b9f optimization: introduce `PresaltedSipHasher` for repeated hashing (Lőrinc)
2033054 refactor: extract `SipHash` C0-C3 constants to class scope (Lőrinc)
9f9eb7f test: rename k1/k2 to k0/k1 in `SipHash` consistency tests (Lőrinc)
Pull request description:
This change is part of [[IBD] - Tracking PR for speeding up Initial Block Download](bitcoin/bitcoin#32043)
### Summary
The in-memory representation of the UTXO set uses (salted) [SipHash](https://github.com/bitcoin/bitcoin/blob/master/src/coins.h#L226) to avoid key collision attacks.
Hashing `uint256` keys is performed frequently throughout the codebase. Previously, specialized optimizations existed as standalone functions (`SipHashUint256` and `SipHashUint256Extra`), but the constant salting operations (C0-C3 XOR with keys) were recomputed on every call.
This PR introduces `PresaltedSipHasher`, a class that caches the initial SipHash state (v0-v3 after XORing constants with keys), eliminating redundant constant computations when hashing multiple values with the same keys. The optimization is applied uniformly across:
- All `Salted*Hasher` classes (`SaltedUint256Hasher`, `SaltedTxidHasher`, `SaltedWtxidHasher`, `SaltedOutpointHasher`)
- `CBlockHeaderAndShortTxIDs` for compact block short ID computation
### Details
The change replaces the standalone `SipHashUint256` and `SipHashUint256Extra` functions with `PresaltedSipHasher` class methods that cache the constant-salted state. This is particularly beneficial for hash map operations where the same salt is used repeatedly (as suggested by Sipa in bitcoin/bitcoin#30442 (comment)).
`CSipHasher` behavior remains unchanged; only the specialized `uint256` paths and callers now reuse the cached state instead of recomputing it.
### Measurements
Benchmarks were run using local `SaltedOutpointHasherBench_*` microbenchmarks (not included in this PR) that exercise `SaltedOutpointHasher` in realistic `std::unordered_set` scenarios.
<details>
<summary>Benchmarks</summary>
```C++
diff --git a/src/bench/crypto_hash.cpp b/src/bench/crypto_hash.cpp
--- a/src/bench/crypto_hash.cpp(revision 9b1a7c3)
+++ b/src/bench/crypto_hash.cpp(revision e1b4f056b3097e7e34b0eda31f57826d81c9d810)
@@ -2,7 +2,6 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-
#include <bench/bench.h>
#include <crypto/muhash.h>
#include <crypto/ripemd160.h>
@@ -12,9 +11,11 @@
#include <crypto/sha512.h>
#include <crypto/siphash.h>
#include <random.h>
-#include <span.h>
#include <tinyformat.h>
#include <uint256.h>
+#include <primitives/transaction.h>
+#include <util/hasher.h>
+#include <unordered_set>
#include <cstdint>
#include <vector>
@@ -205,6 +206,98 @@
});
}
+static void SaltedOutpointHasherBench_hash(benchmark::Bench& bench)
+{
+ FastRandomContext rng{/*fDeterministic=*/true};
+ constexpr size_t size{1000};
+
+ std::vector<COutPoint> outpoints(size);
+ for (auto& outpoint : outpoints) {
+ outpoint = {Txid::FromUint256(rng.rand256()), rng.rand32()};
+ }
+
+ const SaltedOutpointHasher hasher;
+ bench.batch(size).run([&] {
+ size_t result{0};
+ for (const auto& outpoint : outpoints) {
+ result ^= hasher(outpoint);
+ }
+ ankerl::nanobench::doNotOptimizeAway(result);
+ });
+}
+
+static void SaltedOutpointHasherBench_match(benchmark::Bench& bench)
+{
+ FastRandomContext rng{/*fDeterministic=*/true};
+ constexpr size_t size{1000};
+
+ std::unordered_set<COutPoint, SaltedOutpointHasher> values;
+ std::vector<COutPoint> value_vector;
+ values.reserve(size);
+ value_vector.reserve(size);
+
+ for (size_t i{0}; i < size; ++i) {
+ COutPoint outpoint{Txid::FromUint256(rng.rand256()), rng.rand32()};
+ values.emplace(outpoint);
+ value_vector.push_back(outpoint);
+ assert(values.contains(outpoint));
+ }
+
+ bench.batch(size).run([&] {
+ bool result{true};
+ for (const auto& outpoint : value_vector) {
+ result ^= values.contains(outpoint);
+ }
+ ankerl::nanobench::doNotOptimizeAway(result);
+ });
+}
+
+static void SaltedOutpointHasherBench_mismatch(benchmark::Bench& bench)
+{
+ FastRandomContext rng{/*fDeterministic=*/true};
+ constexpr size_t size{1000};
+
+ std::unordered_set<COutPoint, SaltedOutpointHasher> values;
+ std::vector<COutPoint> missing_value_vector;
+ values.reserve(size);
+ missing_value_vector.reserve(size);
+
+ for (size_t i{0}; i < size; ++i) {
+ values.emplace(Txid::FromUint256(rng.rand256()), rng.rand32());
+ COutPoint missing_outpoint{Txid::FromUint256(rng.rand256()), rng.rand32()};
+ missing_value_vector.push_back(missing_outpoint);
+ assert(!values.contains(missing_outpoint));
+ }
+
+ bench.batch(size).run([&] {
+ bool result{false};
+ for (const auto& outpoint : missing_value_vector) {
+ result ^= values.contains(outpoint);
+ }
+ ankerl::nanobench::doNotOptimizeAway(result);
+ });
+}
+
+static void SaltedOutpointHasherBench_create_set(benchmark::Bench& bench)
+{
+ FastRandomContext rng{/*fDeterministic=*/true};
+ constexpr size_t size{1000};
+
+ std::vector<COutPoint> outpoints(size);
+ for (auto& outpoint : outpoints) {
+ outpoint = {Txid::FromUint256(rng.rand256()), rng.rand32()};
+ }
+
+ bench.batch(size).run([&] {
+ std::unordered_set<COutPoint, SaltedOutpointHasher> set;
+ set.reserve(size);
+ for (const auto& outpoint : outpoints) {
+ set.emplace(outpoint);
+ }
+ ankerl::nanobench::doNotOptimizeAway(set.size());
+ });
+}
+
static void MuHash(benchmark::Bench& bench)
{
MuHash3072 acc;
@@ -276,6 +369,10 @@
BENCHMARK(SHA256_32b_AVX2, benchmark::PriorityLevel::HIGH);
BENCHMARK(SHA256_32b_SHANI, benchmark::PriorityLevel::HIGH);
BENCHMARK(SipHash_32b, benchmark::PriorityLevel::HIGH);
+BENCHMARK(SaltedOutpointHasherBench_hash, benchmark::PriorityLevel::HIGH);
+BENCHMARK(SaltedOutpointHasherBench_match, benchmark::PriorityLevel::HIGH);
+BENCHMARK(SaltedOutpointHasherBench_mismatch, benchmark::PriorityLevel::HIGH);
+BENCHMARK(SaltedOutpointHasherBench_create_set, benchmark::PriorityLevel::HIGH);
BENCHMARK(SHA256D64_1024_STANDARD, benchmark::PriorityLevel::HIGH);
BENCHMARK(SHA256D64_1024_SSE4, benchmark::PriorityLevel::HIGH);
BENCHMARK(SHA256D64_1024_AVX2, benchmark::PriorityLevel::HIGH);
```
</details>
> cmake -B build -DBUILD_BENCH=ON -DCMAKE_BUILD_TYPE=Release && cmake --build build -j$(nproc) && build/bin/bench_bitcoin -filter='SaltedOutpointHasherBench' -min-time=10000
> Before:
| ns/op | op/s | err% | total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
| 58.60 | 17,065,922.04 | 0.3% | 11.02 | `SaltedOutpointHasherBench_create_set`
| 11.97 | 83,576,684.83 | 0.1% | 11.01 | `SaltedOutpointHasherBench_hash`
| 14.50 | 68,985,850.12 | 0.3% | 10.96 | `SaltedOutpointHasherBench_match`
| 13.90 | 71,942,033.47 | 0.4% | 11.03 | `SaltedOutpointHasherBench_mismatch`
> After:
| ns/op | op/s | err% | total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
| 57.27 | 17,462,299.19 | 0.1% | 11.02 | `SaltedOutpointHasherBench_create_set`
| 11.24 | 88,997,888.48 | 0.3% | 11.04 | `SaltedOutpointHasherBench_hash`
| 13.91 | 71,902,014.20 | 0.2% | 11.01 | `SaltedOutpointHasherBench_match`
| 13.29 | 75,230,390.31 | 0.1% | 11.00 | `SaltedOutpointHasherBench_mismatch`
compared to master:
```python
create_set - 17,462,299.19 / 17,065,922.04 - 2.3% faster
hash - 88,997,888.48 / 83,576,684.83 - 6.4% faster
match - 71,902,014.20 / 68,985,850.12 - 4.2% faster
mismatch - 75,230,390.31 / 71,942,033.47 - 4.5% faster
```
> C++ compiler .......................... GNU 13.3.0
> Before:
| ns/op | op/s | err% | ins/op | cyc/op | IPC | bra/op | miss% | total | benchmark
|--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:----------
| 136.76 | 7,312,133.16 | 0.0% | 1,086.67 | 491.12 | 2.213 | 119.54 | 1.1% | 11.01 | `SaltedOutpointHasherBench_create_set`
| 23.82 | 41,978,882.62 | 0.0% | 252.01 | 85.57 | 2.945 | 4.00 | 0.0% | 11.00 | `SaltedOutpointHasherBench_hash`
| 60.42 | 16,549,695.42 | 0.1% | 460.51 | 217.04 | 2.122 | 21.00 | 1.4% | 10.99 | `SaltedOutpointHasherBench_match`
| 78.66 | 12,713,595.35 | 0.1% | 555.59 | 282.52 | 1.967 | 20.19 | 2.2% | 10.74 | `SaltedOutpointHasherBench_mismatch`
> After:
| ns/op | op/s | err% | ins/op | cyc/op | IPC | bra/op | miss% | total | benchmark
|--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:----------
| 135.38 | 7,386,349.49 | 0.0% | 1,078.19 | 486.16 | 2.218 | 119.56 | 1.1% | 11.00 | `SaltedOutpointHasherBench_create_set`
| 23.67 | 42,254,558.08 | 0.0% | 247.01 | 85.01 | 2.906 | 4.00 | 0.0% | 11.00 | `SaltedOutpointHasherBench_hash`
| 58.95 | 16,962,220.14 | 0.1% | 446.55 | 211.74 | 2.109 | 20.86 | 1.4% | 11.01 | `SaltedOutpointHasherBench_match`
| 76.98 | 12,991,047.69 | 0.1% | 548.93 | 276.50 | 1.985 | 20.25 | 2.3% | 10.72 | `SaltedOutpointHasherBench_mismatch`
```python
compared to master:
create_set - 7,386,349.49 / 7,312,133.16 - 1.0% faster
hash - 42,254,558.08 / 41,978,882.62 - 0.6% faster
match - 16,962,220.14 / 16,549,695.42 - 2.4% faster
mismatch - 12,991,047.69 / 12,713,595.35 - 2.1% faster
```
ACKs for top commit:
achow101:
ACK 6eb5ba5
vasild:
ACK 6eb5ba5
sipa:
ACK 6eb5ba5
Tree-SHA512: 9688b87e1d79f8af9efc18a8487922c5f1735487a9c5b78029dd46abc1d94f05d499cd1036bd615849aa7d6b17d11653c968086050dd7d04300403ebd0e81210File tree
9 files changed
+132
-122
lines changed- src
- bench
- crypto
- test
- fuzz
- util
9 files changed
+132
-122
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
196 | | - | |
| 196 | + | |
197 | 197 | | |
198 | 198 | | |
199 | 199 | | |
200 | | - | |
201 | | - | |
202 | | - | |
| 200 | + | |
203 | 201 | | |
204 | 202 | | |
205 | 203 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | | - | |
22 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
23 | 26 | | |
24 | | - | |
| 27 | + | |
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
31 | 34 | | |
32 | | - | |
| 35 | + | |
| 36 | + | |
33 | 37 | | |
34 | 38 | | |
35 | 39 | | |
36 | 40 | | |
37 | 41 | | |
38 | 42 | | |
39 | | - | |
40 | | - | |
| 43 | + | |
41 | 44 | | |
42 | 45 | | |
43 | | - | |
| 46 | + | |
| 47 | + | |
44 | 48 | | |
45 | | - | |
| 49 | + | |
46 | 50 | | |
47 | 51 | | |
48 | 52 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
90 | | - | |
91 | | - | |
| 91 | + | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
| 115 | + | |
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
| 22 | + | |
31 | 23 | | |
32 | 24 | | |
33 | 25 | | |
34 | | - | |
| 26 | + | |
35 | 27 | | |
36 | | - | |
| 28 | + | |
37 | 29 | | |
38 | 30 | | |
39 | 31 | | |
40 | 32 | | |
41 | 33 | | |
42 | 34 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
47 | 39 | | |
48 | | - | |
| 40 | + | |
49 | 41 | | |
50 | 42 | | |
51 | 43 | | |
52 | 44 | | |
53 | 45 | | |
54 | | - | |
55 | | - | |
56 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
57 | 49 | | |
58 | 50 | | |
59 | 51 | | |
| |||
68 | 60 | | |
69 | 61 | | |
70 | 62 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
77 | 69 | | |
78 | 70 | | |
79 | 71 | | |
80 | 72 | | |
81 | 73 | | |
82 | 74 | | |
83 | | - | |
| 75 | + | |
84 | 76 | | |
85 | | - | |
| 77 | + | |
86 | 78 | | |
87 | 79 | | |
88 | 80 | | |
| |||
96 | 88 | | |
97 | 89 | | |
98 | 90 | | |
99 | | - | |
| 91 | + | |
100 | 92 | | |
101 | | - | |
| 93 | + | |
102 | 94 | | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
| 95 | + | |
108 | 96 | | |
109 | 97 | | |
110 | 98 | | |
| |||
136 | 124 | | |
137 | 125 | | |
138 | 126 | | |
139 | | - | |
| 127 | + | |
| 128 | + | |
140 | 129 | | |
141 | | - | |
| 130 | + | |
142 | 131 | | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
| 132 | + | |
149 | 133 | | |
150 | 134 | | |
151 | 135 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
14 | 26 | | |
15 | 27 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
20 | 31 | | |
21 | 32 | | |
22 | | - | |
| 33 | + | |
23 | 34 | | |
24 | | - | |
| 35 | + | |
25 | 36 | | |
26 | 37 | | |
27 | 38 | | |
| |||
32 | 43 | | |
33 | 44 | | |
34 | 45 | | |
35 | | - | |
| 46 | + | |
| 47 | + | |
36 | 48 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
44 | 54 | | |
45 | | - | |
46 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
47 | 71 | | |
48 | 72 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
122 | | - | |
| 121 | + | |
| 122 | + | |
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
| 107 | + | |
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
| 131 | + | |
132 | 132 | | |
133 | | - | |
| 133 | + | |
134 | 134 | | |
135 | 135 | | |
| 136 | + | |
136 | 137 | | |
137 | | - | |
138 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
139 | 145 | | |
140 | 146 | | |
141 | 147 | | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | 148 | | |
146 | | - | |
147 | | - | |
| 149 | + | |
148 | 150 | | |
149 | 151 | | |
150 | 152 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | | - | |
12 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
13 | 14 | | |
14 | | - | |
15 | | - | |
16 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
17 | 19 | | |
18 | | - | |
19 | | - | |
20 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
21 | 24 | | |
22 | | - | |
23 | | - | |
24 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
29 | | - | |
| 32 | + | |
| 33 | + | |
30 | 34 | | |
31 | 35 | | |
32 | 36 | | |
| |||
0 commit comments