Skip to content

Commit a19235c

Browse files
author
Lőrinc
committed
Preallocate result in TryParseHex to avoid resizing
Running `make && ./src/bench/bench_bitcoin -filter=HexParse` a few times results in: ``` | ns/base16 | base16/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 0.68 | 1,465,555,976.27 | 0.8% | 0.01 | `HexParse` | 0.68 | 1,472,962,920.18 | 0.3% | 0.01 | `HexParse` | 0.68 | 1,476,159,423.00 | 0.3% | 0.01 | `HexParse` ```
1 parent b7489ec commit a19235c

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

src/util/strencodings.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ template <typename Byte>
8181
std::optional<std::vector<Byte>> TryParseHex(std::string_view str)
8282
{
8383
std::vector<Byte> vch;
84+
vch.reserve(str.size() / 2); // two hex characters form a single byte
85+
8486
auto it = str.begin();
8587
while (it != str.end()) {
8688
if (IsSpace(*it)) {

0 commit comments

Comments
 (0)