Skip to content

Commit f5d4c65

Browse files
authored
Merge pull request libbitcoin#1582 from evoskuil/master
Optimize populate by avoiding hash copies, normalize ref optimizations.
2 parents cd5ec49 + f8da59c commit f5d4c65

File tree

14 files changed

+218
-158
lines changed

14 files changed

+218
-158
lines changed

include/bitcoin/system/chain/block.hpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#define LIBBITCOIN_SYSTEM_CHAIN_BLOCK_HPP
2121

2222
#include <memory>
23-
#include <unordered_set>
2423
#include <vector>
2524
#include <bitcoin/system/chain/context.hpp>
2625
#include <bitcoin/system/chain/header.hpp>
@@ -188,15 +187,6 @@ class BC_API block
188187

189188
private:
190189
typedef struct { size_t nominal; size_t witnessed; } sizes;
191-
using point_cref = std::reference_wrapper<const point>;
192-
using point_hash = std::hash<std::reference_wrapper<const point>>;
193-
using hash_cref = std::reference_wrapper<const hash_digest>;
194-
using hash_hash = unique_hash_t<>;
195-
196-
using unordered_set_of_constant_referenced_points =
197-
std::unordered_set<point_cref, point_hash>;
198-
using unordered_set_of_constant_referenced_hashes =
199-
std::unordered_set<hash_cref, hash_hash>;
200190

201191
void assign_data(reader& source, bool witness) NOEXCEPT;
202192
static block from_data(reader& source, bool witness) NOEXCEPT;
@@ -243,7 +233,7 @@ struct hash<bc::system::chain::block>
243233
{
244234
size_t operator()(const bc::system::chain::block& value) const NOEXCEPT
245235
{
246-
return bc::system::unique_hash_t<>{}(value.hash());
236+
return bc::system::unique_hash(value.hash());
247237
}
248238
};
249239
} // namespace std

include/bitcoin/system/chain/checkpoint.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ struct hash<bc::system::chain::checkpoint>
127127
size_t operator()(const bc::system::chain::checkpoint& value) const NOEXCEPT
128128
{
129129
return bc::system::hash_combine(value.height(),
130-
bc::system::unique_hash_t<>{}(value.hash()));
130+
bc::system::unique_hash(value.hash()));
131131
}
132132
};
133133
} // namespace std

include/bitcoin/system/chain/header.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ struct hash<bc::system::chain::header>
175175
{
176176
size_t operator()(const bc::system::chain::header& value) const NOEXCEPT
177177
{
178-
return bc::system::unique_hash_t<>{}(value.hash());
178+
return bc::system::unique_hash(value.hash());
179179
}
180180
};
181181
} // namespace std

include/bitcoin/system/chain/input.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ typedef std_vector<input::cptr> input_cptrs;
171171
typedef std::shared_ptr<input_cptrs> inputs_ptr;
172172
typedef std::shared_ptr<const input_cptrs> inputs_cptr;
173173

174+
/// Constant reference optimizers.
175+
struct cref_point { hash_cref hash; uint32_t index; };
176+
using unordered_map_of_cref_point_to_output_cptr_cref =
177+
std::unordered_map<cref_point, output_cptr_cref>;
178+
BC_API bool operator<(const cref_point& left, const cref_point& right) NOEXCEPT;
179+
BC_API bool operator==(const cref_point& left, const cref_point& right) NOEXCEPT;
180+
BC_API bool operator!=(const cref_point& left, const cref_point& right) NOEXCEPT;
181+
174182
DECLARE_JSON_VALUE_CONVERTORS(input);
175183
DECLARE_JSON_VALUE_CONVERTORS(input::cptr);
176184

@@ -188,6 +196,17 @@ struct hash<bc::system::chain::input>
188196
return std::hash<bc::system::data_chunk>{}(value.to_data());
189197
}
190198
};
199+
200+
template<>
201+
struct hash<bc::system::chain::cref_point>
202+
{
203+
std::size_t operator()(
204+
const bc::system::chain::cref_point& value) const NOEXCEPT
205+
{
206+
return bc::system::hash_combine(value.index,
207+
bc::system::unique_hash(value.hash.get()));
208+
}
209+
};
191210
} // namespace std
192211

193212
#endif

include/bitcoin/system/chain/output.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ typedef std_vector<output::cptr> output_cptrs;
114114
typedef std::shared_ptr<output_cptrs> outputs_ptr;
115115
typedef std::shared_ptr<const output_cptrs> outputs_cptr;
116116

117+
/// Constant reference optimizers.
118+
using output_cptr_cref = std::reference_wrapper<const output::cptr>;
119+
117120
DECLARE_JSON_VALUE_CONVERTORS(output);
118121
DECLARE_JSON_VALUE_CONVERTORS(output::cptr);
119122

include/bitcoin/system/chain/point.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <istream>
2323
#include <memory>
24+
#include <unordered_set>
2425
#include <vector>
2526
#include <bitcoin/system/data/data.hpp>
2627
#include <bitcoin/system/define.hpp>
@@ -112,6 +113,13 @@ bool operator<(const point& left, const point& right) NOEXCEPT;
112113

