Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2021-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <node/minisketchwrapper.h>7#include <util/log.h>8#include <util/time.h>10#include <minisketch.h>12#include <algorithm>13#include <cstddef>14#include <cstdint>15#include <optional>16#include <utility>17#include <vector>19namespace node {20namespace {22static constexpr uint32_t BITS = 32;24uint32_t FindBestImplementation()25{26std::optional<std::pair<SteadyClock::duration, uint32_t>> best;28uint32_t max_impl = Minisketch::MaxImplementation();29for (uint32_t impl = 0; impl <= max_impl; ++impl) {30std::vector<SteadyClock::duration> benches;31uint64_t offset = 0;32/* Run a little benchmark with capacity 32, adding 184 entries, and decoding 11 of them once. */33for (int b = 0; b < 11; ++b) {34if (!Minisketch::ImplementationSupported(BITS, impl)) break;35Minisketch sketch(BITS, impl, 32);36auto start = SteadyClock::now();37for (uint64_t e = 0; e < 100; ++e) {38sketch.Add(e*1337 + b*13337 + offset);39}40for (uint64_t e = 0; e < 84; ++e) {41sketch.Add(e*1337 + b*13337 + offset);42}43offset += (*sketch.Decode(32))[0];44auto stop = SteadyClock::now();45benches.push_back(stop - start);46}47/* Remember which implementation has the best median benchmark time. */48if (!benches.empty()) {49std::sort(benches.begin(), benches.end());50if (!best || best->first > benches[5]) {51best = std::make_pair(benches[5], impl);52}53}54}55assert(best.has_value());56LogInfo("Using Minisketch implementation number %i", best->second);57return best->second;58}60uint32_t Minisketch32Implementation()61{62// Fast compute-once idiom.63static uint32_t best = FindBestImplementation();64return best;65}67} // namespace70Minisketch MakeMinisketch32(size_t capacity)71{72return Minisketch(BITS, Minisketch32Implementation(), capacity);73}75Minisketch MakeMinisketch32FP(size_t max_elements, uint32_t fpbits)76{77return Minisketch::CreateFP(BITS, Minisketch32Implementation(), max_elements, fpbits);78}79} // namespace node
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.