]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/hobject: speed up non-printables handling when listing hobjects 42206/head
authorRonen Friedman <rfriedma@redhat.com>
Wed, 7 Jul 2021 08:36:27 +0000 (08:36 +0000)
committerRonen Friedman <rfriedma@redhat.com>
Thu, 8 Jul 2021 07:00:47 +0000 (10:00 +0300)
Using charconv::to_chars().
Anecdotal speedup, handling an assorted mix of strings: 2.4X
(tested on my laptop)

Signed-off-by: Ronen Friedman <rfriedma@redhat.com>
src/common/hobject.cc

index fe4af49d8e9bcf4e73fc34d2a5bf18267dde85ef..1aee4cc42546a5705808d1f6ee20ddd6d71e1673 100644 (file)
@@ -1,6 +1,8 @@
 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
 // vim: ts=8 sw=2 smarttab
 
+#include <charconv>
+
 #include "hobject.h"
 #include "common/Formatter.h"
 
@@ -210,15 +212,18 @@ void hobject_t::generate_test_instances(list<hobject_t*>& 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);
     }
   }
 }