This pull request introduces optimizations to the TryParseHex
function, focusing primarily on the ideal case (valid hexadecimal input without spaces).
A new benchmark, HexParse
was introduced in a separate commit.
The main optimization preallocates the result vector based on the input string’s length. This aims to completely avoid costly dynamic reallocations when no spaces are present.
Before:
0| ns/base16 | base16/s | err% | total | benchmark
1|--------------------:|--------------------:|--------:|----------:|:----------
2| 1.60 | 623,238,893.11 | 0.3% | 0.01 | `HexParse`
3| 1.65 | 606,747,566.34 | 0.6% | 0.01 | `HexParse`
4| 1.60 | 626,149,544.07 | 0.3% | 0.01 | `HexParse`
After:
0| ns/base16 | base16/s | err% | total | benchmark
1|--------------------:|--------------------:|--------:|----------:|:----------
2| 0.68 | 1,465,555,976.27 | 0.8% | 0.01 | `HexParse`
3| 0.68 | 1,472,962,920.18 | 0.3% | 0.01 | `HexParse`
4| 0.68 | 1,476,159,423.00 | 0.3% | 0.01 | `HexParse`