diff options
author | MarcoFalke <falke.marco@gmail.com> | 2021-11-12 10:19:33 +0100 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2021-11-12 10:56:08 +0100 |
commit | fa8f60e31102e1153ad1452fbced51e54487a3d4 (patch) | |
tree | 2cdbc4e6776c99b1aba5796383951e89b6bcce89 /src/node | |
parent | 1ff265a20c36ada4c7b1c5c88d31eb92ec9e8420 (diff) |
scripted-diff: Move minisketchwrapper to src/node
-BEGIN VERIFY SCRIPT-
# Move module
git mv src/minisketchwrapper.cpp src/node/
git mv src/minisketchwrapper.h src/node/
# Replacements
sed -i 's:minisketchwrapper:node/minisketchwrapper:g' $(git grep -l minisketchwrapper)
sed -i 's:MINISKETCHWRAPPER_H:NODE_MINISKETCHWRAPPER_H:g' $(git grep -l MINISKETCHWRAPPER_H)
sed -i 's:DBWRAPPER_H:NODE_MINISKETCHWRAPPER_H:g' ./src/node/minisketchwrapper.h
-END VERIFY SCRIPT-
Diffstat (limited to 'src/node')
-rw-r--r-- | src/node/minisketchwrapper.cpp | 77 | ||||
-rw-r--r-- | src/node/minisketchwrapper.h | 17 |
2 files changed, 94 insertions, 0 deletions
diff --git a/src/node/minisketchwrapper.cpp b/src/node/minisketchwrapper.cpp new file mode 100644 index 0000000000..572df63463 --- /dev/null +++ b/src/node/minisketchwrapper.cpp @@ -0,0 +1,77 @@ +// Copyright (c) 2021 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include <node/minisketchwrapper.h> + +#include <logging.h> +#include <util/time.h> + +#include <minisketch.h> + +#include <algorithm> +#include <cstddef> +#include <cstdint> +#include <optional> +#include <utility> +#include <vector> + +namespace { + +static constexpr uint32_t BITS = 32; + +uint32_t FindBestImplementation() +{ + std::optional<std::pair<int64_t, uint32_t>> best; + + uint32_t max_impl = Minisketch::MaxImplementation(); + for (uint32_t impl = 0; impl <= max_impl; ++impl) { + std::vector<int64_t> benches; + uint64_t offset = 0; + /* Run a little benchmark with capacity 32, adding 184 entries, and decoding 11 of them once. */ + for (int b = 0; b < 11; ++b) { + if (!Minisketch::ImplementationSupported(BITS, impl)) break; + Minisketch sketch(BITS, impl, 32); + auto start = GetTimeMicros(); + for (uint64_t e = 0; e < 100; ++e) { + sketch.Add(e*1337 + b*13337 + offset); + } + for (uint64_t e = 0; e < 84; ++e) { + sketch.Add(e*1337 + b*13337 + offset); + } + offset += (*sketch.Decode(32))[0]; + auto stop = GetTimeMicros(); + benches.push_back(stop - start); + } + /* Remember which implementation has the best median benchmark time. */ + if (!benches.empty()) { + std::sort(benches.begin(), benches.end()); + if (!best || best->first > benches[5]) { + best = std::make_pair(benches[5], impl); + } + } + } + assert(best.has_value()); + LogPrintf("Using Minisketch implementation number %i\n", best->second); + return best->second; +} + +uint32_t Minisketch32Implementation() +{ + // Fast compute-once idiom. + static uint32_t best = FindBestImplementation(); + return best; +} + +} // namespace + + +Minisketch MakeMinisketch32(size_t capacity) +{ + return Minisketch(BITS, Minisketch32Implementation(), capacity); +} + +Minisketch MakeMinisketch32FP(size_t max_elements, uint32_t fpbits) +{ + return Minisketch::CreateFP(BITS, Minisketch32Implementation(), max_elements, fpbits); +} diff --git a/src/node/minisketchwrapper.h b/src/node/minisketchwrapper.h new file mode 100644 index 0000000000..426781d508 --- /dev/null +++ b/src/node/minisketchwrapper.h @@ -0,0 +1,17 @@ +// Copyright (c) 2021 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_NODE_MINISKETCHWRAPPER_H +#define BITCOIN_NODE_MINISKETCHWRAPPER_H + +#include <minisketch.h> +#include <cstddef> +#include <cstdint> + +/** Wrapper around Minisketch::Minisketch(32, implementation, capacity). */ +Minisketch MakeMinisketch32(size_t capacity); +/** Wrapper around Minisketch::CreateFP. */ +Minisketch MakeMinisketch32FP(size_t max_elements, uint32_t fpbits); + +#endif // BITCOIN_NODE_MINISKETCHWRAPPER_H |