Bitcoin Core  24.99.0
P2P Digital Currency
gcs_filter.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2020 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 <bench/bench.h>
6 #include <blockfilter.h>
7 
9 {
10  GCSFilter::ElementSet elements;
11 
12  // Testing the benchmarks with different number of elements show that a filter
13  // with at least 100,000 elements results in benchmarks that have the same
14  // ns/op. This makes it easy to reason about how long (in nanoseconds) a single
15  // filter element takes to process.
16  for (int i = 0; i < 100000; ++i) {
17  GCSFilter::Element element(32);
18  element[0] = static_cast<unsigned char>(i);
19  element[1] = static_cast<unsigned char>(i >> 8);
20  elements.insert(std::move(element));
21  }
22 
23  return elements;
24 }
25 
27 {
28  auto elements = GenerateGCSTestElements();
29 
30  GCSFilter filter({0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, elements);
31  BlockFilter block_filter(BlockFilterType::BASIC, {}, filter.GetEncoded(), /*skip_decode_check=*/false);
32 
33  bench.run([&] {
34  block_filter.GetHash();
35  });
36 }
37 
39 {
40  auto elements = GenerateGCSTestElements();
41 
42  uint64_t siphash_k0 = 0;
43  bench.run([&]{
44  GCSFilter filter({siphash_k0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, elements);
45 
46  siphash_k0++;
47  });
48 }
49 
50 static void GCSFilterDecode(benchmark::Bench& bench)
51 {
52  auto elements = GenerateGCSTestElements();
53 
54  GCSFilter filter({0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, elements);
55  auto encoded = filter.GetEncoded();
56 
57  bench.run([&] {
58  GCSFilter filter({0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, encoded, /*skip_decode_check=*/false);
59  });
60 }
61 
63 {
64  auto elements = GenerateGCSTestElements();
65 
66  GCSFilter filter({0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, elements);
67  auto encoded = filter.GetEncoded();
68 
69  bench.run([&] {
70  GCSFilter filter({0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, encoded, /*skip_decode_check=*/true);
71  });
72 }
73 
74 static void GCSFilterMatch(benchmark::Bench& bench)
75 {
76  auto elements = GenerateGCSTestElements();
77 
78  GCSFilter filter({0, 0, BASIC_FILTER_P, BASIC_FILTER_M}, elements);
79 
80  bench.run([&] {
81  filter.Match(GCSFilter::Element());
82  });
83 }
constexpr uint8_t BASIC_FILTER_P
Definition: blockfilter.h:86
constexpr uint32_t BASIC_FILTER_M
Definition: blockfilter.h:87
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:112
This implements a Golomb-coded set as defined in BIP 158.
Definition: blockfilter.h:26
std::vector< unsigned char > Element
Definition: blockfilter.h:28
std::unordered_set< Element, ByteVectorHash > ElementSet
Definition: blockfilter.h:29
const std::vector< unsigned char > & GetEncoded() const LIFETIMEBOUND
Definition: blockfilter.h:70
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
BENCHMARK(GCSBlockFilterGetHash)
static void GCSFilterConstruct(benchmark::Bench &bench)
Definition: gcs_filter.cpp:38
static void GCSFilterDecode(benchmark::Bench &bench)
Definition: gcs_filter.cpp:50
static void GCSBlockFilterGetHash(benchmark::Bench &bench)
Definition: gcs_filter.cpp:26
static void GCSFilterDecodeSkipCheck(benchmark::Bench &bench)
Definition: gcs_filter.cpp:62
static void GCSFilterMatch(benchmark::Bench &bench)
Definition: gcs_filter.cpp:74
static const GCSFilter::ElementSet GenerateGCSTestElements()
Definition: gcs_filter.cpp:8