mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
The following scripted-diff commit will replace ParseHex("...") with "..."_hex_u8, but this replacement will not work in cases where vectors are needed instead of arrays, and is not ideal in cases where std::byte is accepted. For example, it is currently necessary to use _hex_v_u8 when calling CScript operator<< because that operator does not currently support std::array or std::byte. Conversely, it is incorrect to use _hex_v instead of _hex in net_processing.cpp for the MakeAndPushMessage argument, because if the argument is a std::vector it is considered variable-length and serialized with a size prefix, but if the argument is a std::array or Span is it considered fixed length and serialized without a prefix. By the same logic, it is also safe to change the NUMS_H constant in pubkey.cpp from a std::vector to std::array because it is never serialized.
35 lines
1.1 KiB
C++
35 lines
1.1 KiB
C++
// Copyright (c) 2018-2022 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 <bech32.h>
|
|
#include <bench/bench.h>
|
|
#include <util/strencodings.h>
|
|
|
|
#include <vector>
|
|
|
|
using namespace util::hex_literals;
|
|
|
|
static void Bech32Encode(benchmark::Bench& bench)
|
|
{
|
|
constexpr std::array<uint8_t, 32> v{"c97f5a67ec381b760aeaf67573bc164845ff39a3bb26a1cee401ac67243b48db"_hex_u8};
|
|
std::vector<unsigned char> tmp = {0};
|
|
tmp.reserve(1 + v.size() * 8 / 5);
|
|
ConvertBits<8, 5, true>([&](unsigned char c) { tmp.push_back(c); }, v.begin(), v.end());
|
|
bench.batch(v.size()).unit("byte").run([&] {
|
|
bech32::Encode(bech32::Encoding::BECH32, "bc", tmp);
|
|
});
|
|
}
|
|
|
|
|
|
static void Bech32Decode(benchmark::Bench& bench)
|
|
{
|
|
std::string addr = "bc1qkallence7tjawwvy0dwt4twc62qjgaw8f4vlhyd006d99f09";
|
|
bench.batch(addr.size()).unit("byte").run([&] {
|
|
bech32::Decode(addr);
|
|
});
|
|
}
|
|
|
|
|
|
BENCHMARK(Bech32Encode, benchmark::PriorityLevel::HIGH);
|
|
BENCHMARK(Bech32Decode, benchmark::PriorityLevel::HIGH);
|