]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/cmdparse: use string_view for the key
authorKefu Chai <kchai@redhat.com>
Thu, 20 May 2021 02:20:50 +0000 (10:20 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 22 May 2021 00:48:52 +0000 (08:48 +0800)
for better usability and performance. as the main use case of
cmd_getval() and cmd_putval() only uses a literal string for the key.
it's a waste to build a std::string out of it and throw it away after
looking the cmdmap with it.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/cmdparse.cc
src/common/cmdparse.h
src/mon/MDSMonitor.cc

index f1756a292a5215ec8b3bd978873d18778d7704f0..1754869eaa719edecefe112c8ca5eb484b6f22cd 100644 (file)
@@ -638,7 +638,7 @@ bool validate_cmd(CephContext* cct,
 }
 
 bool cmd_getval(const cmdmap_t& cmdmap,
-               const std::string& k, bool& val)
+               std::string_view k, bool& val)
 {
   /*
    * Specialized getval for booleans.  CephBool didn't exist before Nautilus,
@@ -651,7 +651,8 @@ bool cmd_getval(const cmdmap_t& cmdmap,
       return true;
     } catch (boost::bad_get&) {
       try {
-        std::string expected = "--" + k;
+        std::string expected{"--"};
+       expected += k;
         std::replace(expected.begin(), expected.end(), '_', '-');
 
         std::string v_str = boost::get<std::string>(cmdmap.find(k)->second);
index 112c24930532188ca35c5f62c9bf4a793f791cb4..78b6eb623beeeb4ad8d74005d6e806e3f625c14b 100644 (file)
@@ -48,8 +48,10 @@ std::string cmd_vartype_stringify(const cmd_vartype& v);
 
 struct bad_cmd_get : public std::exception {
   std::string desc;
-  bad_cmd_get(const std::string& f, const cmdmap_t& cmdmap) {
-    desc = "bad or missing field '" + f + "'";
+  bad_cmd_get(std::string_view f, const cmdmap_t& cmdmap) {
+    desc += "bad or missing field '";
+    desc += f;
+    desc += "'";
   }
   const char *what() const throw() override {
     return desc.c_str();
@@ -57,11 +59,11 @@ struct bad_cmd_get : public std::exception {
 };
 
 bool cmd_getval(const cmdmap_t& cmdmap,
-               const std::string& k, bool& val);
+               std::string_view k, bool& val);
 
 template <typename T>
 bool cmd_getval(const cmdmap_t& cmdmap,
-               const std::string& k, T& val)
+               std::string_view k, T& val)
 {
   if (cmdmap.count(k)) {
     try {
@@ -78,7 +80,7 @@ bool cmd_getval(const cmdmap_t& cmdmap,
 
 template <typename T>
 bool cmd_getval(
-  const cmdmap_t& cmdmap, const std::string& k,
+  const cmdmap_t& cmdmap, std::string_view k,
   T& val, const T& defval)
 {
   if (cmdmap.count(k)) {
@@ -96,9 +98,9 @@ bool cmd_getval(
 
 template <typename T>
 void
-cmd_putval(CephContext *cct, cmdmap_t& cmdmap, const std::string& k, const T& val)
+cmd_putval(CephContext *cct, cmdmap_t& cmdmap, std::string_view k, const T& val)
 {
-  cmdmap[k] = val;
+  cmdmap.insert_or_assign(std::string{k}, val);
 }
 
 bool validate_cmd(CephContext* cct,
index 92ced5a5efc2be1081afbd1228ec99860bf6a5fb..1b927ae358413db3817098034d3389e3e3cf57e8 100644 (file)
@@ -86,19 +86,19 @@ static const string MDS_HEALTH_PREFIX("mds_health");
  */
 namespace TOPNSPC::common {
 template<> bool cmd_getval(const cmdmap_t& cmdmap,
-                          const std::string& k, mds_gid_t &val)
+                          std::string_view k, mds_gid_t &val)
 {
   return cmd_getval(cmdmap, k, (int64_t&)val);
 }
 
 template<> bool cmd_getval(const cmdmap_t& cmdmap,
-                          const std::string& k, mds_rank_t &val)
+                          std::string_view k, mds_rank_t &val)
 {
   return cmd_getval(cmdmap, k, (int64_t&)val);
 }
 
 template<> bool cmd_getval(const cmdmap_t& cmdmap,
-                          const std::string& k, MDSMap::DaemonState &val)
+                          std::string_view k, MDSMap::DaemonState &val)
 {
   return cmd_getval(cmdmap, k, (int64_t&)val);
 }