HomePhabricator

util: optimize HexStr

Description

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

Reviewers: #bitcoin_abc, roqqit

Reviewed By: roqqit

Subscribers: Sintayew4

Differential Revision: https://reviews.bitcoinabc.org/D17159

Details

Provenance
Martin Leitner-Ankerl <martin.ankerl@gmail.com>Authored on Mar 23 2022, 06:08
PiRKCommitted on Tue, Nov 19, 21:04
PiRKPushed on Tue, Nov 19, 21:04
Reviewer
roqqit
Differential Revision
D17159: util: optimize HexStr
Parents
rABC94d6cf737131: Rework receive buffer pushback
Branches
Unknown
Tags
Unknown

Event Timeline

PiRK committed rABCa91e6a41e27b: util: optimize HexStr (authored by Martin Leitner-Ankerl <martin.ankerl@gmail.com>).Tue, Nov 19, 21:04