mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-11-20 10:38:42 +01:00
Reduce memory copying operations in bech32 encode
Here I've reduced the memory reallocations and copying operations in bech32 encode, making it ~15% faster. make && ./src/bench/bench_bitcoin --filter='Bech32Encode' --min-time=1000 Before: | ns/byte | byte/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 19.97 | 50,074,562.72 | 0.1% | 1.06 | `Bech32Encode` After: | ns/byte | byte/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 17.33 | 57,687,668.20 | 0.1% | 1.10 | `Bech32Encode` Co-authored-by: josibake <josibake@protonmail.com>
This commit is contained in:
parent
d5ece3c4b5
commit
07f64177a4
@ -363,13 +363,13 @@ std::string Encode(Encoding encoding, const std::string& hrp, const data& values
|
||||
// to return a lowercase Bech32/Bech32m string, but if given an uppercase HRP, the
|
||||
// result will always be invalid.
|
||||
for (const char& c : hrp) assert(c < 'A' || c > 'Z');
|
||||
data checksum = CreateChecksum(encoding, hrp, values);
|
||||
data combined = Cat(values, checksum);
|
||||
std::string ret = hrp + '1';
|
||||
ret.reserve(ret.size() + combined.size());
|
||||
for (const auto c : combined) {
|
||||
ret += CHARSET[c];
|
||||
}
|
||||
|
||||
std::string ret;
|
||||
ret.reserve(hrp.size() + 1 + values.size() + CHECKSUM_SIZE);
|
||||
ret += hrp;
|
||||
ret += '1';
|
||||
for (const uint8_t& i : values) ret += CHARSET[i];
|
||||
for (const uint8_t& i : CreateChecksum(encoding, hrp, values)) ret += CHARSET[i];
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user