]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/cmdparse: use map::find() only a single time
authorKefu Chai <kchai@redhat.com>
Thu, 20 May 2021 02:27:37 +0000 (10:27 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 22 May 2021 00:48:52 +0000 (08:48 +0800)
instead of using the combo of

if (map.count(key)) {
  return map.find(key)->second;
}

just use

found = map.find(key);
if (found != map.end()) {
  return found->second;
}

to avoid repeating the lookup in the map with the same key.

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

index 1754869eaa719edecefe112c8ca5eb484b6f22cd..6817f6454115f1ba910731e6846af9dc63f8f865 100644 (file)
@@ -645,29 +645,30 @@ bool cmd_getval(const cmdmap_t& cmdmap,
    * so earlier clients are sent a CephChoices argdesc instead, and will
    * send us a "--foo-bar" value string for boolean arguments.
    */
-  if (cmdmap.count(k)) {
+  auto found = cmdmap.find(k);
+  if (found == cmdmap.end()) {
+    return false;
+  }
+  try {
+    val = boost::get<bool>(found->second);
+    return true;
+  } catch (boost::bad_get&) {
     try {
-      val = boost::get<bool>(cmdmap.find(k)->second);
-      return true;
-    } catch (boost::bad_get&) {
-      try {
-        std::string expected{"--"};
-       expected += k;
-        std::replace(expected.begin(), expected.end(), '_', '-');
-
-        std::string v_str = boost::get<std::string>(cmdmap.find(k)->second);
-        if (v_str == expected) {
-          val = true;
-          return true;
-        } else {
-          throw bad_cmd_get(k, cmdmap);
-        }
-      } catch (boost::bad_get&) {
-        throw bad_cmd_get(k, cmdmap);
+      std::string expected{"--"};
+      expected += k;
+      std::replace(expected.begin(), expected.end(), '_', '-');
+
+      std::string v_str = boost::get<std::string>(found->second);
+      if (v_str == expected) {
+       val = true;
+       return true;
+      } else {
+       throw bad_cmd_get(k, cmdmap);
       }
+    } catch (boost::bad_get&) {
+      throw bad_cmd_get(k, cmdmap);
     }
   }
-  return false;
 }
 
 }
index 78b6eb623beeeb4ad8d74005d6e806e3f625c14b..25d72e87920db5a42836e9af1efaf5b52b9a0785 100644 (file)
@@ -65,15 +65,16 @@ template <typename T>
 bool cmd_getval(const cmdmap_t& cmdmap,
                std::string_view k, T& val)
 {
-  if (cmdmap.count(k)) {
-    try {
-      val = boost::get<T>(cmdmap.find(k)->second);
-      return true;
-    } catch (boost::bad_get&) {
-      throw bad_cmd_get(k, cmdmap);
-    }
+  auto found = cmdmap.find(k);
+  if (found == cmdmap.end()) {
+    return false;
+  }
+  try {
+    val = boost::get<T>(found->second);
+    return true;
+  } catch (boost::bad_get&) {
+    throw bad_cmd_get(k, cmdmap);
   }
-  return false;
 }
 
 // with default
@@ -83,17 +84,17 @@ bool cmd_getval(
   const cmdmap_t& cmdmap, std::string_view k,
   T& val, const T& defval)
 {
-  if (cmdmap.count(k)) {
-    try {
-      val = boost::get<T>(cmdmap.find(k)->second);
-      return true;
-    } catch (boost::bad_get&) {
-      throw bad_cmd_get(k, cmdmap);
-    }
-  } else {
+  auto found = cmdmap.find(k);
+  if (found == cmdmap.end()) {
     val = defval;
     return true;
   }
+  try {
+    val = boost::get<T>(cmdmap.find(k)->second);
+    return true;
+  } catch (boost::bad_get&) {
+    throw bad_cmd_get(k, cmdmap);
+  }
 }
 
 template <typename T>