Skip to content

Commit 5e61532

Browse files
committed
util: optimizes HexStr
In my benchmark, this rewrite improves runtime 27% (g++) to 46% (clang++) for the benchmark `HexStrBench`: g++ 11.2.0 | ns/byte | byte/s | err% | ins/byte | cyc/byte | IPC | bra/byte | miss% | total | benchmark |--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:---------- | 0.94 | 1,061,381,310.36 | 0.7% | 12.00 | 3.01 | 3.990 | 1.00 | 0.0% | 0.01 | `HexStrBench` master | 0.68 | 1,465,366,544.25 | 1.7% | 6.00 | 2.16 | 2.778 | 1.00 | 0.0% | 0.01 | `HexStrBench` branch clang++ 13.0.1 | ns/byte | byte/s | err% | ins/byte | cyc/byte | IPC | bra/byte | miss% | total | benchmark |--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:---------- | 0.80 | 1,244,713,415.92 | 0.9% | 10.00 | 2.56 | 3.913 | 0.50 | 0.0% | 0.01 | `HexStrBench` master | 0.43 | 2,324,188,940.72 | 0.2% | 4.00 | 1.37 | 2.914 | 0.25 | 0.0% | 0.01 | `HexStrBench` branch 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.
1 parent 4e2b99f commit 5e61532

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/util/strencodings.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <tinyformat.h>
1010

1111
#include <algorithm>
12+
#include <array>
1213
#include <cstdlib>
1314
#include <cstring>
1415
#include <limits>
@@ -508,17 +509,37 @@ std::string Capitalize(std::string str)
508509
return str;
509510
}
510511

512+
namespace {
513+
514+
using ByteAsHex = std::array<char, 2>;
515+
516+
constexpr std::array<ByteAsHex, 256> CreateByteToHexMap()
517+
{
518+
constexpr char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
519+
520+
std::array<ByteAsHex, 256> byte_to_hex{};
521+
for (size_t i = 0; i < byte_to_hex.size(); ++i) {
522+
byte_to_hex[i][0] = hexmap[i >> 4];
523+
byte_to_hex[i][1] = hexmap[i & 15];
524+
}
525+
return byte_to_hex;
526+
}
527+
528+
} // namespace
529+
511530
std::string HexStr(const Span<const uint8_t> s)
512531
{
513532
std::string rv(s.size() * 2, '\0');
514-
static constexpr char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
515-
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
516-
auto it = rv.begin();
533+
static constexpr auto byte_to_hex = CreateByteToHexMap();
534+
static_assert(sizeof(byte_to_hex) == 512);
535+
536+
char* it = rv.data();
517537
for (uint8_t v : s) {
518-
*it++ = hexmap[v >> 4];
519-
*it++ = hexmap[v & 15];
538+
std::memcpy(it, byte_to_hex[v].data(), 2);
539+
it += 2;
520540
}
521-
assert(it == rv.end());
541+
542+
assert(it == rv.data() + rv.size());
522543
return rv;
523544
}
524545

0 commit comments

Comments
 (0)