Skip to content

Commit fcc3b65

Browse files
committed
Merge bitcoin/bitcoin#29607: refactor: Reduce memory copying operations in bech32 encoding
07f6417 Reduce memory copying operations in bech32 encode (Lőrinc) d5ece3c Reserve hrp memory in Decode and LocateErrors (Lőrinc) Pull request description: Started optimizing the base conversions in [TryParseHex](bitcoin/bitcoin#29458), [Base58](bitcoin/bitcoin#29473) and [IsSpace](bitcoin/bitcoin#29602) - this is the next step. Part of this change was already merged in bitcoin/bitcoin#30047, which made decoding `~26%` faster. 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` ``` ACKs for top commit: josibake: ACK bitcoin/bitcoin@07f6417 sipa: utACK 07f6417 achow101: ACK 07f6417 Tree-SHA512: 511885217d044ad7ef2bdf9203b8e0b94eec8b279bc193bb7e63e29ab868df6d21e9e4c7a24390358e1f9c131447ee42039df72edcf1e2b11e1856eb2b3e10dd
2 parents 080a47c + 07f6417 commit fcc3b65

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/bech32.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,13 @@ std::string Encode(Encoding encoding, const std::string& hrp, const data& values
363363
// to return a lowercase Bech32/Bech32m string, but if given an uppercase HRP, the
364364
// result will always be invalid.
365365
for (const char& c : hrp) assert(c < 'A' || c > 'Z');
366-
data checksum = CreateChecksum(encoding, hrp, values);
367-
data combined = Cat(values, checksum);
368-
std::string ret = hrp + '1';
369-
ret.reserve(ret.size() + combined.size());
370-
for (const auto c : combined) {
371-
ret += CHARSET[c];
372-
}
366+
367+
std::string ret;
368+
ret.reserve(hrp.size() + 1 + values.size() + CHECKSUM_SIZE);
369+
ret += hrp;
370+
ret += '1';
371+
for (const uint8_t& i : values) ret += CHARSET[i];
372+
for (const uint8_t& i : CreateChecksum(encoding, hrp, values)) ret += CHARSET[i];
373373
return ret;
374374
}
375375

@@ -393,6 +393,7 @@ DecodeResult Decode(const std::string& str, CharLimit limit) {
393393
values[i] = rev;
394394
}
395395
std::string hrp;
396+
hrp.reserve(pos);
396397
for (size_t i = 0; i < pos; ++i) {
397398
hrp += LowerCase(str[i]);
398399
}
@@ -425,6 +426,7 @@ std::pair<std::string, std::vector<int>> LocateErrors(const std::string& str, Ch
425426
}
426427

427428
std::string hrp;
429+
hrp.reserve(pos);
428430
for (size_t i = 0; i < pos; ++i) {
429431
hrp += LowerCase(str[i]);
430432
}

0 commit comments

Comments
 (0)