aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorW. J. van der Laan <laanwj@protonmail.com>2021-05-19 10:07:25 +0200
committerW. J. van der Laan <laanwj@protonmail.com>2021-05-19 10:07:53 +0200
commit1ed859e90e18384376e3a1ff0cb76f3e9ab11c2d (patch)
tree94fc45fb3be9eb9eb69237d9f41a12b40a4a7359 /src/util
parent2fc111b6e3b051b45bc44267c3397cc12cadae3a (diff)
parent74bf850ac47735f2ef4306059d3e664d40cac85e (diff)
downloadbitcoin-1ed859e90e18384376e3a1ff0cb76f3e9ab11c2d.tar.xz
Merge bitcoin/bitcoin#21173: util: faster HexStr => 13% faster blockToJSON
74bf850ac47735f2ef4306059d3e664d40cac85e faster HexStr => 13% faster blockToJSON (Martin Ankerl) Pull request description: `std::string`'s push_back is rather slow because it needs to check & update the string size. For `HexStr` the output string size is already easily know, so we can initially create the string with the correct size and then just assign the data. `HexStr` is heavily usd in `blockToJSON`, so this change is a noticeable benefit. Benchmark on an i7-8700 @3.2GHz: * 71,315,461.00 ns/op master * 62,842,490.00 ns/op this commit So this little change makes `blockToJSON` about ~13% faster. ACKs for top commit: laanwj: Code review ACK 74bf850ac47735f2ef4306059d3e664d40cac85e theStack: re-ACK 74bf850ac47735f2ef4306059d3e664d40cac85e Tree-SHA512: fc99105123edc11f4e40ed77aea80cf7f32e49c53369aa364b38395dcb48575e15040b0489ed30d0fe857c032a04e225c33e9d95cdfa109a3cb5a6ec9a972415
Diffstat (limited to 'src/util')
-rw-r--r--src/util/strencodings.cpp11
1 files changed, 6 insertions, 5 deletions
diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp
index 4734de3e0b..f514613f0d 100644
--- a/src/util/strencodings.cpp
+++ b/src/util/strencodings.cpp
@@ -593,13 +593,14 @@ std::string Capitalize(std::string str)
std::string HexStr(const Span<const uint8_t> s)
{
- std::string rv;
+ std::string rv(s.size() * 2, '\0');
static constexpr char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
- rv.reserve(s.size() * 2);
- for (uint8_t v: s) {
- rv.push_back(hexmap[v >> 4]);
- rv.push_back(hexmap[v & 15]);
+ auto it = rv.begin();
+ for (uint8_t v : s) {
+ *it++ = hexmap[v >> 4];
+ *it++ = hexmap[v & 15];
}
+ assert(it == rv.end());
return rv;
}