Skip to content

Commit 54b5e1a

Browse files
sipafanquake
authored andcommitted
Add thin Minisketch wrapper to pick best implementation
1 parent ee9dc71 commit 54b5e1a

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed

src/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ BITCOIN_CORE_H = \
167167
memusage.h \
168168
merkleblock.h \
169169
miner.h \
170+
minisketchwrapper.h \
170171
net.h \
171172
net_permissions.h \
172173
net_processing.h \
@@ -334,6 +335,7 @@ libbitcoin_server_a_SOURCES = \
334335
init.cpp \
335336
mapport.cpp \
336337
miner.cpp \
338+
minisketchwrapper.cpp \
337339
net.cpp \
338340
net_processing.cpp \
339341
node/blockstorage.cpp \

src/minisketchwrapper.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <minisketchwrapper.h>
6+
7+
#include <logging.h>
8+
#include <util/time.h>
9+
10+
#include <minisketch.h>
11+
12+
#include <algorithm>
13+
#include <cstddef>
14+
#include <cstdint>
15+
#include <optional>
16+
#include <utility>
17+
#include <vector>
18+
19+
namespace {
20+
21+
static constexpr uint32_t BITS = 32;
22+
23+
uint32_t FindBestImplementation()
24+
{
25+
std::optional<std::pair<int64_t, uint32_t>> best;
26+
27+
uint32_t max_impl = Minisketch::MaxImplementation();
28+
for (uint32_t impl = 0; impl <= max_impl; ++impl) {
29+
std::vector<int64_t> benches;
30+
uint64_t offset = 0;
31+
/* Run a little benchmark with capacity 32, adding 184 entries, and decoding 11 of them once. */
32+
for (int b = 0; b < 11; ++b) {
33+
if (!Minisketch::ImplementationSupported(BITS, impl)) break;
34+
Minisketch sketch(BITS, impl, 32);
35+
auto start = GetTimeMicros();
36+
for (uint64_t e = 0; e < 100; ++e) {
37+
sketch.Add(e*1337 + b*13337 + offset);
38+
}
39+
for (uint64_t e = 0; e < 84; ++e) {
40+
sketch.Add(e*1337 + b*13337 + offset);
41+
}
42+
offset += (*sketch.Decode(32))[0];
43+
auto stop = GetTimeMicros();
44+
benches.push_back(stop - start);
45+
}
46+
/* Remember which implementation has the best median benchmark time. */
47+
if (!benches.empty()) {
48+
std::sort(benches.begin(), benches.end());
49+
if (!best || best->first > benches[5]) {
50+
best = std::make_pair(benches[5], impl);
51+
}
52+
}
53+
}
54+
assert(best.has_value());
55+
LogPrintf("Using Minisketch implementation number %i\n", best->second);
56+
return best->second;
57+
}
58+
59+
uint32_t Minisketch32Implementation()
60+
{
61+
// Fast compute-once idiom.
62+
static uint32_t best = FindBestImplementation();
63+
return best;
64+
}
65+
66+
} // namespace
67+
68+
69+
Minisketch MakeMinisketch32(size_t capacity)
70+
{
71+
return Minisketch(BITS, Minisketch32Implementation(), capacity);
72+
}
73+
74+
Minisketch MakeMinisketch32FP(size_t max_elements, uint32_t fpbits)
75+
{
76+
return Minisketch::CreateFP(BITS, Minisketch32Implementation(), max_elements, fpbits);
77+
}

src/minisketchwrapper.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_MINISKETCHWRAPPER_H
6+
#define BITCOIN_MINISKETCHWRAPPER_H
7+
8+
#include <minisketch.h>
9+
#include <cstddef>
10+
#include <cstdint>
11+
12+
/** Wrapper around Minisketch::Minisketch(32, implementation, capacity). */
13+
Minisketch MakeMinisketch32(size_t capacity);
14+
/** Wrapper around Minisketch::CreateFP. */
15+
Minisketch MakeMinisketch32FP(size_t max_elements, uint32_t fpbits);
16+
17+
#endif // BITCOIN_DBWRAPPER_H

src/test/minisketch_tests.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <minisketch.h>
6+
#include <minisketchwrapper.h>
67
#include <random.h>
78
#include <test/util/setup_common.h>
89

@@ -24,13 +25,13 @@ BOOST_AUTO_TEST_CASE(minisketch_test)
2425
uint32_t start_b = start_a + a_not_b;
2526
uint32_t end_b = start_b + both + b_not_a;
2627

27-
Minisketch sketch_a(32, 0, 10);
28+
Minisketch sketch_a = MakeMinisketch32(10);
2829
for (uint32_t a = start_a; a < end_a; ++a) sketch_a.Add(a);
29-
Minisketch sketch_b(32, 0, 10);
30+
Minisketch sketch_b = MakeMinisketch32(10);
3031
for (uint32_t b = start_b; b < end_b; ++b) sketch_b.Add(b);
3132

32-
Minisketch sketch_ar(32, 0, 10);
33-
Minisketch sketch_br(32, 0, 10);
33+
Minisketch sketch_ar = MakeMinisketch32(10);
34+
Minisketch sketch_br = MakeMinisketch32(10);
3435
sketch_ar.Deserialize(sketch_a.Serialize());
3536
sketch_br.Deserialize(sketch_b.Serialize());
3637

0 commit comments

Comments
 (0)