Page MenuHomePhabricator

util: optimize HexStr
ClosedPublic

Authored by PiRK on Nov 18 2024, 15:47.

Details

Reviewers
roqqit
Group Reviewers
Restricted Project
Commits
rABCa91e6a41e27b: util: optimize HexStr
Summary

test: Adds a test for HexStr that checks all 256 bytes

This makes sure the whole HexStr mapping table is checked.

bench: Adds a benchmark for HexStr

Benchmarks conversion of a full binary block into hex, like it is done in rest.cpp.

util: optimizes HexStr

In my benchmark, this rewrite improves runtime 27% (g++) to 46% (clang++) for the benchmark HexStrBench:

Note that the idea for this change comes from denis2342 in PR 23364. This is a rewrite so no unaligned accesses occur.

Also, the lookup table is now calculated at compile time, which hopefully makes the code a bit easier to review.

Before

|             ns/byte |              byte/s |    err% |     total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
|                0.52 |    1,924,791,810.24 |    0.2% |      0.01 | `HexStrBench` (clang)
|                0.65 |    1,547,633,014.74 |    0.3% |      0.01 | `HexStrBench` (gcc)

After

|             ns/byte |              byte/s |    err% |     total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
|                0.35 |    2,827,553,475.08 |    0.5% |      0.01 | `HexStrBench` (clang)
|                0.36 |    2,761,315,307.88 |    0.4% |      0.01 | `HexStrBench  (gcc)`

This is a backport of core#24852
Depends on D17138

Test Plan

ninja all check-all bitcoin-bench