Bitcoin Core  22.99.0
P2P Digital Currency
minisketchwrapper.cpp
Go to the documentation of this file.
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 
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 }
minisketchwrapper.h
assert
assert(!tx.IsCoinBase())
MakeMinisketch32FP
Minisketch MakeMinisketch32FP(size_t max_elements, uint32_t fpbits)
Wrapper around Minisketch::CreateFP.
Definition: minisketchwrapper.cpp:74
minisketch.h
time.h
LogPrintf
#define LogPrintf(...)
Definition: logging.h:187
MakeMinisketch32
Minisketch MakeMinisketch32(size_t capacity)
Wrapper around Minisketch::Minisketch(32, implementation, capacity).
Definition: minisketchwrapper.cpp:69
stop
static RPCHelpMan stop()
Definition: server.cpp:161
GetTimeMicros
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:122