From: Joel Rosdahl Date: Mon, 21 Sep 2020 19:43:44 +0000 (+0200) Subject: Optimize Util::format_hex X-Git-Tag: v4.0~69 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1152b4e52b55d8d6530312f5b88cc1dc227cee0c;p=thirdparty%2Fccache.git Optimize Util::format_hex Not that it matters much, but not using fmt::format makes Util::format_hex ≈20x faster. --- diff --git a/src/Util.cpp b/src/Util.cpp index 7b4d8b7e5..99eaf7880 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -524,10 +524,12 @@ format_argv_for_logging(const char* const* argv) std::string format_hex(const uint8_t* data, size_t size) { + static const char digits[] = "0123456789abcdef"; std::string result; - result.reserve(2 * size); - for (size_t i = 0; i < size; i++) { - result += fmt::format("{:02x}", data[i]); + result.resize(2 * size); + for (size_t i = 0; i < size; ++i) { + result[i * 2] = digits[data[i] >> 4]; + result[i * 2 + 1] = digits[data[i] & 0xF]; } return result; } diff --git a/src/third_party/base32hex.c b/src/third_party/base32hex.c index aebfab5d5..4c2275d03 100644 --- a/src/third_party/base32hex.c +++ b/src/third_party/base32hex.c @@ -23,7 +23,7 @@ * Returns length of encoded string. */ unsigned int base32hex(char *out, const uint8_t *in, unsigned int len) { -int buf = 0, bits = 0; +unsigned int buf = 0, bits = 0; char *x = out; while (len-- > 0) {