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`
```
This commit is contained in:
Lőrinc 2024-02-25 17:50:25 +01:00
parent b7489ecb52
commit a19235c14b

View File

@ -81,6 +81,8 @@ template <typename Byte>
std::optional<std::vector<Byte>> TryParseHex(std::string_view str)
{
std::vector<Byte> vch;
vch.reserve(str.size() / 2); // two hex characters form a single byte
auto it = str.begin();
while (it != str.end()) {
if (IsSpace(*it)) {