]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common/config: return const reference instead of a copy
authorKefu Chai <kchai@redhat.com>
Fri, 6 Oct 2017 10:27:51 +0000 (18:27 +0800)
committerKefu Chai <kchai@redhat.com>
Mon, 9 Oct 2017 02:42:53 +0000 (10:42 +0800)
avoid creating temporary Option::value_t instances if possible

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

index 2b54a3bf95bfb76aecec908d2dc975d199c2ad85..404928f0e369e86f211753d03d98dcaf450635b6 100644 (file)
@@ -871,18 +871,21 @@ int md_config_t::get_val(const std::string &key, char **buf, int len) const
   return _get_val(key, buf,len);
 }
 
-Option::value_t md_config_t::get_val_generic(const std::string &key) const
+const Option::value_t&
+md_config_t::get_val_generic(const std::string &key) const
 {
   Mutex::Locker l(lock);
   return _get_val_generic(key);
 }
 
-Option::value_t md_config_t::_get_val_generic(const std::string &key) const
+const Option::value_t&
+md_config_t::_get_val_generic(const std::string &key) const
 {
   assert(lock.is_locked());
 
+  static const Option::value_t empty{boost::blank()};
   if (key.empty()) {
-    return Option::value_t(boost::blank());
+    return empty;
   }
 
   // In key names, leading and trailing whitespace are not significant.
@@ -894,7 +897,7 @@ Option::value_t md_config_t::_get_val_generic(const std::string &key) const
     // entries in ::values
     return values.at(k);
   } else {
-    return Option::value_t(boost::blank());
+    return empty;
   }
 }
 
@@ -902,12 +905,12 @@ int md_config_t::_get_val(const std::string &key, std::string *value) const {
   assert(lock.is_locked());
 
   std::string normalized_key(ConfFile::normalize_key_name(key));
-  Option::value_t config_value = _get_val_generic(normalized_key.c_str());
+  auto& config_value = _get_val_generic(normalized_key.c_str());
   if (!boost::get<boost::blank>(&config_value)) {
     ostringstream oss;
-    if (bool *flag = boost::get<bool>(&config_value)) {
+    if (auto *flag = boost::get<bool>(&config_value)) {
       oss << (*flag ? "true" : "false");
-    } else if (double *dp = boost::get<double>(&config_value)) {
+    } else if (auto *dp = boost::get<double>(&config_value)) {
       oss << std::fixed << *dp;
     } else {
       oss << config_value;
index b0ba521afee7915f0261547a3b9b70d29ccc4267..2b2509f98a6f4494b6747de087afceb6f6848c72 100644 (file)
@@ -160,7 +160,7 @@ public:
   // No metavariables will be returned (they will have already been expanded)
   int get_val(const std::string &key, char **buf, int len) const;
   int _get_val(const std::string &key, char **buf, int len) const;
-  Option::value_t get_val_generic(const std::string &key) const;
+  const Option::value_t& get_val_generic(const std::string &key) const;
   template<typename T> const T& get_val(const std::string &key) const;
   template<typename T> const T& _get_val(const std::string &key) const;
 
@@ -200,7 +200,7 @@ private:
   void validate_default_settings();
 
   int _get_val(const std::string &key, std::string *value) const;
-  Option::value_t _get_val_generic(const std::string &key) const;
+  const Option::value_t& _get_val_generic(const std::string &key) const;
   void _show_config(std::ostream *out, Formatter *f);
 
   void _get_my_sections(std::vector <std::string> &sections) const;