From 969cd6dc1d60acd98c89815dd53c11bf4dac2518 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 12 Jun 2019 17:47:05 -0400 Subject: [PATCH] common/hex_util: Reserve std::string memory ahead of time Avoids potentially performing multiple reallocations (depending on the size of the input data) by reserving the necessary amount of memory ahead of time. This is trivially doable, so there's no harm in it. --- src/common/hex_util.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/common/hex_util.h b/src/common/hex_util.h index a64c9b485..bb4736f96 100644 --- a/src/common/hex_util.h +++ b/src/common/hex_util.h @@ -36,10 +36,15 @@ std::string HexToString(const ContiguousContainer& data, bool upper = true) { static_assert(std::is_same_v, "Underlying type within the contiguous container must be u8."); + constexpr std::size_t pad_width = 2; + std::string out; + out.reserve(std::size(data) * pad_width); + for (const u8 c : data) { out += fmt::format(upper ? "{:02X}" : "{:02x}", c); } + return out; }