Skip to content

Commit 7142c44

Browse files
committed
Add thin Minisketch wrapper to pick best implementation
1 parent fcb7528 commit 7142c44

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ BITCOIN_CORE_H = \
179179
memusage.h \
180180
merkleblock.h \
181181
miner.h \
182+
minisketchwrapper.h \
182183
net.h \
183184
net_permissions.h \
184185
net_processing.h \
@@ -337,6 +338,7 @@ libbitcoin_server_a_SOURCES = \
337338
init.cpp \
338339
mapport.cpp \
339340
miner.cpp \
341+
minisketchwrapper.cpp \
340342
net.cpp \
341343
net_processing.cpp \
342344
node/blockstorage.cpp \

src/minisketchwrapper.cpp

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

src/minisketchwrapper.h

Lines changed: 17 additions & 0 deletions
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 <stddef.h>
10+
#include <stdint.h>
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

Lines changed: 5 additions & 4 deletions
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
#include <vector>
@@ -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)