113114
typedef std_vector<point> points;
114115

116+
/// Constant reference optimizers.
117+
using point_cref = std::reference_wrapper<const point>;
118+
using unordered_set_of_point_cref = std::unordered_set<point_cref>;
119+
BC_API bool operator<(const point_cref& left, const point_cref& right) NOEXCEPT;
120+
BC_API bool operator==(const point_cref& left, const point_cref& right) NOEXCEPT;
121+
BC_API bool operator!=(const point_cref& left, const point_cref& right) NOEXCEPT;
122+
115123
DECLARE_JSON_VALUE_CONVERTORS(point);
116124
DECLARE_JSON_VALUE_CONVERTORS(point::cptr);
117125

@@ -127,27 +135,19 @@ struct hash<bc::system::chain::point>
127135
size_t operator()(const bc::system::chain::point& value) const NOEXCEPT
128136
{
129137
return bc::system::hash_combine(value.index(),
130-
bc::system::unique_hash_t<>{}(value.hash()));
138+
bc::system::unique_hash(value.hash()));
131139
}
132140
};
133141

134142
template<>
135-
struct hash<std::reference_wrapper<const bc::system::chain::point>>
143+
struct hash<bc::system::chain::point_cref>
136144
{
137-
using wrapped = std::reference_wrapper<const bc::system::chain::point>;
138-
std::size_t operator()(const wrapped& point) const NOEXCEPT
145+
std::size_t operator()(
146+
const bc::system::chain::point_cref& value) const NOEXCEPT
139147
{
140-
return std::hash<bc::system::chain::point>{}(point.get());
148+
return std::hash<bc::system::chain::point>{}(value.get());
141149
}
142150
};
143-
144-
inline bool operator==(
145-
const std::reference_wrapper<const bc::system::chain::point>& left,
146-
const std::reference_wrapper<const bc::system::chain::point>& right) NOEXCEPT
147-
{
148-
return left.get() == right.get();
149-
}
150-
151151
} // namespace std
152152

153153
#endif

include/bitcoin/system/chain/transaction.hpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,16 +300,9 @@ struct hash<bc::system::chain::transaction>
300300
size_t operator()(const bc::system::chain::transaction& value) const NOEXCEPT
301301
{
302302
// Witness coinbases will collide (null_hash).
303-
return bc::system::unique_hash_t<>{}(value.hash(true));
303+
return bc::system::unique_hash(value.hash(true));
304304
}
305305
};
306-
307-
inline bool operator==(
308-
const std::reference_wrapper<const bc::system::hash_digest>& left,
309-
const std::reference_wrapper<const bc::system::hash_digest>& right) NOEXCEPT
310-
{
311-
return left.get() == right.get();
312-
}
313306
} // namespace std
314307

315308
#endif

include/bitcoin/system/hash/functions.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <algorithm>
2323
#include <memory>
24+
#include <unordered_set>
2425
#include <bitcoin/system/data/data.hpp>
2526
#include <bitcoin/system/define.hpp>
2627
#include <bitcoin/system/endian/endian.hpp>
@@ -156,9 +157,35 @@ INLINE constexpr size_t djb2_hash(const data_slice& data) NOEXCEPT;
156157
/// Combine hash values, such as a pair of djb2_hash outputs [hash tables].
157158
INLINE constexpr size_t hash_combine(size_t left, size_t right) NOEXCEPT;
158159

160+
/// Constant reference optimizers.
161+
using hash_cref = std::reference_wrapper<const hash_digest>;
162+
using unordered_set_of_hash_cref = std::unordered_set<hash_cref>;
163+
159164
} // namespace system
160165
} // namespace libbitcoin
161166

167+
/// Comparison operators for constant reference optimizers.
168+
namespace std
169+
{
170+
inline bool operator<(const bc::system::hash_cref& left,
171+
const bc::system::hash_cref& right) NOEXCEPT
172+
{
173+
return left.get() < right.get();
174+
}
175+
176+
inline bool operator==(const bc::system::hash_cref& left,
177+
const bc::system::hash_cref& right) NOEXCEPT
178+
{
179+
return left.get() == right.get();
180+
}
181+
182+
inline bool operator!=(const bc::system::hash_cref& left,
183+
const bc::system::hash_cref& right) NOEXCEPT
184+
{
185+
return !(left == right);
186+
}
187+
} // namespace std
188+
162189
/// Extend std and boost namespaces with djb2_hash.
163190
/// ---------------------------------------------------------------------------
164191
/// This allows data_array/chunk to be incorporated into std/boost hash tables.
@@ -182,6 +209,15 @@ struct hash<bc::system::data_array<Size>>
182209
return bc::system::djb2_hash(data);
183210
}
184211
};
212+
213+
template <>
214+
struct hash<bc::system::hash_cref>
215+
{
216+
size_t operator()(const bc::system::hash_cref& hash) const NOEXCEPT
217+
{
218+
return bc::system::unique_hash(hash.get());
219+
}
220+
};
185221
} // namespace std
186222

187223
namespace boost

0 commit comments

Comments
 (0)