@@ -26,6 +26,20 @@ OUTCOME_CPP_DEFINE_CATEGORY(fc::storage::hamt, HamtError, e) {
26
26
namespace fc ::storage::hamt {
27
27
using common::which;
28
28
29
+ inline auto leafFind (Node::Leaf &leaf, BytesIn key) {
30
+ return std::find_if (
31
+ leaf.begin (), leaf.end (), [&](auto &p) { return p.first == key; });
32
+ }
33
+
34
+ inline void leafInsert (Node::Leaf &leaf, Node::Leaf::value_type pair) {
35
+ leaf.emplace (
36
+ std::lower_bound (leaf.begin (),
37
+ leaf.end (),
38
+ pair,
39
+ [&](auto &l, auto &r) { return l.first < r.first ; }),
40
+ std::move (pair));
41
+ }
42
+
29
43
CBOR2_ENCODE (Node) {
30
44
Bits bits;
31
45
auto l_items{s.list ()};
@@ -93,7 +107,7 @@ namespace fc::storage::hamt {
93
107
for (size_t j = 0 ; j < n_leaf; ++j) {
94
108
auto l_pair{l_leaf.list ()};
95
109
auto key{l_pair.get <Bytes>()};
96
- leaf.emplace (std::move (key), l_pair.raw ());
110
+ leaf.emplace_back (std::move (key), l_pair.raw ());
97
111
}
98
112
v.items [j] = std::move (leaf);
99
113
}
@@ -140,7 +154,7 @@ namespace fc::storage::hamt {
140
154
node = boost::get<Node::Ptr>(item);
141
155
} else {
142
156
auto &leaf = boost::get<Node::Leaf>(item);
143
- auto it{leaf. find ( key)};
157
+ auto it{leafFind (leaf, key)};
144
158
if (it == leaf.end ()) {
145
159
return HamtError::kNotFound ;
146
160
}
@@ -219,10 +233,10 @@ namespace fc::storage::hamt {
219
233
*boost::get<Node::Ptr>(item), consumeIndex (indices), key, value);
220
234
}
221
235
auto &leaf = boost::get<Node::Leaf>(item);
222
- if (auto it2{leaf. find ( key)}; it2 != leaf.end ()) {
236
+ if (auto it2{leafFind (leaf, key)}; it2 != leaf.end ()) {
223
237
copy (it2->second , value);
224
238
} else if (leaf.size () < kLeafMax ) {
225
- leaf. emplace ( copy (key), copy (value));
239
+ leafInsert (leaf, { copy (key), copy (value)} );
226
240
} else {
227
241
auto child = std::make_shared<Node>();
228
242
child->v3 = v3 ();
@@ -255,13 +269,14 @@ namespace fc::storage::hamt {
255
269
OUTCOME_TRY (cleanShard (item));
256
270
} else {
257
271
auto &leaf = boost::get<Node::Leaf>(item);
258
- if (leaf.find (key) == leaf.end ()) {
272
+ auto it2{leafFind (leaf, key)};
273
+ if (it2 == leaf.end ()) {
259
274
return HamtError::kNotFound ;
260
275
}
261
276
if (leaf.size () == 1 ) {
262
277
node.items .erase (index);
263
278
} else {
264
- leaf.erase (leaf. find (key) );
279
+ leaf.erase (it2 );
265
280
}
266
281
}
267
282
return outcome::success ();
@@ -281,7 +296,7 @@ namespace fc::storage::hamt {
281
296
return outcome::success ();
282
297
}
283
298
for (auto &pair : boost::get<Node::Leaf>(item2.second )) {
284
- leaf. emplace ( pair);
299
+ leafInsert (leaf, pair);
285
300
if (leaf.size () > kLeafMax ) {
286
301
return outcome::success ();
287
302
}
0 commit comments