From 4450f388e2992e0fac2ef53b40fa478ca7d23736 Mon Sep 17 00:00:00 2001 From: Ronen Friedman Date: Wed, 7 Jul 2021 08:36:27 +0000 Subject: [PATCH] common/hobject: speed up non-printables handling when listing hobjects Using charconv::to_chars(). Anecdotal speedup, handling an assorted mix of strings: 2.4X (tested on my laptop) Signed-off-by: Ronen Friedman --- src/common/hobject.cc | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/common/hobject.cc b/src/common/hobject.cc index fe4af49d8e9bc..1aee4cc42546a 100644 --- a/src/common/hobject.cc +++ b/src/common/hobject.cc @@ -1,6 +1,8 @@ // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- // vim: ts=8 sw=2 smarttab +#include + #include "hobject.h" #include "common/Formatter.h" @@ -210,15 +212,18 @@ void hobject_t::generate_test_instances(list& o) static void append_out_escaped(const string &in, string *out) { - for (string::const_iterator i = in.begin(); i != in.end(); ++i) { - int k = (int)(unsigned char)(*i); - if (k == '%' || k == ':' || k == '/' || k < 32 || k >= 127) { - out->push_back('%'); - char buf[3]; - snprintf(buf, sizeof(buf), "%02x", (int)(unsigned char)k); + for (auto c : in) { + int i = (int)(unsigned char)(c); + if (i <= 0x0f) { + char buf[4] = {'%', '0'}; + std::to_chars(buf + 2, buf + 3, i, 16); + out->append(buf); + } else if (i < 32 || i >= 127 || i == '%' || i == ':' || i == '/') { + char buf[4] = {'%'}; + std::to_chars(buf + 1, buf + 3, i, 16); out->append(buf); } else { - out->push_back(k); + out->push_back(c); } } } -- 2.39.5